blob: 3a494cfa56d2838df387daec0ef2205f1b1be3e0 [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,
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -070031 SCAN_EXCEED_SWAP_PTE,
32 SCAN_EXCEED_SHARED_PTE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070033 SCAN_PTE_NON_PRESENT,
Peter Xue1e267c2020-04-06 20:06:04 -070034 SCAN_PTE_UFFD_WP,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070035 SCAN_PAGE_RO,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -070036 SCAN_LACK_REFERENCED_PAGE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070037 SCAN_PAGE_NULL,
38 SCAN_SCAN_ABORT,
39 SCAN_PAGE_COUNT,
40 SCAN_PAGE_LRU,
41 SCAN_PAGE_LOCK,
42 SCAN_PAGE_ANON,
43 SCAN_PAGE_COMPOUND,
44 SCAN_ANY_PROCESS,
45 SCAN_VMA_NULL,
46 SCAN_VMA_CHECK,
47 SCAN_ADDRESS_RANGE,
48 SCAN_SWAP_CACHE_PAGE,
49 SCAN_DEL_PAGE_LRU,
50 SCAN_ALLOC_HUGE_PAGE_FAIL,
51 SCAN_CGROUP_CHARGE_FAIL,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070052 SCAN_TRUNCATED,
Song Liu99cb0db2019-09-23 15:38:00 -070053 SCAN_PAGE_HAS_PRIVATE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070054};
55
56#define CREATE_TRACE_POINTS
57#include <trace/events/huge_memory.h>
58
59/* default scan 8*512 pte (or vmas) every 30 second */
60static unsigned int khugepaged_pages_to_scan __read_mostly;
61static unsigned int khugepaged_pages_collapsed;
62static unsigned int khugepaged_full_scans;
63static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
64/* during fragmentation poll the hugepage allocator once every minute */
65static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
66static unsigned long khugepaged_sleep_expire;
67static DEFINE_SPINLOCK(khugepaged_mm_lock);
68static 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 */
74static unsigned int khugepaged_max_ptes_none __read_mostly;
75static unsigned int khugepaged_max_ptes_swap __read_mostly;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -070076static unsigned int khugepaged_max_ptes_shared __read_mostly;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070077
78#define MM_SLOTS_HASH_BITS 10
79static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
80
81static struct kmem_cache *mm_slot_cache __read_mostly;
82
Song Liu27e1f822019-09-23 15:38:30 -070083#define MAX_PTE_MAPPED_THP 8
84
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070085/**
86 * struct mm_slot - hash lookup from mm to mm_slot
87 * @hash: hash collision list
88 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
89 * @mm: the mm that this information is valid for
90 */
91struct mm_slot {
92 struct hlist_node hash;
93 struct list_head mm_node;
94 struct mm_struct *mm;
Song Liu27e1f822019-09-23 15:38:30 -070095
96 /* pte-mapped THP in this mm */
97 int nr_pte_mapped_thp;
98 unsigned long pte_mapped_thp[MAX_PTE_MAPPED_THP];
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070099};
100
101/**
102 * struct khugepaged_scan - cursor for scanning
103 * @mm_head: the head of the mm list to scan
104 * @mm_slot: the current mm_slot we are scanning
105 * @address: the next address inside that to be scanned
106 *
107 * There is only the one khugepaged_scan instance of this cursor structure.
108 */
109struct khugepaged_scan {
110 struct list_head mm_head;
111 struct mm_slot *mm_slot;
112 unsigned long address;
113};
114
115static struct khugepaged_scan khugepaged_scan = {
116 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
117};
118
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800119#ifdef CONFIG_SYSFS
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700120static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
121 struct kobj_attribute *attr,
122 char *buf)
123{
124 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
125}
126
127static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
128 struct kobj_attribute *attr,
129 const char *buf, size_t count)
130{
131 unsigned long msecs;
132 int err;
133
134 err = kstrtoul(buf, 10, &msecs);
135 if (err || msecs > UINT_MAX)
136 return -EINVAL;
137
138 khugepaged_scan_sleep_millisecs = msecs;
139 khugepaged_sleep_expire = 0;
140 wake_up_interruptible(&khugepaged_wait);
141
142 return count;
143}
144static struct kobj_attribute scan_sleep_millisecs_attr =
145 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
146 scan_sleep_millisecs_store);
147
148static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
149 struct kobj_attribute *attr,
150 char *buf)
151{
152 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
153}
154
155static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
156 struct kobj_attribute *attr,
157 const char *buf, size_t count)
158{
159 unsigned long msecs;
160 int err;
161
162 err = kstrtoul(buf, 10, &msecs);
163 if (err || msecs > UINT_MAX)
164 return -EINVAL;
165
166 khugepaged_alloc_sleep_millisecs = msecs;
167 khugepaged_sleep_expire = 0;
168 wake_up_interruptible(&khugepaged_wait);
169
170 return count;
171}
172static struct kobj_attribute alloc_sleep_millisecs_attr =
173 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
174 alloc_sleep_millisecs_store);
175
176static ssize_t pages_to_scan_show(struct kobject *kobj,
177 struct kobj_attribute *attr,
178 char *buf)
179{
180 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
181}
182static ssize_t pages_to_scan_store(struct kobject *kobj,
183 struct kobj_attribute *attr,
184 const char *buf, size_t count)
185{
186 int err;
187 unsigned long pages;
188
189 err = kstrtoul(buf, 10, &pages);
190 if (err || !pages || pages > UINT_MAX)
191 return -EINVAL;
192
193 khugepaged_pages_to_scan = pages;
194
195 return count;
196}
197static struct kobj_attribute pages_to_scan_attr =
198 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
199 pages_to_scan_store);
200
201static ssize_t pages_collapsed_show(struct kobject *kobj,
202 struct kobj_attribute *attr,
203 char *buf)
204{
205 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
206}
207static struct kobj_attribute pages_collapsed_attr =
208 __ATTR_RO(pages_collapsed);
209
210static ssize_t full_scans_show(struct kobject *kobj,
211 struct kobj_attribute *attr,
212 char *buf)
213{
214 return sprintf(buf, "%u\n", khugepaged_full_scans);
215}
216static struct kobj_attribute full_scans_attr =
217 __ATTR_RO(full_scans);
218
219static ssize_t khugepaged_defrag_show(struct kobject *kobj,
220 struct kobj_attribute *attr, char *buf)
221{
222 return single_hugepage_flag_show(kobj, attr, buf,
223 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
224}
225static ssize_t khugepaged_defrag_store(struct kobject *kobj,
226 struct kobj_attribute *attr,
227 const char *buf, size_t count)
228{
229 return single_hugepage_flag_store(kobj, attr, buf, count,
230 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
231}
232static struct kobj_attribute khugepaged_defrag_attr =
233 __ATTR(defrag, 0644, khugepaged_defrag_show,
234 khugepaged_defrag_store);
235
236/*
237 * max_ptes_none controls if khugepaged should collapse hugepages over
238 * any unmapped ptes in turn potentially increasing the memory
239 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
240 * reduce the available free memory in the system as it
241 * runs. Increasing max_ptes_none will instead potentially reduce the
242 * free memory in the system during the khugepaged scan.
243 */
244static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
245 struct kobj_attribute *attr,
246 char *buf)
247{
248 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
249}
250static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
251 struct kobj_attribute *attr,
252 const char *buf, size_t count)
253{
254 int err;
255 unsigned long max_ptes_none;
256
257 err = kstrtoul(buf, 10, &max_ptes_none);
258 if (err || max_ptes_none > HPAGE_PMD_NR-1)
259 return -EINVAL;
260
261 khugepaged_max_ptes_none = max_ptes_none;
262
263 return count;
264}
265static struct kobj_attribute khugepaged_max_ptes_none_attr =
266 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
267 khugepaged_max_ptes_none_store);
268
269static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
270 struct kobj_attribute *attr,
271 char *buf)
272{
273 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
274}
275
276static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
277 struct kobj_attribute *attr,
278 const char *buf, size_t count)
279{
280 int err;
281 unsigned long max_ptes_swap;
282
283 err = kstrtoul(buf, 10, &max_ptes_swap);
284 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
285 return -EINVAL;
286
287 khugepaged_max_ptes_swap = max_ptes_swap;
288
289 return count;
290}
291
292static struct kobj_attribute khugepaged_max_ptes_swap_attr =
293 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
294 khugepaged_max_ptes_swap_store);
295
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700296static ssize_t khugepaged_max_ptes_shared_show(struct kobject *kobj,
297 struct kobj_attribute *attr,
298 char *buf)
299{
300 return sprintf(buf, "%u\n", khugepaged_max_ptes_shared);
301}
302
303static ssize_t khugepaged_max_ptes_shared_store(struct kobject *kobj,
304 struct kobj_attribute *attr,
305 const char *buf, size_t count)
306{
307 int err;
308 unsigned long max_ptes_shared;
309
310 err = kstrtoul(buf, 10, &max_ptes_shared);
311 if (err || max_ptes_shared > HPAGE_PMD_NR-1)
312 return -EINVAL;
313
314 khugepaged_max_ptes_shared = max_ptes_shared;
315
316 return count;
317}
318
319static struct kobj_attribute khugepaged_max_ptes_shared_attr =
320 __ATTR(max_ptes_shared, 0644, khugepaged_max_ptes_shared_show,
321 khugepaged_max_ptes_shared_store);
322
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700323static struct attribute *khugepaged_attr[] = {
324 &khugepaged_defrag_attr.attr,
325 &khugepaged_max_ptes_none_attr.attr,
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700326 &khugepaged_max_ptes_swap_attr.attr,
327 &khugepaged_max_ptes_shared_attr.attr,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700328 &pages_to_scan_attr.attr,
329 &pages_collapsed_attr.attr,
330 &full_scans_attr.attr,
331 &scan_sleep_millisecs_attr.attr,
332 &alloc_sleep_millisecs_attr.attr,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700333 NULL,
334};
335
336struct attribute_group khugepaged_attr_group = {
337 .attrs = khugepaged_attr,
338 .name = "khugepaged",
339};
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800340#endif /* CONFIG_SYSFS */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700341
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700342int hugepage_madvise(struct vm_area_struct *vma,
343 unsigned long *vm_flags, int advice)
344{
345 switch (advice) {
346 case MADV_HUGEPAGE:
347#ifdef CONFIG_S390
348 /*
349 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
350 * can't handle this properly after s390_enable_sie, so we simply
351 * ignore the madvise to prevent qemu from causing a SIGSEGV.
352 */
353 if (mm_has_pgste(vma->vm_mm))
354 return 0;
355#endif
356 *vm_flags &= ~VM_NOHUGEPAGE;
357 *vm_flags |= VM_HUGEPAGE;
358 /*
359 * If the vma become good for khugepaged to scan,
360 * register it here without waiting a page fault that
361 * may not happen any time soon.
362 */
363 if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
364 khugepaged_enter_vma_merge(vma, *vm_flags))
365 return -ENOMEM;
366 break;
367 case MADV_NOHUGEPAGE:
368 *vm_flags &= ~VM_HUGEPAGE;
369 *vm_flags |= VM_NOHUGEPAGE;
370 /*
371 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
372 * this vma even if we leave the mm registered in khugepaged if
373 * it got registered before VM_NOHUGEPAGE was set.
374 */
375 break;
376 }
377
378 return 0;
379}
380
381int __init khugepaged_init(void)
382{
383 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
384 sizeof(struct mm_slot),
385 __alignof__(struct mm_slot), 0, NULL);
386 if (!mm_slot_cache)
387 return -ENOMEM;
388
389 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
390 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
391 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700392 khugepaged_max_ptes_shared = HPAGE_PMD_NR / 2;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700393
394 return 0;
395}
396
397void __init khugepaged_destroy(void)
398{
399 kmem_cache_destroy(mm_slot_cache);
400}
401
402static inline struct mm_slot *alloc_mm_slot(void)
403{
404 if (!mm_slot_cache) /* initialization failed */
405 return NULL;
406 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
407}
408
409static inline void free_mm_slot(struct mm_slot *mm_slot)
410{
411 kmem_cache_free(mm_slot_cache, mm_slot);
412}
413
414static struct mm_slot *get_mm_slot(struct mm_struct *mm)
415{
416 struct mm_slot *mm_slot;
417
418 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
419 if (mm == mm_slot->mm)
420 return mm_slot;
421
422 return NULL;
423}
424
425static void insert_to_mm_slots_hash(struct mm_struct *mm,
426 struct mm_slot *mm_slot)
427{
428 mm_slot->mm = mm;
429 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
430}
431
432static inline int khugepaged_test_exit(struct mm_struct *mm)
433{
Hugh Dickinsbbe98f92020-08-06 23:26:25 -0700434 return atomic_read(&mm->mm_users) == 0 || !mmget_still_valid(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700435}
436
Song Liu50f8b922018-08-17 15:47:00 -0700437static bool hugepage_vma_check(struct vm_area_struct *vma,
438 unsigned long vm_flags)
Yang Shic2231022018-08-17 15:45:26 -0700439{
Song Liu50f8b922018-08-17 15:47:00 -0700440 if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
441 (vm_flags & VM_NOHUGEPAGE) ||
Yang Shic2231022018-08-17 15:45:26 -0700442 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
443 return false;
Song Liu99cb0db2019-09-23 15:38:00 -0700444
445 if (shmem_file(vma->vm_file) ||
446 (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) &&
447 vma->vm_file &&
448 (vm_flags & VM_DENYWRITE))) {
Yang Shic2231022018-08-17 15:45:26 -0700449 return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
450 HPAGE_PMD_NR);
451 }
452 if (!vma->anon_vma || vma->vm_ops)
453 return false;
Anshuman Khandual222100e2020-04-01 21:07:52 -0700454 if (vma_is_temporary_stack(vma))
Yang Shic2231022018-08-17 15:45:26 -0700455 return false;
Song Liu50f8b922018-08-17 15:47:00 -0700456 return !(vm_flags & VM_NO_KHUGEPAGED);
Yang Shic2231022018-08-17 15:45:26 -0700457}
458
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700459int __khugepaged_enter(struct mm_struct *mm)
460{
461 struct mm_slot *mm_slot;
462 int wakeup;
463
464 mm_slot = alloc_mm_slot();
465 if (!mm_slot)
466 return -ENOMEM;
467
468 /* __khugepaged_exit() must not run from under us */
Hugh Dickinsf3f99d62020-08-20 17:42:02 -0700469 VM_BUG_ON_MM(atomic_read(&mm->mm_users) == 0, mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700470 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
471 free_mm_slot(mm_slot);
472 return 0;
473 }
474
475 spin_lock(&khugepaged_mm_lock);
476 insert_to_mm_slots_hash(mm, mm_slot);
477 /*
478 * Insert just behind the scanning cursor, to let the area settle
479 * down a little.
480 */
481 wakeup = list_empty(&khugepaged_scan.mm_head);
482 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
483 spin_unlock(&khugepaged_mm_lock);
484
Vegard Nossumf1f10072017-02-27 14:30:07 -0800485 mmgrab(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700486 if (wakeup)
487 wake_up_interruptible(&khugepaged_wait);
488
489 return 0;
490}
491
492int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
493 unsigned long vm_flags)
494{
495 unsigned long hstart, hend;
Yang Shic2231022018-08-17 15:45:26 -0700496
497 /*
Song Liu99cb0db2019-09-23 15:38:00 -0700498 * khugepaged only supports read-only files for non-shmem files.
499 * khugepaged does not yet work on special mappings. And
500 * file-private shmem THP is not supported.
Yang Shic2231022018-08-17 15:45:26 -0700501 */
Song Liu50f8b922018-08-17 15:47:00 -0700502 if (!hugepage_vma_check(vma, vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700503 return 0;
Yang Shic2231022018-08-17 15:45:26 -0700504
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700505 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
506 hend = vma->vm_end & HPAGE_PMD_MASK;
507 if (hstart < hend)
508 return khugepaged_enter(vma, vm_flags);
509 return 0;
510}
511
512void __khugepaged_exit(struct mm_struct *mm)
513{
514 struct mm_slot *mm_slot;
515 int free = 0;
516
517 spin_lock(&khugepaged_mm_lock);
518 mm_slot = get_mm_slot(mm);
519 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
520 hash_del(&mm_slot->hash);
521 list_del(&mm_slot->mm_node);
522 free = 1;
523 }
524 spin_unlock(&khugepaged_mm_lock);
525
526 if (free) {
527 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
528 free_mm_slot(mm_slot);
529 mmdrop(mm);
530 } else if (mm_slot) {
531 /*
532 * This is required to serialize against
533 * khugepaged_test_exit() (which is guaranteed to run
534 * under mmap sem read mode). Stop here (after we
535 * return all pagetables will be destroyed) until
536 * khugepaged has finished working on the pagetables
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700537 * under the mmap_lock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700538 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700539 mmap_write_lock(mm);
540 mmap_write_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700541 }
542}
543
544static void release_pte_page(struct page *page)
545{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700546 mod_node_page_state(page_pgdat(page),
547 NR_ISOLATED_ANON + page_is_file_lru(page),
548 -compound_nr(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700549 unlock_page(page);
550 putback_lru_page(page);
551}
552
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700553static void release_pte_pages(pte_t *pte, pte_t *_pte,
554 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700555{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700556 struct page *page, *tmp;
557
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700558 while (--_pte >= pte) {
559 pte_t pteval = *_pte;
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700560
561 page = pte_page(pteval);
562 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)) &&
563 !PageCompound(page))
564 release_pte_page(page);
565 }
566
567 list_for_each_entry_safe(page, tmp, compound_pagelist, lru) {
568 list_del(&page->lru);
569 release_pte_page(page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700570 }
571}
572
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700573static bool is_refcount_suitable(struct page *page)
574{
575 int expected_refcount;
576
577 expected_refcount = total_mapcount(page);
578 if (PageSwapCache(page))
579 expected_refcount += compound_nr(page);
580
581 return page_count(page) == expected_refcount;
582}
583
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700584static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
585 unsigned long address,
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700586 pte_t *pte,
587 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700588{
589 struct page *page = NULL;
590 pte_t *_pte;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700591 int none_or_zero = 0, shared = 0, result = 0, referenced = 0;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700592 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700593
594 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
595 _pte++, address += PAGE_SIZE) {
596 pte_t pteval = *_pte;
597 if (pte_none(pteval) || (pte_present(pteval) &&
598 is_zero_pfn(pte_pfn(pteval)))) {
599 if (!userfaultfd_armed(vma) &&
600 ++none_or_zero <= khugepaged_max_ptes_none) {
601 continue;
602 } else {
603 result = SCAN_EXCEED_NONE_PTE;
604 goto out;
605 }
606 }
607 if (!pte_present(pteval)) {
608 result = SCAN_PTE_NON_PRESENT;
609 goto out;
610 }
611 page = vm_normal_page(vma, address, pteval);
612 if (unlikely(!page)) {
613 result = SCAN_PAGE_NULL;
614 goto out;
615 }
616
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700617 VM_BUG_ON_PAGE(!PageAnon(page), page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700618
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700619 if (page_mapcount(page) > 1 &&
620 ++shared > khugepaged_max_ptes_shared) {
621 result = SCAN_EXCEED_SHARED_PTE;
622 goto out;
623 }
624
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700625 if (PageCompound(page)) {
626 struct page *p;
627 page = compound_head(page);
628
629 /*
630 * Check if we have dealt with the compound page
631 * already
632 */
633 list_for_each_entry(p, compound_pagelist, lru) {
634 if (page == p)
635 goto next;
636 }
637 }
638
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700639 /*
640 * We can do it before isolate_lru_page because the
641 * page can't be freed from under us. NOTE: PG_lock
642 * is needed to serialize against split_huge_page
643 * when invoked from the VM.
644 */
645 if (!trylock_page(page)) {
646 result = SCAN_PAGE_LOCK;
647 goto out;
648 }
649
650 /*
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700651 * Check if the page has any GUP (or other external) pins.
652 *
653 * The page table that maps the page has been already unlinked
654 * from the page table tree and this process cannot get
655 * an additinal pin on the page.
656 *
657 * New pins can come later if the page is shared across fork,
658 * but not from this process. The other process cannot write to
659 * the page, only trigger CoW.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700660 */
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700661 if (!is_refcount_suitable(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700662 unlock_page(page);
663 result = SCAN_PAGE_COUNT;
664 goto out;
665 }
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700666 if (!pte_write(pteval) && PageSwapCache(page) &&
667 !reuse_swap_page(page, NULL)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700668 /*
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700669 * Page is in the swap cache and cannot be re-used.
670 * It cannot be collapsed into a THP.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700671 */
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700672 unlock_page(page);
673 result = SCAN_SWAP_CACHE_PAGE;
674 goto out;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700675 }
676
677 /*
678 * Isolate the page to avoid collapsing an hugepage
679 * currently in use by the VM.
680 */
681 if (isolate_lru_page(page)) {
682 unlock_page(page);
683 result = SCAN_DEL_PAGE_LRU;
684 goto out;
685 }
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700686 mod_node_page_state(page_pgdat(page),
687 NR_ISOLATED_ANON + page_is_file_lru(page),
688 compound_nr(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700689 VM_BUG_ON_PAGE(!PageLocked(page), page);
690 VM_BUG_ON_PAGE(PageLRU(page), page);
691
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700692 if (PageCompound(page))
693 list_add_tail(&page->lru, compound_pagelist);
694next:
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700695 /* There should be enough young pte to collapse the page */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700696 if (pte_young(pteval) ||
697 page_is_young(page) || PageReferenced(page) ||
698 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700699 referenced++;
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700700
701 if (pte_write(pteval))
702 writable = true;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700703 }
704 if (likely(writable)) {
705 if (likely(referenced)) {
706 result = SCAN_SUCCEED;
707 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
708 referenced, writable, result);
709 return 1;
710 }
711 } else {
712 result = SCAN_PAGE_RO;
713 }
714
715out:
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700716 release_pte_pages(pte, _pte, compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700717 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
718 referenced, writable, result);
719 return 0;
720}
721
722static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
723 struct vm_area_struct *vma,
724 unsigned long address,
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700725 spinlock_t *ptl,
726 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700727{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700728 struct page *src_page, *tmp;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700729 pte_t *_pte;
David Rientjes338a16b2017-05-12 15:47:03 -0700730 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
731 _pte++, page++, address += PAGE_SIZE) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700732 pte_t pteval = *_pte;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700733
734 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
735 clear_user_highpage(page, address);
736 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
737 if (is_zero_pfn(pte_pfn(pteval))) {
738 /*
739 * ptl mostly unnecessary.
740 */
741 spin_lock(ptl);
742 /*
743 * paravirt calls inside pte_clear here are
744 * superfluous.
745 */
746 pte_clear(vma->vm_mm, address, _pte);
747 spin_unlock(ptl);
748 }
749 } else {
750 src_page = pte_page(pteval);
751 copy_user_highpage(page, src_page, address, vma);
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700752 if (!PageCompound(src_page))
753 release_pte_page(src_page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700754 /*
755 * ptl mostly unnecessary, but preempt has to
756 * be disabled to update the per-cpu stats
757 * inside page_remove_rmap().
758 */
759 spin_lock(ptl);
760 /*
761 * paravirt calls inside pte_clear here are
762 * superfluous.
763 */
764 pte_clear(vma->vm_mm, address, _pte);
765 page_remove_rmap(src_page, false);
766 spin_unlock(ptl);
767 free_page_and_swap_cache(src_page);
768 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700769 }
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700770
771 list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) {
772 list_del(&src_page->lru);
773 release_pte_page(src_page);
774 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700775}
776
777static void khugepaged_alloc_sleep(void)
778{
779 DEFINE_WAIT(wait);
780
781 add_wait_queue(&khugepaged_wait, &wait);
782 freezable_schedule_timeout_interruptible(
783 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
784 remove_wait_queue(&khugepaged_wait, &wait);
785}
786
787static int khugepaged_node_load[MAX_NUMNODES];
788
789static bool khugepaged_scan_abort(int nid)
790{
791 int i;
792
793 /*
Mel Gormana5f5f912016-07-28 15:46:32 -0700794 * If node_reclaim_mode is disabled, then no extra effort is made to
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700795 * allocate memory locally.
796 */
Mel Gormana5f5f912016-07-28 15:46:32 -0700797 if (!node_reclaim_mode)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700798 return false;
799
800 /* If there is a count for this node already, it must be acceptable */
801 if (khugepaged_node_load[nid])
802 return false;
803
804 for (i = 0; i < MAX_NUMNODES; i++) {
805 if (!khugepaged_node_load[i])
806 continue;
Matt Fleminga55c7452019-08-08 20:53:01 +0100807 if (node_distance(nid, i) > node_reclaim_distance)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700808 return true;
809 }
810 return false;
811}
812
813/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
814static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
815{
Vlastimil Babka25160352016-07-28 15:49:25 -0700816 return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700817}
818
819#ifdef CONFIG_NUMA
820static int khugepaged_find_target_node(void)
821{
822 static int last_khugepaged_target_node = NUMA_NO_NODE;
823 int nid, target_node = 0, max_value = 0;
824
825 /* find first node with max normal pages hit */
826 for (nid = 0; nid < MAX_NUMNODES; nid++)
827 if (khugepaged_node_load[nid] > max_value) {
828 max_value = khugepaged_node_load[nid];
829 target_node = nid;
830 }
831
832 /* do some balance if several nodes have the same hit record */
833 if (target_node <= last_khugepaged_target_node)
834 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
835 nid++)
836 if (max_value == khugepaged_node_load[nid]) {
837 target_node = nid;
838 break;
839 }
840
841 last_khugepaged_target_node = target_node;
842 return target_node;
843}
844
845static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
846{
847 if (IS_ERR(*hpage)) {
848 if (!*wait)
849 return false;
850
851 *wait = false;
852 *hpage = NULL;
853 khugepaged_alloc_sleep();
854 } else if (*hpage) {
855 put_page(*hpage);
856 *hpage = NULL;
857 }
858
859 return true;
860}
861
862static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700863khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700864{
865 VM_BUG_ON_PAGE(*hpage, *hpage);
866
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700867 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
868 if (unlikely(!*hpage)) {
869 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
870 *hpage = ERR_PTR(-ENOMEM);
871 return NULL;
872 }
873
874 prep_transhuge_page(*hpage);
875 count_vm_event(THP_COLLAPSE_ALLOC);
876 return *hpage;
877}
878#else
879static int khugepaged_find_target_node(void)
880{
881 return 0;
882}
883
884static inline struct page *alloc_khugepaged_hugepage(void)
885{
886 struct page *page;
887
888 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
889 HPAGE_PMD_ORDER);
890 if (page)
891 prep_transhuge_page(page);
892 return page;
893}
894
895static struct page *khugepaged_alloc_hugepage(bool *wait)
896{
897 struct page *hpage;
898
899 do {
900 hpage = alloc_khugepaged_hugepage();
901 if (!hpage) {
902 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
903 if (!*wait)
904 return NULL;
905
906 *wait = false;
907 khugepaged_alloc_sleep();
908 } else
909 count_vm_event(THP_COLLAPSE_ALLOC);
910 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
911
912 return hpage;
913}
914
915static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
916{
Hugh Dickins033b5d72020-10-09 20:07:59 -0700917 /*
918 * If the hpage allocated earlier was briefly exposed in page cache
919 * before collapse_file() failed, it is possible that racing lookups
920 * have not yet completed, and would then be unpleasantly surprised by
921 * finding the hpage reused for the same mapping at a different offset.
922 * Just release the previous allocation if there is any danger of that.
923 */
924 if (*hpage && page_count(*hpage) > 1) {
925 put_page(*hpage);
926 *hpage = NULL;
927 }
928
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700929 if (!*hpage)
930 *hpage = khugepaged_alloc_hugepage(wait);
931
932 if (unlikely(!*hpage))
933 return false;
934
935 return true;
936}
937
938static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700939khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700940{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700941 VM_BUG_ON(!*hpage);
942
943 return *hpage;
944}
945#endif
946
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700947/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700948 * If mmap_lock temporarily dropped, revalidate vma
949 * before taking mmap_lock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700950 * Return 0 if succeeds, otherwise return none-zero
951 * value (scan code).
952 */
953
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700954static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
955 struct vm_area_struct **vmap)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700956{
957 struct vm_area_struct *vma;
958 unsigned long hstart, hend;
959
960 if (unlikely(khugepaged_test_exit(mm)))
961 return SCAN_ANY_PROCESS;
962
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700963 *vmap = vma = find_vma(mm, address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700964 if (!vma)
965 return SCAN_VMA_NULL;
966
967 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
968 hend = vma->vm_end & HPAGE_PMD_MASK;
969 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
970 return SCAN_ADDRESS_RANGE;
Song Liu50f8b922018-08-17 15:47:00 -0700971 if (!hugepage_vma_check(vma, vma->vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700972 return SCAN_VMA_CHECK;
Kirill A. Shutemov594cced2020-07-23 21:15:34 -0700973 /* Anon VMA expected */
974 if (!vma->anon_vma || vma->vm_ops)
975 return SCAN_VMA_CHECK;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700976 return 0;
977}
978
979/*
980 * Bring missing pages in from swap, to complete THP collapse.
981 * Only done if khugepaged_scan_pmd believes it is worthwhile.
982 *
983 * Called and returns without pte mapped or spinlocks held,
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700984 * but with mmap_lock held to protect against vma changes.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700985 */
986
987static bool __collapse_huge_page_swapin(struct mm_struct *mm,
988 struct vm_area_struct *vma,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700989 unsigned long address, pmd_t *pmd,
990 int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700991{
Souptick Joarder2b740302018-08-23 17:01:36 -0700992 int swapped_in = 0;
993 vm_fault_t ret = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800994 struct vm_fault vmf = {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700995 .vma = vma,
996 .address = address,
997 .flags = FAULT_FLAG_ALLOW_RETRY,
998 .pmd = pmd,
Jan Kara0721ec82016-12-14 15:07:04 -0800999 .pgoff = linear_page_index(vma, address),
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001000 };
1001
Jan Kara82b0f8c2016-12-14 15:06:58 -08001002 vmf.pte = pte_offset_map(pmd, address);
1003 for (; vmf.address < address + HPAGE_PMD_NR*PAGE_SIZE;
1004 vmf.pte++, vmf.address += PAGE_SIZE) {
Jan Kara29943022016-12-14 15:07:16 -08001005 vmf.orig_pte = *vmf.pte;
1006 if (!is_swap_pte(vmf.orig_pte))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001007 continue;
1008 swapped_in++;
Jan Kara29943022016-12-14 15:07:16 -08001009 ret = do_swap_page(&vmf);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001010
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001011 /* do_swap_page returns VM_FAULT_RETRY with released mmap_lock */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001012 if (ret & VM_FAULT_RETRY) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001013 mmap_read_lock(mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001014 if (hugepage_vma_revalidate(mm, address, &vmf.vma)) {
Ebru Akagunduz47f863e2016-07-26 15:26:43 -07001015 /* vma is no longer available, don't continue to swapin */
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001016 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001017 return false;
Ebru Akagunduz47f863e2016-07-26 15:26:43 -07001018 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001019 /* check if the pmd is still valid */
SeongJae Park835152a2017-05-12 15:46:38 -07001020 if (mm_find_pmd(mm, address) != pmd) {
1021 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001022 return false;
SeongJae Park835152a2017-05-12 15:46:38 -07001023 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001024 }
1025 if (ret & VM_FAULT_ERROR) {
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001026 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001027 return false;
1028 }
1029 /* pte is unmapped now, we need to map it */
Jan Kara82b0f8c2016-12-14 15:06:58 -08001030 vmf.pte = pte_offset_map(pmd, vmf.address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001031 }
Jan Kara82b0f8c2016-12-14 15:06:58 -08001032 vmf.pte--;
1033 pte_unmap(vmf.pte);
Kirill A. Shutemovae2c5d82020-06-03 16:00:17 -07001034
1035 /* Drain LRU add pagevec to remove extra pin on the swapped in pages */
1036 if (swapped_in)
1037 lru_add_drain();
1038
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001039 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001040 return true;
1041}
1042
1043static void collapse_huge_page(struct mm_struct *mm,
1044 unsigned long address,
1045 struct page **hpage,
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001046 int node, int referenced, int unmapped)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001047{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -07001048 LIST_HEAD(compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001049 pmd_t *pmd, _pmd;
1050 pte_t *pte;
1051 pgtable_t pgtable;
1052 struct page *new_page;
1053 spinlock_t *pmd_ptl, *pte_ptl;
1054 int isolated = 0, result = 0;
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001055 struct vm_area_struct *vma;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001056 struct mmu_notifier_range range;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001057 gfp_t gfp;
1058
1059 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1060
1061 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -08001062 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001063
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -07001064 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001065 * Before allocating the hugepage, release the mmap_lock read lock.
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -07001066 * The allocation can take potentially a long time if it involves
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001067 * sync compaction, and we do not need to hold the mmap_lock during
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -07001068 * that. We will recheck the vma after taking it again in write mode.
1069 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001070 mmap_read_unlock(mm);
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -07001071 new_page = khugepaged_alloc_page(hpage, gfp, node);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001072 if (!new_page) {
1073 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
1074 goto out_nolock;
1075 }
1076
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07001077 if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001078 result = SCAN_CGROUP_CHARGE_FAIL;
1079 goto out_nolock;
1080 }
Johannes Weiner9d82c692020-06-03 16:02:04 -07001081 count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001082
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001083 mmap_read_lock(mm);
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001084 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001085 if (result) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001086 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001087 goto out_nolock;
1088 }
1089
1090 pmd = mm_find_pmd(mm, address);
1091 if (!pmd) {
1092 result = SCAN_PMD_NULL;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001093 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001094 goto out_nolock;
1095 }
1096
1097 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001098 * __collapse_huge_page_swapin always returns with mmap_lock locked.
1099 * If it fails, we release mmap_lock and jump out_nolock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001100 * Continuing to collapse causes inconsistency.
1101 */
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001102 if (unmapped && !__collapse_huge_page_swapin(mm, vma, address,
1103 pmd, referenced)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001104 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001105 goto out_nolock;
1106 }
1107
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001108 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001109 /*
1110 * Prevent all access to pagetables with the exception of
1111 * gup_fast later handled by the ptep_clear_flush and the VM
1112 * handled by the anon_vma lock + PG_lock.
1113 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001114 mmap_write_lock(mm);
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001115 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001116 if (result)
1117 goto out;
1118 /* check if the pmd is still valid */
1119 if (mm_find_pmd(mm, address) != pmd)
1120 goto out;
1121
1122 anon_vma_lock_write(vma->anon_vma);
1123
Jérôme Glisse7269f992019-05-13 17:20:53 -07001124 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001125 address, address + HPAGE_PMD_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001126 mmu_notifier_invalidate_range_start(&range);
Ville Syrjäläec649c9d2019-11-05 21:16:48 -08001127
1128 pte = pte_offset_map(pmd, address);
1129 pte_ptl = pte_lockptr(mm, pmd);
1130
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001131 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1132 /*
1133 * After this gup_fast can't run anymore. This also removes
1134 * any huge TLB entry from the CPU so we won't allow
1135 * huge and small TLB entries for the same virtual address
1136 * to avoid the risk of CPU bugs in that area.
1137 */
1138 _pmd = pmdp_collapse_flush(vma, address, pmd);
1139 spin_unlock(pmd_ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001140 mmu_notifier_invalidate_range_end(&range);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001141
1142 spin_lock(pte_ptl);
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -07001143 isolated = __collapse_huge_page_isolate(vma, address, pte,
1144 &compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001145 spin_unlock(pte_ptl);
1146
1147 if (unlikely(!isolated)) {
1148 pte_unmap(pte);
1149 spin_lock(pmd_ptl);
1150 BUG_ON(!pmd_none(*pmd));
1151 /*
1152 * We can only use set_pmd_at when establishing
1153 * hugepmds and never for establishing regular pmds that
1154 * points to regular pagetables. Use pmd_populate for that
1155 */
1156 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1157 spin_unlock(pmd_ptl);
1158 anon_vma_unlock_write(vma->anon_vma);
1159 result = SCAN_FAIL;
1160 goto out;
1161 }
1162
1163 /*
1164 * All pages are isolated and locked so anon_vma rmap
1165 * can't run anymore.
1166 */
1167 anon_vma_unlock_write(vma->anon_vma);
1168
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -07001169 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl,
1170 &compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001171 pte_unmap(pte);
1172 __SetPageUptodate(new_page);
1173 pgtable = pmd_pgtable(_pmd);
1174
1175 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001176 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001177
1178 /*
1179 * spin_lock() below is not the equivalent of smp_wmb(), so
1180 * this is needed to avoid the copy_huge_page writes to become
1181 * visible after the set_pmd_at() write.
1182 */
1183 smp_wmb();
1184
1185 spin_lock(pmd_ptl);
1186 BUG_ON(!pmd_none(*pmd));
Johannes Weinerbe5d0a72020-06-03 16:01:57 -07001187 page_add_new_anon_rmap(new_page, vma, address, true);
Joonsoo Kimb5181542020-08-11 18:30:40 -07001188 lru_cache_add_inactive_or_unevictable(new_page, vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001189 pgtable_trans_huge_deposit(mm, pmd, pgtable);
1190 set_pmd_at(mm, address, pmd, _pmd);
1191 update_mmu_cache_pmd(vma, address, pmd);
1192 spin_unlock(pmd_ptl);
1193
1194 *hpage = NULL;
1195
1196 khugepaged_pages_collapsed++;
1197 result = SCAN_SUCCEED;
1198out_up_write:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001199 mmap_write_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001200out_nolock:
Johannes Weiner9d82c692020-06-03 16:02:04 -07001201 if (!IS_ERR_OR_NULL(*hpage))
1202 mem_cgroup_uncharge(*hpage);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001203 trace_mm_collapse_huge_page(mm, isolated, result);
1204 return;
1205out:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001206 goto out_up_write;
1207}
1208
1209static int khugepaged_scan_pmd(struct mm_struct *mm,
1210 struct vm_area_struct *vma,
1211 unsigned long address,
1212 struct page **hpage)
1213{
1214 pmd_t *pmd;
1215 pte_t *pte, *_pte;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -07001216 int ret = 0, result = 0, referenced = 0;
1217 int none_or_zero = 0, shared = 0;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001218 struct page *page = NULL;
1219 unsigned long _address;
1220 spinlock_t *ptl;
1221 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001222 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001223
1224 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1225
1226 pmd = mm_find_pmd(mm, address);
1227 if (!pmd) {
1228 result = SCAN_PMD_NULL;
1229 goto out;
1230 }
1231
1232 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1233 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1234 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
1235 _pte++, _address += PAGE_SIZE) {
1236 pte_t pteval = *_pte;
1237 if (is_swap_pte(pteval)) {
1238 if (++unmapped <= khugepaged_max_ptes_swap) {
Peter Xue1e267c2020-04-06 20:06:04 -07001239 /*
1240 * Always be strict with uffd-wp
1241 * enabled swap entries. Please see
1242 * comment below for pte_uffd_wp().
1243 */
1244 if (pte_swp_uffd_wp(pteval)) {
1245 result = SCAN_PTE_UFFD_WP;
1246 goto out_unmap;
1247 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001248 continue;
1249 } else {
1250 result = SCAN_EXCEED_SWAP_PTE;
1251 goto out_unmap;
1252 }
1253 }
1254 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
1255 if (!userfaultfd_armed(vma) &&
1256 ++none_or_zero <= khugepaged_max_ptes_none) {
1257 continue;
1258 } else {
1259 result = SCAN_EXCEED_NONE_PTE;
1260 goto out_unmap;
1261 }
1262 }
1263 if (!pte_present(pteval)) {
1264 result = SCAN_PTE_NON_PRESENT;
1265 goto out_unmap;
1266 }
Peter Xue1e267c2020-04-06 20:06:04 -07001267 if (pte_uffd_wp(pteval)) {
1268 /*
1269 * Don't collapse the page if any of the small
1270 * PTEs are armed with uffd write protection.
1271 * Here we can also mark the new huge pmd as
1272 * write protected if any of the small ones is
1273 * marked but that could bring uknown
1274 * userfault messages that falls outside of
1275 * the registered range. So, just be simple.
1276 */
1277 result = SCAN_PTE_UFFD_WP;
1278 goto out_unmap;
1279 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001280 if (pte_write(pteval))
1281 writable = true;
1282
1283 page = vm_normal_page(vma, _address, pteval);
1284 if (unlikely(!page)) {
1285 result = SCAN_PAGE_NULL;
1286 goto out_unmap;
1287 }
1288
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -07001289 if (page_mapcount(page) > 1 &&
1290 ++shared > khugepaged_max_ptes_shared) {
1291 result = SCAN_EXCEED_SHARED_PTE;
1292 goto out_unmap;
1293 }
1294
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -07001295 page = compound_head(page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001296
1297 /*
1298 * Record which node the original page is from and save this
1299 * information to khugepaged_node_load[].
1300 * Khupaged will allocate hugepage from the node has the max
1301 * hit record.
1302 */
1303 node = page_to_nid(page);
1304 if (khugepaged_scan_abort(node)) {
1305 result = SCAN_SCAN_ABORT;
1306 goto out_unmap;
1307 }
1308 khugepaged_node_load[node]++;
1309 if (!PageLRU(page)) {
1310 result = SCAN_PAGE_LRU;
1311 goto out_unmap;
1312 }
1313 if (PageLocked(page)) {
1314 result = SCAN_PAGE_LOCK;
1315 goto out_unmap;
1316 }
1317 if (!PageAnon(page)) {
1318 result = SCAN_PAGE_ANON;
1319 goto out_unmap;
1320 }
1321
1322 /*
Kirill A. Shutemov94456892020-06-03 16:00:20 -07001323 * Check if the page has any GUP (or other external) pins.
1324 *
1325 * Here the check is racy it may see totmal_mapcount > refcount
1326 * in some cases.
1327 * For example, one process with one forked child process.
1328 * The parent has the PMD split due to MADV_DONTNEED, then
1329 * the child is trying unmap the whole PMD, but khugepaged
1330 * may be scanning the parent between the child has
1331 * PageDoubleMap flag cleared and dec the mapcount. So
1332 * khugepaged may see total_mapcount > refcount.
1333 *
1334 * But such case is ephemeral we could always retry collapse
1335 * later. However it may report false positive if the page
1336 * has excessive GUP pins (i.e. 512). Anyway the same check
1337 * will be done again later the risk seems low.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001338 */
Kirill A. Shutemov94456892020-06-03 16:00:20 -07001339 if (!is_refcount_suitable(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001340 result = SCAN_PAGE_COUNT;
1341 goto out_unmap;
1342 }
1343 if (pte_young(pteval) ||
1344 page_is_young(page) || PageReferenced(page) ||
1345 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001346 referenced++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001347 }
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001348 if (!writable) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001349 result = SCAN_PAGE_RO;
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001350 } else if (!referenced || (unmapped && referenced < HPAGE_PMD_NR/2)) {
1351 result = SCAN_LACK_REFERENCED_PAGE;
1352 } else {
1353 result = SCAN_SUCCEED;
1354 ret = 1;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001355 }
1356out_unmap:
1357 pte_unmap_unlock(pte, ptl);
1358 if (ret) {
1359 node = khugepaged_find_target_node();
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001360 /* collapse_huge_page will return with the mmap_lock released */
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001361 collapse_huge_page(mm, address, hpage, node,
1362 referenced, unmapped);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001363 }
1364out:
1365 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
1366 none_or_zero, result, unmapped);
1367 return ret;
1368}
1369
1370static void collect_mm_slot(struct mm_slot *mm_slot)
1371{
1372 struct mm_struct *mm = mm_slot->mm;
1373
Lance Roy35f3aa32018-10-04 23:45:47 -07001374 lockdep_assert_held(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001375
1376 if (khugepaged_test_exit(mm)) {
1377 /* free mm_slot */
1378 hash_del(&mm_slot->hash);
1379 list_del(&mm_slot->mm_node);
1380
1381 /*
1382 * Not strictly needed because the mm exited already.
1383 *
1384 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1385 */
1386
1387 /* khugepaged_mm_lock actually not necessary for the below */
1388 free_mm_slot(mm_slot);
1389 mmdrop(mm);
1390 }
1391}
1392
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001393#ifdef CONFIG_SHMEM
Song Liu27e1f822019-09-23 15:38:30 -07001394/*
1395 * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1396 * khugepaged should try to collapse the page table.
1397 */
1398static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
1399 unsigned long addr)
1400{
1401 struct mm_slot *mm_slot;
1402
1403 VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1404
1405 spin_lock(&khugepaged_mm_lock);
1406 mm_slot = get_mm_slot(mm);
1407 if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP))
1408 mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
1409 spin_unlock(&khugepaged_mm_lock);
1410 return 0;
1411}
1412
1413/**
1414 * Try to collapse a pte-mapped THP for mm at address haddr.
1415 *
1416 * This function checks whether all the PTEs in the PMD are pointing to the
1417 * right THP. If so, retract the page table so the THP can refault in with
1418 * as pmd-mapped.
1419 */
1420void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
1421{
1422 unsigned long haddr = addr & HPAGE_PMD_MASK;
1423 struct vm_area_struct *vma = find_vma(mm, haddr);
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001424 struct page *hpage;
Song Liu27e1f822019-09-23 15:38:30 -07001425 pte_t *start_pte, *pte;
1426 pmd_t *pmd, _pmd;
1427 spinlock_t *ptl;
1428 int count = 0;
1429 int i;
1430
1431 if (!vma || !vma->vm_file ||
1432 vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE)
1433 return;
1434
1435 /*
1436 * This vm_flags may not have VM_HUGEPAGE if the page was not
1437 * collapsed by this mm. But we can still collapse if the page is
1438 * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
1439 * will not fail the vma for missing VM_HUGEPAGE
1440 */
1441 if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
1442 return;
1443
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001444 hpage = find_lock_page(vma->vm_file->f_mapping,
1445 linear_page_index(vma, haddr));
1446 if (!hpage)
1447 return;
1448
1449 if (!PageHead(hpage))
1450 goto drop_hpage;
1451
Song Liu27e1f822019-09-23 15:38:30 -07001452 pmd = mm_find_pmd(mm, haddr);
1453 if (!pmd)
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001454 goto drop_hpage;
Song Liu27e1f822019-09-23 15:38:30 -07001455
1456 start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
1457
1458 /* step 1: check all mapped PTEs are to the right huge page */
1459 for (i = 0, addr = haddr, pte = start_pte;
1460 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1461 struct page *page;
1462
1463 /* empty pte, skip */
1464 if (pte_none(*pte))
1465 continue;
1466
1467 /* page swapped out, abort */
1468 if (!pte_present(*pte))
1469 goto abort;
1470
1471 page = vm_normal_page(vma, addr, *pte);
1472
Song Liu27e1f822019-09-23 15:38:30 -07001473 /*
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001474 * Note that uprobe, debugger, or MAP_PRIVATE may change the
1475 * page table, but the new page will not be a subpage of hpage.
Song Liu27e1f822019-09-23 15:38:30 -07001476 */
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001477 if (hpage + i != page)
Song Liu27e1f822019-09-23 15:38:30 -07001478 goto abort;
1479 count++;
1480 }
1481
1482 /* step 2: adjust rmap */
1483 for (i = 0, addr = haddr, pte = start_pte;
1484 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1485 struct page *page;
1486
1487 if (pte_none(*pte))
1488 continue;
1489 page = vm_normal_page(vma, addr, *pte);
1490 page_remove_rmap(page, false);
1491 }
1492
1493 pte_unmap_unlock(start_pte, ptl);
1494
1495 /* step 3: set proper refcount and mm_counters. */
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001496 if (count) {
Song Liu27e1f822019-09-23 15:38:30 -07001497 page_ref_sub(hpage, count);
1498 add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1499 }
1500
1501 /* step 4: collapse pmd */
1502 ptl = pmd_lock(vma->vm_mm, pmd);
Hugh Dickins723a80d2020-08-06 23:26:15 -07001503 _pmd = pmdp_collapse_flush(vma, haddr, pmd);
Song Liu27e1f822019-09-23 15:38:30 -07001504 spin_unlock(ptl);
1505 mm_dec_nr_ptes(mm);
1506 pte_free(mm, pmd_pgtable(_pmd));
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001507
1508drop_hpage:
1509 unlock_page(hpage);
1510 put_page(hpage);
Song Liu27e1f822019-09-23 15:38:30 -07001511 return;
1512
1513abort:
1514 pte_unmap_unlock(start_pte, ptl);
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001515 goto drop_hpage;
Song Liu27e1f822019-09-23 15:38:30 -07001516}
1517
1518static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
1519{
1520 struct mm_struct *mm = mm_slot->mm;
1521 int i;
1522
1523 if (likely(mm_slot->nr_pte_mapped_thp == 0))
1524 return 0;
1525
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001526 if (!mmap_write_trylock(mm))
Song Liu27e1f822019-09-23 15:38:30 -07001527 return -EBUSY;
1528
1529 if (unlikely(khugepaged_test_exit(mm)))
1530 goto out;
1531
1532 for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
1533 collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i]);
1534
1535out:
1536 mm_slot->nr_pte_mapped_thp = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001537 mmap_write_unlock(mm);
Song Liu27e1f822019-09-23 15:38:30 -07001538 return 0;
1539}
1540
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001541static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
1542{
1543 struct vm_area_struct *vma;
Hugh Dickins18e77602020-08-06 23:26:22 -07001544 struct mm_struct *mm;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001545 unsigned long addr;
1546 pmd_t *pmd, _pmd;
1547
1548 i_mmap_lock_write(mapping);
1549 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Song Liu27e1f822019-09-23 15:38:30 -07001550 /*
1551 * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1552 * got written to. These VMAs are likely not worth investing
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001553 * mmap_write_lock(mm) as PMD-mapping is likely to be split
Song Liu27e1f822019-09-23 15:38:30 -07001554 * later.
1555 *
1556 * Not that vma->anon_vma check is racy: it can be set up after
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001557 * the check but before we took mmap_lock by the fault path.
Song Liu27e1f822019-09-23 15:38:30 -07001558 * But page lock would prevent establishing any new ptes of the
1559 * page, so we are safe.
1560 *
1561 * An alternative would be drop the check, but check that page
1562 * table is clear before calling pmdp_collapse_flush() under
1563 * ptl. It has higher chance to recover THP for the VMA, but
1564 * has higher cost too.
1565 */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001566 if (vma->anon_vma)
1567 continue;
1568 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
1569 if (addr & ~HPAGE_PMD_MASK)
1570 continue;
1571 if (vma->vm_end < addr + HPAGE_PMD_SIZE)
1572 continue;
Hugh Dickins18e77602020-08-06 23:26:22 -07001573 mm = vma->vm_mm;
1574 pmd = mm_find_pmd(mm, addr);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001575 if (!pmd)
1576 continue;
1577 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001578 * We need exclusive mmap_lock to retract page table.
Song Liu27e1f822019-09-23 15:38:30 -07001579 *
1580 * We use trylock due to lock inversion: we need to acquire
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001581 * mmap_lock while holding page lock. Fault path does it in
Song Liu27e1f822019-09-23 15:38:30 -07001582 * reverse order. Trylock is a way to avoid deadlock.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001583 */
Hugh Dickins18e77602020-08-06 23:26:22 -07001584 if (mmap_write_trylock(mm)) {
1585 if (!khugepaged_test_exit(mm)) {
1586 spinlock_t *ptl = pmd_lock(mm, pmd);
1587 /* assume page table is clear */
1588 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1589 spin_unlock(ptl);
1590 mm_dec_nr_ptes(mm);
1591 pte_free(mm, pmd_pgtable(_pmd));
1592 }
1593 mmap_write_unlock(mm);
Song Liu27e1f822019-09-23 15:38:30 -07001594 } else {
1595 /* Try again later */
Hugh Dickins18e77602020-08-06 23:26:22 -07001596 khugepaged_add_pte_mapped_thp(mm, addr);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001597 }
1598 }
1599 i_mmap_unlock_write(mapping);
1600}
1601
1602/**
Song Liu99cb0db2019-09-23 15:38:00 -07001603 * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001604 *
1605 * Basic scheme is simple, details are more complex:
Hugh Dickins87c460a2018-11-30 14:10:43 -08001606 * - allocate and lock a new huge page;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001607 * - scan page cache replacing old pages with the new one
Song Liu99cb0db2019-09-23 15:38:00 -07001608 * + swap/gup in pages if necessary;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001609 * + fill in gaps;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001610 * + keep old pages around in case rollback is required;
1611 * - if replacing succeeds:
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001612 * + copy data over;
1613 * + free old pages;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001614 * + unlock huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001615 * - if replacing failed;
1616 * + put all pages back and unfreeze them;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001617 * + restore gaps in the page cache;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001618 * + unlock and free huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001619 */
Song Liu579c5712019-09-23 15:37:57 -07001620static void collapse_file(struct mm_struct *mm,
1621 struct file *file, pgoff_t start,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001622 struct page **hpage, int node)
1623{
Song Liu579c5712019-09-23 15:37:57 -07001624 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001625 gfp_t gfp;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001626 struct page *new_page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001627 pgoff_t index, end = start + HPAGE_PMD_NR;
1628 LIST_HEAD(pagelist);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001629 XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001630 int nr_none = 0, result = SCAN_SUCCEED;
Song Liu99cb0db2019-09-23 15:38:00 -07001631 bool is_shmem = shmem_file(file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001632
Song Liu99cb0db2019-09-23 15:38:00 -07001633 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001634 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1635
1636 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -08001637 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001638
1639 new_page = khugepaged_alloc_page(hpage, gfp, node);
1640 if (!new_page) {
1641 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
1642 goto out;
1643 }
1644
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -07001645 if (unlikely(mem_cgroup_charge(new_page, mm, gfp))) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001646 result = SCAN_CGROUP_CHARGE_FAIL;
1647 goto out;
1648 }
Johannes Weiner9d82c692020-06-03 16:02:04 -07001649 count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001650
Hugh Dickins95feeab2018-11-30 14:10:50 -08001651 /* This will be less messy when we use multi-index entries */
1652 do {
1653 xas_lock_irq(&xas);
1654 xas_create_range(&xas);
1655 if (!xas_error(&xas))
1656 break;
1657 xas_unlock_irq(&xas);
1658 if (!xas_nomem(&xas, GFP_KERNEL)) {
Hugh Dickins95feeab2018-11-30 14:10:50 -08001659 result = SCAN_FAIL;
1660 goto out;
1661 }
1662 } while (1);
1663
Hugh Dickins042a3082018-11-30 14:10:39 -08001664 __SetPageLocked(new_page);
Song Liu99cb0db2019-09-23 15:38:00 -07001665 if (is_shmem)
1666 __SetPageSwapBacked(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001667 new_page->index = start;
1668 new_page->mapping = mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001669
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001670 /*
Hugh Dickins87c460a2018-11-30 14:10:43 -08001671 * At this point the new_page is locked and not up-to-date.
1672 * It's safe to insert it into the page cache, because nobody would
1673 * be able to map it or use it in another way until we unlock it.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001674 */
1675
Matthew Wilcox77da9382017-12-04 14:56:08 -05001676 xas_set(&xas, start);
1677 for (index = start; index < end; index++) {
1678 struct page *page = xas_next(&xas);
1679
1680 VM_BUG_ON(index != xas.xa_index);
Song Liu99cb0db2019-09-23 15:38:00 -07001681 if (is_shmem) {
1682 if (!page) {
1683 /*
1684 * Stop if extent has been truncated or
1685 * hole-punched, and is now completely
1686 * empty.
1687 */
1688 if (index == start) {
1689 if (!xas_next_entry(&xas, end - 1)) {
1690 result = SCAN_TRUNCATED;
1691 goto xa_locked;
1692 }
1693 xas_set(&xas, index);
1694 }
1695 if (!shmem_charge(mapping->host, 1)) {
1696 result = SCAN_FAIL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001697 goto xa_locked;
Hugh Dickins701270f2018-11-30 14:10:25 -08001698 }
Song Liu99cb0db2019-09-23 15:38:00 -07001699 xas_store(&xas, new_page);
1700 nr_none++;
1701 continue;
Hugh Dickins701270f2018-11-30 14:10:25 -08001702 }
Song Liu99cb0db2019-09-23 15:38:00 -07001703
1704 if (xa_is_value(page) || !PageUptodate(page)) {
1705 xas_unlock_irq(&xas);
1706 /* swap in or instantiate fallocated page */
1707 if (shmem_getpage(mapping->host, index, &page,
1708 SGP_NOHUGE)) {
1709 result = SCAN_FAIL;
1710 goto xa_unlocked;
1711 }
1712 } else if (trylock_page(page)) {
1713 get_page(page);
1714 xas_unlock_irq(&xas);
1715 } else {
1716 result = SCAN_PAGE_LOCK;
Hugh Dickins042a3082018-11-30 14:10:39 -08001717 goto xa_locked;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001718 }
Song Liu99cb0db2019-09-23 15:38:00 -07001719 } else { /* !is_shmem */
1720 if (!page || xa_is_value(page)) {
1721 xas_unlock_irq(&xas);
1722 page_cache_sync_readahead(mapping, &file->f_ra,
1723 file, index,
David Howellse5a59d32020-09-04 16:36:16 -07001724 end - index);
Song Liu99cb0db2019-09-23 15:38:00 -07001725 /* drain pagevecs to help isolate_lru_page() */
1726 lru_add_drain();
1727 page = find_lock_page(mapping, index);
1728 if (unlikely(page == NULL)) {
1729 result = SCAN_FAIL;
1730 goto xa_unlocked;
1731 }
Song Liu75f36062019-11-30 17:57:19 -08001732 } else if (PageDirty(page)) {
1733 /*
1734 * khugepaged only works on read-only fd,
1735 * so this page is dirty because it hasn't
1736 * been flushed since first write. There
1737 * won't be new dirty pages.
1738 *
1739 * Trigger async flush here and hope the
1740 * writeback is done when khugepaged
1741 * revisits this page.
1742 *
1743 * This is a one-off situation. We are not
1744 * forcing writeback in loop.
1745 */
1746 xas_unlock_irq(&xas);
1747 filemap_flush(mapping);
1748 result = SCAN_FAIL;
1749 goto xa_unlocked;
Song Liu99cb0db2019-09-23 15:38:00 -07001750 } else if (trylock_page(page)) {
1751 get_page(page);
1752 xas_unlock_irq(&xas);
1753 } else {
1754 result = SCAN_PAGE_LOCK;
1755 goto xa_locked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001756 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001757 }
1758
1759 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001760 * The page must be locked, so we can drop the i_pages lock
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001761 * without racing with truncate.
1762 */
1763 VM_BUG_ON_PAGE(!PageLocked(page), page);
Song Liu4655e5e2019-11-15 17:34:53 -08001764
1765 /* make sure the page is up to date */
1766 if (unlikely(!PageUptodate(page))) {
1767 result = SCAN_FAIL;
1768 goto out_unlock;
1769 }
Hugh Dickins06a5e122018-11-30 14:10:47 -08001770
1771 /*
1772 * If file was truncated then extended, or hole-punched, before
1773 * we locked the first page, then a THP might be there already.
1774 */
1775 if (PageTransCompound(page)) {
1776 result = SCAN_PAGE_COMPOUND;
1777 goto out_unlock;
1778 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001779
1780 if (page_mapping(page) != mapping) {
1781 result = SCAN_TRUNCATED;
1782 goto out_unlock;
1783 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001784
Song Liu4655e5e2019-11-15 17:34:53 -08001785 if (!is_shmem && PageDirty(page)) {
1786 /*
1787 * khugepaged only works on read-only fd, so this
1788 * page is dirty because it hasn't been flushed
1789 * since first write.
1790 */
1791 result = SCAN_FAIL;
1792 goto out_unlock;
1793 }
1794
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001795 if (isolate_lru_page(page)) {
1796 result = SCAN_DEL_PAGE_LRU;
Hugh Dickins042a3082018-11-30 14:10:39 -08001797 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001798 }
1799
Song Liu99cb0db2019-09-23 15:38:00 -07001800 if (page_has_private(page) &&
1801 !try_to_release_page(page, GFP_KERNEL)) {
1802 result = SCAN_PAGE_HAS_PRIVATE;
Hugh Dickins2f33a702020-05-27 22:20:43 -07001803 putback_lru_page(page);
Song Liu99cb0db2019-09-23 15:38:00 -07001804 goto out_unlock;
1805 }
1806
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001807 if (page_mapped(page))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001808 unmap_mapping_pages(mapping, index, 1, false);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001809
Matthew Wilcox77da9382017-12-04 14:56:08 -05001810 xas_lock_irq(&xas);
1811 xas_set(&xas, index);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001812
Matthew Wilcox77da9382017-12-04 14:56:08 -05001813 VM_BUG_ON_PAGE(page != xas_load(&xas), page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001814 VM_BUG_ON_PAGE(page_mapped(page), page);
1815
1816 /*
1817 * The page is expected to have page_count() == 3:
1818 * - we hold a pin on it;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001819 * - one reference from page cache;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001820 * - one from isolate_lru_page;
1821 */
1822 if (!page_ref_freeze(page, 3)) {
1823 result = SCAN_PAGE_COUNT;
Hugh Dickins042a3082018-11-30 14:10:39 -08001824 xas_unlock_irq(&xas);
1825 putback_lru_page(page);
1826 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001827 }
1828
1829 /*
1830 * Add the page to the list to be able to undo the collapse if
1831 * something go wrong.
1832 */
1833 list_add_tail(&page->lru, &pagelist);
1834
1835 /* Finally, replace with the new page. */
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001836 xas_store(&xas, new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001837 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001838out_unlock:
1839 unlock_page(page);
1840 put_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001841 goto xa_unlocked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001842 }
1843
Song Liu99cb0db2019-09-23 15:38:00 -07001844 if (is_shmem)
1845 __inc_node_page_state(new_page, NR_SHMEM_THPS);
Song Liu09d91cd2019-09-23 15:38:03 -07001846 else {
Song Liu99cb0db2019-09-23 15:38:00 -07001847 __inc_node_page_state(new_page, NR_FILE_THPS);
Song Liu09d91cd2019-09-23 15:38:03 -07001848 filemap_nr_thps_inc(mapping);
1849 }
Song Liu99cb0db2019-09-23 15:38:00 -07001850
Hugh Dickins042a3082018-11-30 14:10:39 -08001851 if (nr_none) {
Johannes Weiner9d82c692020-06-03 16:02:04 -07001852 __mod_lruvec_page_state(new_page, NR_FILE_PAGES, nr_none);
Song Liu99cb0db2019-09-23 15:38:00 -07001853 if (is_shmem)
Johannes Weiner9d82c692020-06-03 16:02:04 -07001854 __mod_lruvec_page_state(new_page, NR_SHMEM, nr_none);
Hugh Dickins042a3082018-11-30 14:10:39 -08001855 }
1856
1857xa_locked:
1858 xas_unlock_irq(&xas);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001859xa_unlocked:
Hugh Dickins042a3082018-11-30 14:10:39 -08001860
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001861 if (result == SCAN_SUCCEED) {
Matthew Wilcox77da9382017-12-04 14:56:08 -05001862 struct page *page, *tmp;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001863
1864 /*
Matthew Wilcox77da9382017-12-04 14:56:08 -05001865 * Replacing old pages with new one has succeeded, now we
1866 * need to copy the content and free the old pages.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001867 */
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001868 index = start;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001869 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001870 while (index < page->index) {
1871 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1872 index++;
1873 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001874 copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
1875 page);
1876 list_del(&page->lru);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001877 page->mapping = NULL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001878 page_ref_unfreeze(page, 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001879 ClearPageActive(page);
1880 ClearPageUnevictable(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001881 unlock_page(page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001882 put_page(page);
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001883 index++;
1884 }
1885 while (index < end) {
1886 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1887 index++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001888 }
1889
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001890 SetPageUptodate(new_page);
Hugh Dickins87c460a2018-11-30 14:10:43 -08001891 page_ref_add(new_page, HPAGE_PMD_NR - 1);
Johannes Weiner6058eae2020-06-03 16:02:40 -07001892 if (is_shmem)
Song Liu99cb0db2019-09-23 15:38:00 -07001893 set_page_dirty(new_page);
Johannes Weiner6058eae2020-06-03 16:02:40 -07001894 lru_cache_add(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001895
Hugh Dickins042a3082018-11-30 14:10:39 -08001896 /*
1897 * Remove pte page tables, so we can re-fault the page as huge.
1898 */
1899 retract_page_tables(mapping, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001900 *hpage = NULL;
Yang Shi87aa7522018-08-17 15:45:29 -07001901
1902 khugepaged_pages_collapsed++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001903 } else {
Matthew Wilcox77da9382017-12-04 14:56:08 -05001904 struct page *page;
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001905
Matthew Wilcox77da9382017-12-04 14:56:08 -05001906 /* Something went wrong: roll back page cache changes */
Matthew Wilcox77da9382017-12-04 14:56:08 -05001907 xas_lock_irq(&xas);
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001908 mapping->nrpages -= nr_none;
Song Liu99cb0db2019-09-23 15:38:00 -07001909
1910 if (is_shmem)
1911 shmem_uncharge(mapping->host, nr_none);
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001912
Matthew Wilcox77da9382017-12-04 14:56:08 -05001913 xas_set(&xas, start);
1914 xas_for_each(&xas, page, end - 1) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001915 page = list_first_entry_or_null(&pagelist,
1916 struct page, lru);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001917 if (!page || xas.xa_index < page->index) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001918 if (!nr_none)
1919 break;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001920 nr_none--;
Johannes Weiner59749e62016-12-12 16:43:35 -08001921 /* Put holes back where they were */
Matthew Wilcox77da9382017-12-04 14:56:08 -05001922 xas_store(&xas, NULL);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001923 continue;
1924 }
1925
Matthew Wilcox77da9382017-12-04 14:56:08 -05001926 VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001927
1928 /* Unfreeze the page. */
1929 list_del(&page->lru);
1930 page_ref_unfreeze(page, 2);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001931 xas_store(&xas, page);
1932 xas_pause(&xas);
1933 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001934 unlock_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001935 putback_lru_page(page);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001936 xas_lock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001937 }
1938 VM_BUG_ON(nr_none);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001939 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001940
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001941 new_page->mapping = NULL;
1942 }
Hugh Dickins042a3082018-11-30 14:10:39 -08001943
1944 unlock_page(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001945out:
1946 VM_BUG_ON(!list_empty(&pagelist));
Johannes Weiner9d82c692020-06-03 16:02:04 -07001947 if (!IS_ERR_OR_NULL(*hpage))
1948 mem_cgroup_uncharge(*hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001949 /* TODO: tracepoints */
1950}
1951
Song Liu579c5712019-09-23 15:37:57 -07001952static void khugepaged_scan_file(struct mm_struct *mm,
1953 struct file *file, pgoff_t start, struct page **hpage)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001954{
1955 struct page *page = NULL;
Song Liu579c5712019-09-23 15:37:57 -07001956 struct address_space *mapping = file->f_mapping;
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001957 XA_STATE(xas, &mapping->i_pages, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001958 int present, swap;
1959 int node = NUMA_NO_NODE;
1960 int result = SCAN_SUCCEED;
1961
1962 present = 0;
1963 swap = 0;
1964 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1965 rcu_read_lock();
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001966 xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
1967 if (xas_retry(&xas, page))
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001968 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001969
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001970 if (xa_is_value(page)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001971 if (++swap > khugepaged_max_ptes_swap) {
1972 result = SCAN_EXCEED_SWAP_PTE;
1973 break;
1974 }
1975 continue;
1976 }
1977
1978 if (PageTransCompound(page)) {
1979 result = SCAN_PAGE_COMPOUND;
1980 break;
1981 }
1982
1983 node = page_to_nid(page);
1984 if (khugepaged_scan_abort(node)) {
1985 result = SCAN_SCAN_ABORT;
1986 break;
1987 }
1988 khugepaged_node_load[node]++;
1989
1990 if (!PageLRU(page)) {
1991 result = SCAN_PAGE_LRU;
1992 break;
1993 }
1994
Song Liu99cb0db2019-09-23 15:38:00 -07001995 if (page_count(page) !=
1996 1 + page_mapcount(page) + page_has_private(page)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001997 result = SCAN_PAGE_COUNT;
1998 break;
1999 }
2000
2001 /*
2002 * We probably should check if the page is referenced here, but
2003 * nobody would transfer pte_young() to PageReferenced() for us.
2004 * And rmap walk here is just too costly...
2005 */
2006
2007 present++;
2008
2009 if (need_resched()) {
Matthew Wilcox85b392d2017-12-04 15:06:23 -05002010 xas_pause(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002011 cond_resched_rcu();
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002012 }
2013 }
2014 rcu_read_unlock();
2015
2016 if (result == SCAN_SUCCEED) {
2017 if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
2018 result = SCAN_EXCEED_NONE_PTE;
2019 } else {
2020 node = khugepaged_find_target_node();
Song Liu579c5712019-09-23 15:37:57 -07002021 collapse_file(mm, file, start, hpage, node);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002022 }
2023 }
2024
2025 /* TODO: tracepoints */
2026}
2027#else
Song Liu579c5712019-09-23 15:37:57 -07002028static void khugepaged_scan_file(struct mm_struct *mm,
2029 struct file *file, pgoff_t start, struct page **hpage)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002030{
2031 BUILD_BUG();
2032}
Song Liu27e1f822019-09-23 15:38:30 -07002033
2034static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
2035{
2036 return 0;
2037}
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002038#endif
2039
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002040static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2041 struct page **hpage)
2042 __releases(&khugepaged_mm_lock)
2043 __acquires(&khugepaged_mm_lock)
2044{
2045 struct mm_slot *mm_slot;
2046 struct mm_struct *mm;
2047 struct vm_area_struct *vma;
2048 int progress = 0;
2049
2050 VM_BUG_ON(!pages);
Lance Roy35f3aa32018-10-04 23:45:47 -07002051 lockdep_assert_held(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002052
2053 if (khugepaged_scan.mm_slot)
2054 mm_slot = khugepaged_scan.mm_slot;
2055 else {
2056 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2057 struct mm_slot, mm_node);
2058 khugepaged_scan.address = 0;
2059 khugepaged_scan.mm_slot = mm_slot;
2060 }
2061 spin_unlock(&khugepaged_mm_lock);
Song Liu27e1f822019-09-23 15:38:30 -07002062 khugepaged_collapse_pte_mapped_thps(mm_slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002063
2064 mm = mm_slot->mm;
Yang Shi3b454ad2018-01-31 16:18:28 -08002065 /*
2066 * Don't wait for semaphore (to avoid long wait times). Just move to
2067 * the next mm on the list.
2068 */
2069 vma = NULL;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002070 if (unlikely(!mmap_read_trylock(mm)))
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002071 goto breakouterloop_mmap_lock;
Yang Shi3b454ad2018-01-31 16:18:28 -08002072 if (likely(!khugepaged_test_exit(mm)))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002073 vma = find_vma(mm, khugepaged_scan.address);
2074
2075 progress++;
2076 for (; vma; vma = vma->vm_next) {
2077 unsigned long hstart, hend;
2078
2079 cond_resched();
2080 if (unlikely(khugepaged_test_exit(mm))) {
2081 progress++;
2082 break;
2083 }
Song Liu50f8b922018-08-17 15:47:00 -07002084 if (!hugepage_vma_check(vma, vma->vm_flags)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002085skip:
2086 progress++;
2087 continue;
2088 }
2089 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2090 hend = vma->vm_end & HPAGE_PMD_MASK;
2091 if (hstart >= hend)
2092 goto skip;
2093 if (khugepaged_scan.address > hend)
2094 goto skip;
2095 if (khugepaged_scan.address < hstart)
2096 khugepaged_scan.address = hstart;
2097 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002098 if (shmem_file(vma->vm_file) && !shmem_huge_enabled(vma))
2099 goto skip;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002100
2101 while (khugepaged_scan.address < hend) {
2102 int ret;
2103 cond_resched();
2104 if (unlikely(khugepaged_test_exit(mm)))
2105 goto breakouterloop;
2106
2107 VM_BUG_ON(khugepaged_scan.address < hstart ||
2108 khugepaged_scan.address + HPAGE_PMD_SIZE >
2109 hend);
Song Liu99cb0db2019-09-23 15:38:00 -07002110 if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002111 struct file *file = get_file(vma->vm_file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002112 pgoff_t pgoff = linear_page_index(vma,
2113 khugepaged_scan.address);
Song Liu99cb0db2019-09-23 15:38:00 -07002114
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002115 mmap_read_unlock(mm);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002116 ret = 1;
Song Liu579c5712019-09-23 15:37:57 -07002117 khugepaged_scan_file(mm, file, pgoff, hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002118 fput(file);
2119 } else {
2120 ret = khugepaged_scan_pmd(mm, vma,
2121 khugepaged_scan.address,
2122 hpage);
2123 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002124 /* move to next address */
2125 khugepaged_scan.address += HPAGE_PMD_SIZE;
2126 progress += HPAGE_PMD_NR;
2127 if (ret)
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002128 /* we released mmap_lock so break loop */
2129 goto breakouterloop_mmap_lock;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002130 if (progress >= pages)
2131 goto breakouterloop;
2132 }
2133 }
2134breakouterloop:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002135 mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002136breakouterloop_mmap_lock:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002137
2138 spin_lock(&khugepaged_mm_lock);
2139 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2140 /*
2141 * Release the current mm_slot if this mm is about to die, or
2142 * if we scanned all vmas of this mm.
2143 */
2144 if (khugepaged_test_exit(mm) || !vma) {
2145 /*
2146 * Make sure that if mm_users is reaching zero while
2147 * khugepaged runs here, khugepaged_exit will find
2148 * mm_slot not pointing to the exiting mm.
2149 */
2150 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2151 khugepaged_scan.mm_slot = list_entry(
2152 mm_slot->mm_node.next,
2153 struct mm_slot, mm_node);
2154 khugepaged_scan.address = 0;
2155 } else {
2156 khugepaged_scan.mm_slot = NULL;
2157 khugepaged_full_scans++;
2158 }
2159
2160 collect_mm_slot(mm_slot);
2161 }
2162
2163 return progress;
2164}
2165
2166static int khugepaged_has_work(void)
2167{
2168 return !list_empty(&khugepaged_scan.mm_head) &&
2169 khugepaged_enabled();
2170}
2171
2172static int khugepaged_wait_event(void)
2173{
2174 return !list_empty(&khugepaged_scan.mm_head) ||
2175 kthread_should_stop();
2176}
2177
2178static void khugepaged_do_scan(void)
2179{
2180 struct page *hpage = NULL;
2181 unsigned int progress = 0, pass_through_head = 0;
2182 unsigned int pages = khugepaged_pages_to_scan;
2183 bool wait = true;
2184
2185 barrier(); /* write khugepaged_pages_to_scan to local stack */
2186
Kirill A. Shutemova980df32020-06-03 16:00:12 -07002187 lru_add_drain_all();
2188
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002189 while (progress < pages) {
2190 if (!khugepaged_prealloc_page(&hpage, &wait))
2191 break;
2192
2193 cond_resched();
2194
2195 if (unlikely(kthread_should_stop() || try_to_freeze()))
2196 break;
2197
2198 spin_lock(&khugepaged_mm_lock);
2199 if (!khugepaged_scan.mm_slot)
2200 pass_through_head++;
2201 if (khugepaged_has_work() &&
2202 pass_through_head < 2)
2203 progress += khugepaged_scan_mm_slot(pages - progress,
2204 &hpage);
2205 else
2206 progress = pages;
2207 spin_unlock(&khugepaged_mm_lock);
2208 }
2209
2210 if (!IS_ERR_OR_NULL(hpage))
2211 put_page(hpage);
2212}
2213
2214static bool khugepaged_should_wakeup(void)
2215{
2216 return kthread_should_stop() ||
2217 time_after_eq(jiffies, khugepaged_sleep_expire);
2218}
2219
2220static void khugepaged_wait_work(void)
2221{
2222 if (khugepaged_has_work()) {
2223 const unsigned long scan_sleep_jiffies =
2224 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2225
2226 if (!scan_sleep_jiffies)
2227 return;
2228
2229 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
2230 wait_event_freezable_timeout(khugepaged_wait,
2231 khugepaged_should_wakeup(),
2232 scan_sleep_jiffies);
2233 return;
2234 }
2235
2236 if (khugepaged_enabled())
2237 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2238}
2239
2240static int khugepaged(void *none)
2241{
2242 struct mm_slot *mm_slot;
2243
2244 set_freezable();
2245 set_user_nice(current, MAX_NICE);
2246
2247 while (!kthread_should_stop()) {
2248 khugepaged_do_scan();
2249 khugepaged_wait_work();
2250 }
2251
2252 spin_lock(&khugepaged_mm_lock);
2253 mm_slot = khugepaged_scan.mm_slot;
2254 khugepaged_scan.mm_slot = NULL;
2255 if (mm_slot)
2256 collect_mm_slot(mm_slot);
2257 spin_unlock(&khugepaged_mm_lock);
2258 return 0;
2259}
2260
2261static void set_recommended_min_free_kbytes(void)
2262{
2263 struct zone *zone;
2264 int nr_zones = 0;
2265 unsigned long recommended_min;
2266
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002267 for_each_populated_zone(zone) {
2268 /*
2269 * We don't need to worry about fragmentation of
2270 * ZONE_MOVABLE since it only has movable pages.
2271 */
2272 if (zone_idx(zone) > gfp_zone(GFP_USER))
2273 continue;
2274
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002275 nr_zones++;
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002276 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002277
2278 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
2279 recommended_min = pageblock_nr_pages * nr_zones * 2;
2280
2281 /*
2282 * Make sure that on average at least two pageblocks are almost free
2283 * of another type, one for a migratetype to fall back to and a
2284 * second to avoid subsequent fallbacks of other types There are 3
2285 * MIGRATE_TYPES we care about.
2286 */
2287 recommended_min += pageblock_nr_pages * nr_zones *
2288 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
2289
2290 /* don't ever allow to reserve more than 5% of the lowmem */
2291 recommended_min = min(recommended_min,
2292 (unsigned long) nr_free_buffer_pages() / 20);
2293 recommended_min <<= (PAGE_SHIFT-10);
2294
2295 if (recommended_min > min_free_kbytes) {
2296 if (user_min_free_kbytes >= 0)
2297 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
2298 min_free_kbytes, recommended_min);
2299
2300 min_free_kbytes = recommended_min;
2301 }
2302 setup_per_zone_wmarks();
2303}
2304
2305int start_stop_khugepaged(void)
2306{
2307 static struct task_struct *khugepaged_thread __read_mostly;
2308 static DEFINE_MUTEX(khugepaged_mutex);
2309 int err = 0;
2310
2311 mutex_lock(&khugepaged_mutex);
2312 if (khugepaged_enabled()) {
2313 if (!khugepaged_thread)
2314 khugepaged_thread = kthread_run(khugepaged, NULL,
2315 "khugepaged");
2316 if (IS_ERR(khugepaged_thread)) {
2317 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
2318 err = PTR_ERR(khugepaged_thread);
2319 khugepaged_thread = NULL;
2320 goto fail;
2321 }
2322
2323 if (!list_empty(&khugepaged_scan.mm_head))
2324 wake_up_interruptible(&khugepaged_wait);
2325
2326 set_recommended_min_free_kbytes();
2327 } else if (khugepaged_thread) {
2328 kthread_stop(khugepaged_thread);
2329 khugepaged_thread = NULL;
2330 }
2331fail:
2332 mutex_unlock(&khugepaged_mutex);
2333 return err;
2334}