blob: 4734315f79407bc75db0f995445621f41b1d2f17 [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>
Pasha Tatashin80110bb2022-02-03 20:49:24 -080019#include <linux/page_table_check.h>
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070020#include <linux/swapops.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070021#include <linux/shmem_fs.h>
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070022
23#include <asm/tlb.h>
24#include <asm/pgalloc.h>
25#include "internal.h"
Qi Zhengb26e2702022-08-31 11:19:46 +080026#include "mm_slot.h"
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070027
28enum scan_result {
29 SCAN_FAIL,
30 SCAN_SUCCEED,
31 SCAN_PMD_NULL,
Zach O'Keefe34488392022-09-22 15:40:39 -070032 SCAN_PMD_NONE,
Zach O'Keefe50722802022-07-06 16:59:26 -070033 SCAN_PMD_MAPPED,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070034 SCAN_EXCEED_NONE_PTE,
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -070035 SCAN_EXCEED_SWAP_PTE,
36 SCAN_EXCEED_SHARED_PTE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070037 SCAN_PTE_NON_PRESENT,
Peter Xue1e267c2020-04-06 20:06:04 -070038 SCAN_PTE_UFFD_WP,
Zach O'Keefe58ac9a82022-09-22 15:40:38 -070039 SCAN_PTE_MAPPED_HUGEPAGE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070040 SCAN_PAGE_RO,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -070041 SCAN_LACK_REFERENCED_PAGE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070042 SCAN_PAGE_NULL,
43 SCAN_SCAN_ABORT,
44 SCAN_PAGE_COUNT,
45 SCAN_PAGE_LRU,
46 SCAN_PAGE_LOCK,
47 SCAN_PAGE_ANON,
48 SCAN_PAGE_COMPOUND,
49 SCAN_ANY_PROCESS,
50 SCAN_VMA_NULL,
51 SCAN_VMA_CHECK,
52 SCAN_ADDRESS_RANGE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070053 SCAN_DEL_PAGE_LRU,
54 SCAN_ALLOC_HUGE_PAGE_FAIL,
55 SCAN_CGROUP_CHARGE_FAIL,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070056 SCAN_TRUNCATED,
Song Liu99cb0db2019-09-23 15:38:00 -070057 SCAN_PAGE_HAS_PRIVATE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070058};
59
60#define CREATE_TRACE_POINTS
61#include <trace/events/huge_memory.h>
62
Vijay Balakrishna4aab2be2020-10-10 23:16:40 -070063static struct task_struct *khugepaged_thread __read_mostly;
64static DEFINE_MUTEX(khugepaged_mutex);
65
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070066/* default scan 8*512 pte (or vmas) every 30 second */
67static unsigned int khugepaged_pages_to_scan __read_mostly;
68static unsigned int khugepaged_pages_collapsed;
69static unsigned int khugepaged_full_scans;
70static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
71/* during fragmentation poll the hugepage allocator once every minute */
72static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
73static unsigned long khugepaged_sleep_expire;
74static DEFINE_SPINLOCK(khugepaged_mm_lock);
75static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
76/*
77 * default collapse hugepages if there is at least one pte mapped like
78 * it would have happened if the vma was large enough during page
79 * fault.
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -070080 *
81 * Note that these are only respected if collapse was initiated by khugepaged.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070082 */
83static unsigned int khugepaged_max_ptes_none __read_mostly;
84static unsigned int khugepaged_max_ptes_swap __read_mostly;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -070085static unsigned int khugepaged_max_ptes_shared __read_mostly;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070086
87#define MM_SLOTS_HASH_BITS 10
88static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
89
90static struct kmem_cache *mm_slot_cache __read_mostly;
91
Song Liu27e1f822019-09-23 15:38:30 -070092#define MAX_PTE_MAPPED_THP 8
93
Zach O'Keefe34d6b472022-07-06 16:59:21 -070094struct collapse_control {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -070095 bool is_khugepaged;
96
Zach O'Keefe34d6b472022-07-06 16:59:21 -070097 /* Num pages scanned per node */
98 u32 node_load[MAX_NUMNODES];
99
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700100 /* Last target selected in hpage_collapse_find_target_node() */
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700101 int last_target_node;
102};
103
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700104/**
Qi Zhengb26e2702022-08-31 11:19:46 +0800105 * struct khugepaged_mm_slot - khugepaged information per mm that is being scanned
106 * @slot: hash lookup from mm to mm_slot
Alex Shi336e6b52020-12-14 19:12:01 -0800107 * @nr_pte_mapped_thp: number of pte mapped THP
108 * @pte_mapped_thp: address array corresponding pte mapped THP
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700109 */
Qi Zhengb26e2702022-08-31 11:19:46 +0800110struct khugepaged_mm_slot {
111 struct mm_slot slot;
Song Liu27e1f822019-09-23 15:38:30 -0700112
113 /* pte-mapped THP in this mm */
114 int nr_pte_mapped_thp;
115 unsigned long pte_mapped_thp[MAX_PTE_MAPPED_THP];
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700116};
117
118/**
119 * struct khugepaged_scan - cursor for scanning
120 * @mm_head: the head of the mm list to scan
121 * @mm_slot: the current mm_slot we are scanning
122 * @address: the next address inside that to be scanned
123 *
124 * There is only the one khugepaged_scan instance of this cursor structure.
125 */
126struct khugepaged_scan {
127 struct list_head mm_head;
Qi Zhengb26e2702022-08-31 11:19:46 +0800128 struct khugepaged_mm_slot *mm_slot;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700129 unsigned long address;
130};
131
132static struct khugepaged_scan khugepaged_scan = {
133 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
134};
135
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800136#ifdef CONFIG_SYSFS
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700137static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
138 struct kobj_attribute *attr,
139 char *buf)
140{
Joe Perchesae7a9272020-12-14 19:14:42 -0800141 return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700142}
143
144static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
145 struct kobj_attribute *attr,
146 const char *buf, size_t count)
147{
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800148 unsigned int msecs;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700149 int err;
150
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800151 err = kstrtouint(buf, 10, &msecs);
152 if (err)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700153 return -EINVAL;
154
155 khugepaged_scan_sleep_millisecs = msecs;
156 khugepaged_sleep_expire = 0;
157 wake_up_interruptible(&khugepaged_wait);
158
159 return count;
160}
161static struct kobj_attribute scan_sleep_millisecs_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800162 __ATTR_RW(scan_sleep_millisecs);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700163
164static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
165 struct kobj_attribute *attr,
166 char *buf)
167{
Joe Perchesae7a9272020-12-14 19:14:42 -0800168 return sysfs_emit(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700169}
170
171static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
172 struct kobj_attribute *attr,
173 const char *buf, size_t count)
174{
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800175 unsigned int msecs;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700176 int err;
177
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800178 err = kstrtouint(buf, 10, &msecs);
179 if (err)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700180 return -EINVAL;
181
182 khugepaged_alloc_sleep_millisecs = msecs;
183 khugepaged_sleep_expire = 0;
184 wake_up_interruptible(&khugepaged_wait);
185
186 return count;
187}
188static struct kobj_attribute alloc_sleep_millisecs_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800189 __ATTR_RW(alloc_sleep_millisecs);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700190
191static ssize_t pages_to_scan_show(struct kobject *kobj,
192 struct kobj_attribute *attr,
193 char *buf)
194{
Joe Perchesae7a9272020-12-14 19:14:42 -0800195 return sysfs_emit(buf, "%u\n", khugepaged_pages_to_scan);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700196}
197static ssize_t pages_to_scan_store(struct kobject *kobj,
198 struct kobj_attribute *attr,
199 const char *buf, size_t count)
200{
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800201 unsigned int pages;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700202 int err;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700203
Alexey Dobriyandfefd222020-12-14 19:15:03 -0800204 err = kstrtouint(buf, 10, &pages);
205 if (err || !pages)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700206 return -EINVAL;
207
208 khugepaged_pages_to_scan = pages;
209
210 return count;
211}
212static struct kobj_attribute pages_to_scan_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800213 __ATTR_RW(pages_to_scan);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700214
215static ssize_t pages_collapsed_show(struct kobject *kobj,
216 struct kobj_attribute *attr,
217 char *buf)
218{
Joe Perchesae7a9272020-12-14 19:14:42 -0800219 return sysfs_emit(buf, "%u\n", khugepaged_pages_collapsed);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700220}
221static struct kobj_attribute pages_collapsed_attr =
222 __ATTR_RO(pages_collapsed);
223
224static ssize_t full_scans_show(struct kobject *kobj,
225 struct kobj_attribute *attr,
226 char *buf)
227{
Joe Perchesae7a9272020-12-14 19:14:42 -0800228 return sysfs_emit(buf, "%u\n", khugepaged_full_scans);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700229}
230static struct kobj_attribute full_scans_attr =
231 __ATTR_RO(full_scans);
232
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800233static ssize_t defrag_show(struct kobject *kobj,
234 struct kobj_attribute *attr, char *buf)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700235{
236 return single_hugepage_flag_show(kobj, attr, buf,
Joe Perchesae7a9272020-12-14 19:14:42 -0800237 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700238}
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800239static ssize_t defrag_store(struct kobject *kobj,
240 struct kobj_attribute *attr,
241 const char *buf, size_t count)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700242{
243 return single_hugepage_flag_store(kobj, attr, buf, count,
244 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
245}
246static struct kobj_attribute khugepaged_defrag_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800247 __ATTR_RW(defrag);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700248
249/*
250 * max_ptes_none controls if khugepaged should collapse hugepages over
251 * any unmapped ptes in turn potentially increasing the memory
252 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
253 * reduce the available free memory in the system as it
254 * runs. Increasing max_ptes_none will instead potentially reduce the
255 * free memory in the system during the khugepaged scan.
256 */
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800257static ssize_t max_ptes_none_show(struct kobject *kobj,
258 struct kobj_attribute *attr,
259 char *buf)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700260{
Joe Perchesae7a9272020-12-14 19:14:42 -0800261 return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_none);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700262}
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800263static ssize_t max_ptes_none_store(struct kobject *kobj,
264 struct kobj_attribute *attr,
265 const char *buf, size_t count)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700266{
267 int err;
268 unsigned long max_ptes_none;
269
270 err = kstrtoul(buf, 10, &max_ptes_none);
Miaohe Lin36ee2c72022-06-25 17:28:12 +0800271 if (err || max_ptes_none > HPAGE_PMD_NR - 1)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700272 return -EINVAL;
273
274 khugepaged_max_ptes_none = max_ptes_none;
275
276 return count;
277}
278static struct kobj_attribute khugepaged_max_ptes_none_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800279 __ATTR_RW(max_ptes_none);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700280
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800281static ssize_t max_ptes_swap_show(struct kobject *kobj,
282 struct kobj_attribute *attr,
283 char *buf)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700284{
Joe Perchesae7a9272020-12-14 19:14:42 -0800285 return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_swap);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700286}
287
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800288static ssize_t max_ptes_swap_store(struct kobject *kobj,
289 struct kobj_attribute *attr,
290 const char *buf, size_t count)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700291{
292 int err;
293 unsigned long max_ptes_swap;
294
295 err = kstrtoul(buf, 10, &max_ptes_swap);
Miaohe Lin36ee2c72022-06-25 17:28:12 +0800296 if (err || max_ptes_swap > HPAGE_PMD_NR - 1)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700297 return -EINVAL;
298
299 khugepaged_max_ptes_swap = max_ptes_swap;
300
301 return count;
302}
303
304static struct kobj_attribute khugepaged_max_ptes_swap_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800305 __ATTR_RW(max_ptes_swap);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700306
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800307static ssize_t max_ptes_shared_show(struct kobject *kobj,
308 struct kobj_attribute *attr,
309 char *buf)
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700310{
Joe Perchesae7a9272020-12-14 19:14:42 -0800311 return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_shared);
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700312}
313
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800314static ssize_t max_ptes_shared_store(struct kobject *kobj,
315 struct kobj_attribute *attr,
316 const char *buf, size_t count)
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700317{
318 int err;
319 unsigned long max_ptes_shared;
320
321 err = kstrtoul(buf, 10, &max_ptes_shared);
Miaohe Lin36ee2c72022-06-25 17:28:12 +0800322 if (err || max_ptes_shared > HPAGE_PMD_NR - 1)
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700323 return -EINVAL;
324
325 khugepaged_max_ptes_shared = max_ptes_shared;
326
327 return count;
328}
329
330static struct kobj_attribute khugepaged_max_ptes_shared_attr =
Miaohe Lin6dcdc942022-06-25 17:28:14 +0800331 __ATTR_RW(max_ptes_shared);
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700332
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700333static struct attribute *khugepaged_attr[] = {
334 &khugepaged_defrag_attr.attr,
335 &khugepaged_max_ptes_none_attr.attr,
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700336 &khugepaged_max_ptes_swap_attr.attr,
337 &khugepaged_max_ptes_shared_attr.attr,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700338 &pages_to_scan_attr.attr,
339 &pages_collapsed_attr.attr,
340 &full_scans_attr.attr,
341 &scan_sleep_millisecs_attr.attr,
342 &alloc_sleep_millisecs_attr.attr,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700343 NULL,
344};
345
346struct attribute_group khugepaged_attr_group = {
347 .attrs = khugepaged_attr,
348 .name = "khugepaged",
349};
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800350#endif /* CONFIG_SYSFS */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700351
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700352int hugepage_madvise(struct vm_area_struct *vma,
353 unsigned long *vm_flags, int advice)
354{
355 switch (advice) {
356 case MADV_HUGEPAGE:
357#ifdef CONFIG_S390
358 /*
359 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
360 * can't handle this properly after s390_enable_sie, so we simply
361 * ignore the madvise to prevent qemu from causing a SIGSEGV.
362 */
363 if (mm_has_pgste(vma->vm_mm))
364 return 0;
365#endif
366 *vm_flags &= ~VM_NOHUGEPAGE;
367 *vm_flags |= VM_HUGEPAGE;
368 /*
369 * If the vma become good for khugepaged to scan,
370 * register it here without waiting a page fault that
371 * may not happen any time soon.
372 */
Yang Shic7915762022-05-19 14:08:50 -0700373 khugepaged_enter_vma(vma, *vm_flags);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700374 break;
375 case MADV_NOHUGEPAGE:
376 *vm_flags &= ~VM_HUGEPAGE;
377 *vm_flags |= VM_NOHUGEPAGE;
378 /*
379 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
380 * this vma even if we leave the mm registered in khugepaged if
381 * it got registered before VM_NOHUGEPAGE was set.
382 */
383 break;
384 }
385
386 return 0;
387}
388
389int __init khugepaged_init(void)
390{
391 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
Qi Zhengb26e2702022-08-31 11:19:46 +0800392 sizeof(struct khugepaged_mm_slot),
393 __alignof__(struct khugepaged_mm_slot),
394 0, NULL);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700395 if (!mm_slot_cache)
396 return -ENOMEM;
397
398 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
399 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
400 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700401 khugepaged_max_ptes_shared = HPAGE_PMD_NR / 2;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700402
403 return 0;
404}
405
406void __init khugepaged_destroy(void)
407{
408 kmem_cache_destroy(mm_slot_cache);
409}
410
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700411static inline int hpage_collapse_test_exit(struct mm_struct *mm)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700412{
Jann Horn4d45e752020-10-15 20:13:00 -0700413 return atomic_read(&mm->mm_users) == 0;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700414}
415
Yang Shid2081b22022-05-19 14:08:49 -0700416void __khugepaged_enter(struct mm_struct *mm)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700417{
Qi Zhengb26e2702022-08-31 11:19:46 +0800418 struct khugepaged_mm_slot *mm_slot;
419 struct mm_slot *slot;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700420 int wakeup;
421
Qi Zhengb26e2702022-08-31 11:19:46 +0800422 mm_slot = mm_slot_alloc(mm_slot_cache);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700423 if (!mm_slot)
Yang Shid2081b22022-05-19 14:08:49 -0700424 return;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700425
Qi Zhengb26e2702022-08-31 11:19:46 +0800426 slot = &mm_slot->slot;
427
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700428 /* __khugepaged_exit() must not run from under us */
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700429 VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700430 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
Qi Zhengb26e2702022-08-31 11:19:46 +0800431 mm_slot_free(mm_slot_cache, mm_slot);
Yang Shid2081b22022-05-19 14:08:49 -0700432 return;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700433 }
434
435 spin_lock(&khugepaged_mm_lock);
Qi Zhengb26e2702022-08-31 11:19:46 +0800436 mm_slot_insert(mm_slots_hash, mm, slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700437 /*
438 * Insert just behind the scanning cursor, to let the area settle
439 * down a little.
440 */
441 wakeup = list_empty(&khugepaged_scan.mm_head);
Qi Zhengb26e2702022-08-31 11:19:46 +0800442 list_add_tail(&slot->mm_node, &khugepaged_scan.mm_head);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700443 spin_unlock(&khugepaged_mm_lock);
444
Vegard Nossumf1f10072017-02-27 14:30:07 -0800445 mmgrab(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700446 if (wakeup)
447 wake_up_interruptible(&khugepaged_wait);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700448}
449
Yang Shic7915762022-05-19 14:08:50 -0700450void khugepaged_enter_vma(struct vm_area_struct *vma,
451 unsigned long vm_flags)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700452{
Yang Shi2647d112022-05-19 14:08:49 -0700453 if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) &&
Yang Shi10640262022-06-16 10:48:39 -0700454 hugepage_flags_enabled()) {
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -0700455 if (hugepage_vma_check(vma, vm_flags, false, false, true))
Yang Shi2647d112022-05-19 14:08:49 -0700456 __khugepaged_enter(vma->vm_mm);
457 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700458}
459
460void __khugepaged_exit(struct mm_struct *mm)
461{
Qi Zhengb26e2702022-08-31 11:19:46 +0800462 struct khugepaged_mm_slot *mm_slot;
463 struct mm_slot *slot;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700464 int free = 0;
465
466 spin_lock(&khugepaged_mm_lock);
Qi Zhengb26e2702022-08-31 11:19:46 +0800467 slot = mm_slot_lookup(mm_slots_hash, mm);
468 mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700469 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Qi Zhengb26e2702022-08-31 11:19:46 +0800470 hash_del(&slot->hash);
471 list_del(&slot->mm_node);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700472 free = 1;
473 }
474 spin_unlock(&khugepaged_mm_lock);
475
476 if (free) {
477 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
Qi Zhengb26e2702022-08-31 11:19:46 +0800478 mm_slot_free(mm_slot_cache, mm_slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700479 mmdrop(mm);
480 } else if (mm_slot) {
481 /*
482 * This is required to serialize against
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700483 * hpage_collapse_test_exit() (which is guaranteed to run
484 * under mmap sem read mode). Stop here (after we return all
485 * pagetables will be destroyed) until khugepaged has finished
486 * working on the pagetables under the mmap_lock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700487 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700488 mmap_write_lock(mm);
489 mmap_write_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700490 }
491}
492
493static void release_pte_page(struct page *page)
494{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700495 mod_node_page_state(page_pgdat(page),
496 NR_ISOLATED_ANON + page_is_file_lru(page),
497 -compound_nr(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700498 unlock_page(page);
499 putback_lru_page(page);
500}
501
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700502static void release_pte_pages(pte_t *pte, pte_t *_pte,
503 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700504{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700505 struct page *page, *tmp;
506
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700507 while (--_pte >= pte) {
508 pte_t pteval = *_pte;
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700509
510 page = pte_page(pteval);
511 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)) &&
512 !PageCompound(page))
513 release_pte_page(page);
514 }
515
516 list_for_each_entry_safe(page, tmp, compound_pagelist, lru) {
517 list_del(&page->lru);
518 release_pte_page(page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700519 }
520}
521
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700522static bool is_refcount_suitable(struct page *page)
523{
524 int expected_refcount;
525
526 expected_refcount = total_mapcount(page);
527 if (PageSwapCache(page))
528 expected_refcount += compound_nr(page);
529
530 return page_count(page) == expected_refcount;
531}
532
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700533static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
534 unsigned long address,
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700535 pte_t *pte,
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700536 struct collapse_control *cc,
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700537 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700538{
539 struct page *page = NULL;
540 pte_t *_pte;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700541 int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700542 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700543
Miaohe Lin36ee2c72022-06-25 17:28:12 +0800544 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700545 _pte++, address += PAGE_SIZE) {
546 pte_t pteval = *_pte;
547 if (pte_none(pteval) || (pte_present(pteval) &&
548 is_zero_pfn(pte_pfn(pteval)))) {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700549 ++none_or_zero;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700550 if (!userfaultfd_armed(vma) &&
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700551 (!cc->is_khugepaged ||
552 none_or_zero <= khugepaged_max_ptes_none)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700553 continue;
554 } else {
555 result = SCAN_EXCEED_NONE_PTE;
Yang Yange9ea8742022-01-14 14:07:55 -0800556 count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700557 goto out;
558 }
559 }
560 if (!pte_present(pteval)) {
561 result = SCAN_PTE_NON_PRESENT;
562 goto out;
563 }
564 page = vm_normal_page(vma, address, pteval);
Alex Sierra3218f872022-07-15 10:05:11 -0500565 if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700566 result = SCAN_PAGE_NULL;
567 goto out;
568 }
569
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700570 VM_BUG_ON_PAGE(!PageAnon(page), page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700571
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700572 if (page_mapcount(page) > 1) {
573 ++shared;
574 if (cc->is_khugepaged &&
575 shared > khugepaged_max_ptes_shared) {
576 result = SCAN_EXCEED_SHARED_PTE;
577 count_vm_event(THP_SCAN_EXCEED_SHARED_PTE);
578 goto out;
579 }
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -0700580 }
581
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700582 if (PageCompound(page)) {
583 struct page *p;
584 page = compound_head(page);
585
586 /*
587 * Check if we have dealt with the compound page
588 * already
589 */
590 list_for_each_entry(p, compound_pagelist, lru) {
591 if (page == p)
592 goto next;
593 }
594 }
595
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700596 /*
597 * We can do it before isolate_lru_page because the
598 * page can't be freed from under us. NOTE: PG_lock
599 * is needed to serialize against split_huge_page
600 * when invoked from the VM.
601 */
602 if (!trylock_page(page)) {
603 result = SCAN_PAGE_LOCK;
604 goto out;
605 }
606
607 /*
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700608 * Check if the page has any GUP (or other external) pins.
609 *
610 * The page table that maps the page has been already unlinked
611 * from the page table tree and this process cannot get
Ingo Molnarf0953a12021-05-06 18:06:47 -0700612 * an additional pin on the page.
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700613 *
614 * New pins can come later if the page is shared across fork,
615 * but not from this process. The other process cannot write to
616 * the page, only trigger CoW.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700617 */
Kirill A. Shutemov94456892020-06-03 16:00:20 -0700618 if (!is_refcount_suitable(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700619 unlock_page(page);
620 result = SCAN_PAGE_COUNT;
621 goto out;
622 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700623
624 /*
625 * Isolate the page to avoid collapsing an hugepage
626 * currently in use by the VM.
627 */
628 if (isolate_lru_page(page)) {
629 unlock_page(page);
630 result = SCAN_DEL_PAGE_LRU;
631 goto out;
632 }
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700633 mod_node_page_state(page_pgdat(page),
634 NR_ISOLATED_ANON + page_is_file_lru(page),
635 compound_nr(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700636 VM_BUG_ON_PAGE(!PageLocked(page), page);
637 VM_BUG_ON_PAGE(PageLRU(page), page);
638
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700639 if (PageCompound(page))
640 list_add_tail(&page->lru, compound_pagelist);
641next:
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700642 /*
643 * If collapse was initiated by khugepaged, check that there is
644 * enough young pte to justify collapsing the page
645 */
646 if (cc->is_khugepaged &&
647 (pte_young(pteval) || page_is_young(page) ||
648 PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm,
649 address)))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700650 referenced++;
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700651
652 if (pte_write(pteval))
653 writable = true;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700654 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700655
Miaohe Lin74e579b2021-05-04 18:33:46 -0700656 if (unlikely(!writable)) {
657 result = SCAN_PAGE_RO;
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700658 } else if (unlikely(cc->is_khugepaged && !referenced)) {
Miaohe Lin74e579b2021-05-04 18:33:46 -0700659 result = SCAN_LACK_REFERENCED_PAGE;
660 } else {
661 result = SCAN_SUCCEED;
662 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
663 referenced, writable, result);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700664 return result;
Miaohe Lin74e579b2021-05-04 18:33:46 -0700665 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700666out:
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700667 release_pte_pages(pte, _pte, compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700668 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
669 referenced, writable, result);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700670 return result;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700671}
672
673static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
674 struct vm_area_struct *vma,
675 unsigned long address,
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700676 spinlock_t *ptl,
677 struct list_head *compound_pagelist)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700678{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700679 struct page *src_page, *tmp;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700680 pte_t *_pte;
David Rientjes338a16b2017-05-12 15:47:03 -0700681 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
682 _pte++, page++, address += PAGE_SIZE) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700683 pte_t pteval = *_pte;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700684
685 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
686 clear_user_highpage(page, address);
687 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
688 if (is_zero_pfn(pte_pfn(pteval))) {
689 /*
690 * ptl mostly unnecessary.
691 */
692 spin_lock(ptl);
Pasha Tatashin08d5b292022-01-14 14:06:33 -0800693 ptep_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700694 spin_unlock(ptl);
695 }
696 } else {
697 src_page = pte_page(pteval);
698 copy_user_highpage(page, src_page, address, vma);
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700699 if (!PageCompound(src_page))
700 release_pte_page(src_page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700701 /*
702 * ptl mostly unnecessary, but preempt has to
703 * be disabled to update the per-cpu stats
704 * inside page_remove_rmap().
705 */
706 spin_lock(ptl);
Pasha Tatashin08d5b292022-01-14 14:06:33 -0800707 ptep_clear(vma->vm_mm, address, _pte);
Hugh Dickinscea86fe2022-02-14 18:26:39 -0800708 page_remove_rmap(src_page, vma, false);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700709 spin_unlock(ptl);
710 free_page_and_swap_cache(src_page);
711 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700712 }
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700713
714 list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) {
715 list_del(&src_page->lru);
Miaohe Lin1baec202022-06-25 17:28:16 +0800716 mod_node_page_state(page_pgdat(src_page),
717 NR_ISOLATED_ANON + page_is_file_lru(src_page),
718 -compound_nr(src_page));
719 unlock_page(src_page);
720 free_swap_cache(src_page);
721 putback_lru_page(src_page);
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700722 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700723}
724
725static void khugepaged_alloc_sleep(void)
726{
727 DEFINE_WAIT(wait);
728
729 add_wait_queue(&khugepaged_wait, &wait);
Peter Zijlstraf5d39b02022-08-22 13:18:22 +0200730 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
731 schedule_timeout(msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700732 remove_wait_queue(&khugepaged_wait, &wait);
733}
734
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700735struct collapse_control khugepaged_collapse_control = {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -0700736 .is_khugepaged = true,
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700737 .last_target_node = NUMA_NO_NODE,
738};
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700739
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700740static bool hpage_collapse_scan_abort(int nid, struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700741{
742 int i;
743
744 /*
Mel Gormana5f5f912016-07-28 15:46:32 -0700745 * If node_reclaim_mode is disabled, then no extra effort is made to
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700746 * allocate memory locally.
747 */
Dave Hansen202e35d2021-05-04 18:36:04 -0700748 if (!node_reclaim_enabled())
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700749 return false;
750
751 /* If there is a count for this node already, it must be acceptable */
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700752 if (cc->node_load[nid])
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700753 return false;
754
755 for (i = 0; i < MAX_NUMNODES; i++) {
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700756 if (!cc->node_load[i])
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700757 continue;
Matt Fleminga55c7452019-08-08 20:53:01 +0100758 if (node_distance(nid, i) > node_reclaim_distance)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700759 return true;
760 }
761 return false;
762}
763
Yang Shi10640262022-06-16 10:48:39 -0700764#define khugepaged_defrag() \
765 (transparent_hugepage_flags & \
766 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG))
767
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700768/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
769static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
770{
Vlastimil Babka25160352016-07-28 15:49:25 -0700771 return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700772}
773
774#ifdef CONFIG_NUMA
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700775static int hpage_collapse_find_target_node(struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700776{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700777 int nid, target_node = 0, max_value = 0;
778
779 /* find first node with max normal pages hit */
780 for (nid = 0; nid < MAX_NUMNODES; nid++)
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700781 if (cc->node_load[nid] > max_value) {
782 max_value = cc->node_load[nid];
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700783 target_node = nid;
784 }
785
786 /* do some balance if several nodes have the same hit record */
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700787 if (target_node <= cc->last_target_node)
788 for (nid = cc->last_target_node + 1; nid < MAX_NUMNODES;
789 nid++)
790 if (max_value == cc->node_load[nid]) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700791 target_node = nid;
792 break;
793 }
794
Zach O'Keefe34d6b472022-07-06 16:59:21 -0700795 cc->last_target_node = target_node;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700796 return target_node;
797}
Yang Shic6a7f442022-07-06 16:59:20 -0700798#else
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700799static int hpage_collapse_find_target_node(struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700800{
Yang Shic6a7f442022-07-06 16:59:20 -0700801 return 0;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700802}
Yang Shic6a7f442022-07-06 16:59:20 -0700803#endif
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700804
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700805static bool hpage_collapse_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700806{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700807 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
808 if (unlikely(!*hpage)) {
809 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
Zach O'Keefe9710a78a2022-07-06 16:59:22 -0700810 return false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700811 }
812
813 prep_transhuge_page(*hpage);
814 count_vm_event(THP_COLLAPSE_ALLOC);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700815 return true;
816}
817
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700818/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700819 * If mmap_lock temporarily dropped, revalidate vma
820 * before taking mmap_lock.
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700821 * Returns enum scan_result value.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700822 */
823
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700824static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
Zach O'Keefe34488392022-09-22 15:40:39 -0700825 bool expect_anon,
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -0700826 struct vm_area_struct **vmap,
827 struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700828{
829 struct vm_area_struct *vma;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700830
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700831 if (unlikely(hpage_collapse_test_exit(mm)))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700832 return SCAN_ANY_PROCESS;
833
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700834 *vmap = vma = find_vma(mm, address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700835 if (!vma)
836 return SCAN_VMA_NULL;
837
Yang Shi4fa68932022-06-16 10:48:35 -0700838 if (!transhuge_vma_suitable(vma, address))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700839 return SCAN_ADDRESS_RANGE;
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -0700840 if (!hugepage_vma_check(vma, vma->vm_flags, false, false,
841 cc->is_khugepaged))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700842 return SCAN_VMA_CHECK;
Yang Shif707fa42022-06-16 10:48:36 -0700843 /*
844 * Anon VMA expected, the address may be unmapped then
845 * remapped to file after khugepaged reaquired the mmap_lock.
846 *
847 * hugepage_vma_check may return true for qualified file
848 * vmas.
849 */
Zach O'Keefe34488392022-09-22 15:40:39 -0700850 if (expect_anon && (!(*vmap)->anon_vma || !vma_is_anonymous(*vmap)))
851 return SCAN_PAGE_ANON;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700852 return SCAN_SUCCEED;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700853}
854
Zach O'Keefe50722802022-07-06 16:59:26 -0700855static int find_pmd_or_thp_or_none(struct mm_struct *mm,
856 unsigned long address,
857 pmd_t **pmd)
858{
859 pmd_t pmde;
860
861 *pmd = mm_find_pmd(mm, address);
862 if (!*pmd)
863 return SCAN_PMD_NULL;
864
865 pmde = pmd_read_atomic(*pmd);
866
867#ifdef CONFIG_TRANSPARENT_HUGEPAGE
868 /* See comments in pmd_none_or_trans_huge_or_clear_bad() */
869 barrier();
870#endif
Zach O'Keefe34488392022-09-22 15:40:39 -0700871 if (pmd_none(pmde))
872 return SCAN_PMD_NONE;
Zach O'Keefe50722802022-07-06 16:59:26 -0700873 if (pmd_trans_huge(pmde))
874 return SCAN_PMD_MAPPED;
875 if (pmd_bad(pmde))
876 return SCAN_PMD_NULL;
877 return SCAN_SUCCEED;
878}
879
880static int check_pmd_still_valid(struct mm_struct *mm,
881 unsigned long address,
882 pmd_t *pmd)
883{
884 pmd_t *new_pmd;
885 int result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
886
887 if (result != SCAN_SUCCEED)
888 return result;
889 if (new_pmd != pmd)
890 return SCAN_FAIL;
891 return SCAN_SUCCEED;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700892}
893
894/*
895 * Bring missing pages in from swap, to complete THP collapse.
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700896 * Only done if hpage_collapse_scan_pmd believes it is worthwhile.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700897 *
Miaohe Lin4d928e22022-06-25 17:28:11 +0800898 * Called and returns without pte mapped or spinlocks held.
899 * Note that if false is returned, mmap_lock will be released.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700900 */
901
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700902static int __collapse_huge_page_swapin(struct mm_struct *mm,
903 struct vm_area_struct *vma,
904 unsigned long haddr, pmd_t *pmd,
905 int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700906{
Souptick Joarder2b740302018-08-23 17:01:36 -0700907 int swapped_in = 0;
908 vm_fault_t ret = 0;
Will Deacon2b635dd2021-01-14 15:33:49 +0000909 unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700910
Will Deacon2b635dd2021-01-14 15:33:49 +0000911 for (address = haddr; address < end; address += PAGE_SIZE) {
912 struct vm_fault vmf = {
913 .vma = vma,
914 .address = address,
915 .pgoff = linear_page_index(vma, haddr),
916 .flags = FAULT_FLAG_ALLOW_RETRY,
917 .pmd = pmd,
918 };
919
920 vmf.pte = pte_offset_map(pmd, address);
Jan Kara29943022016-12-14 15:07:16 -0800921 vmf.orig_pte = *vmf.pte;
Will Deacon2b635dd2021-01-14 15:33:49 +0000922 if (!is_swap_pte(vmf.orig_pte)) {
923 pte_unmap(vmf.pte);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700924 continue;
Will Deacon2b635dd2021-01-14 15:33:49 +0000925 }
Jan Kara29943022016-12-14 15:07:16 -0800926 ret = do_swap_page(&vmf);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700927
Miaohe Lin4d928e22022-06-25 17:28:11 +0800928 /*
929 * do_swap_page returns VM_FAULT_RETRY with released mmap_lock.
930 * Note we treat VM_FAULT_RETRY as VM_FAULT_ERROR here because
931 * we do not retry here and swap entry will remain in pagetable
932 * resulting in later failure.
933 */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700934 if (ret & VM_FAULT_RETRY) {
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700935 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700936 /* Likely, but not guaranteed, that page lock failed */
937 return SCAN_PAGE_LOCK;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700938 }
Miaohe Lin4d928e22022-06-25 17:28:11 +0800939 if (ret & VM_FAULT_ERROR) {
940 mmap_read_unlock(mm);
941 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700942 return SCAN_FAIL;
Miaohe Lin4d928e22022-06-25 17:28:11 +0800943 }
944 swapped_in++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700945 }
Kirill A. Shutemovae2c5d82020-06-03 16:00:17 -0700946
947 /* Drain LRU add pagevec to remove extra pin on the swapped in pages */
948 if (swapped_in)
949 lru_add_drain();
950
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700951 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700952 return SCAN_SUCCEED;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700953}
954
Zach O'Keefe9710a78a2022-07-06 16:59:22 -0700955static int alloc_charge_hpage(struct page **hpage, struct mm_struct *mm,
956 struct collapse_control *cc)
957{
958 /* Only allocate from the target node */
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -0700959 gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() :
960 GFP_TRANSHUGE) | __GFP_THISNODE;
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700961 int node = hpage_collapse_find_target_node(cc);
Zach O'Keefe9710a78a2022-07-06 16:59:22 -0700962
Zach O'Keefe7d2c4382022-07-06 16:59:28 -0700963 if (!hpage_collapse_alloc_page(hpage, gfp, node))
Zach O'Keefe9710a78a2022-07-06 16:59:22 -0700964 return SCAN_ALLOC_HUGE_PAGE_FAIL;
965 if (unlikely(mem_cgroup_charge(page_folio(*hpage), mm, gfp)))
966 return SCAN_CGROUP_CHARGE_FAIL;
967 count_memcg_page_event(*hpage, THP_COLLAPSE_ALLOC);
968 return SCAN_SUCCEED;
969}
970
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700971static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
972 int referenced, int unmapped,
973 struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700974{
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -0700975 LIST_HEAD(compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700976 pmd_t *pmd, _pmd;
977 pte_t *pte;
978 pgtable_t pgtable;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700979 struct page *hpage;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700980 spinlock_t *pmd_ptl, *pte_ptl;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700981 int result = SCAN_FAIL;
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700982 struct vm_area_struct *vma;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800983 struct mmu_notifier_range range;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700984
985 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
986
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700987 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700988 * Before allocating the hugepage, release the mmap_lock read lock.
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700989 * The allocation can take potentially a long time if it involves
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700990 * sync compaction, and we do not need to hold the mmap_lock during
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700991 * that. We will recheck the vma after taking it again in write mode.
992 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700993 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700994
Zach O'Keefe50ad2f22022-07-06 16:59:23 -0700995 result = alloc_charge_hpage(&hpage, mm, cc);
Zach O'Keefe9710a78a2022-07-06 16:59:22 -0700996 if (result != SCAN_SUCCEED)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700997 goto out_nolock;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700998
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700999 mmap_read_lock(mm);
Zach O'Keefe34488392022-09-22 15:40:39 -07001000 result = hugepage_vma_revalidate(mm, address, true, &vma, cc);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001001 if (result != SCAN_SUCCEED) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001002 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001003 goto out_nolock;
1004 }
1005
Zach O'Keefe50722802022-07-06 16:59:26 -07001006 result = find_pmd_or_thp_or_none(mm, address, &pmd);
1007 if (result != SCAN_SUCCEED) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001008 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001009 goto out_nolock;
1010 }
1011
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001012 if (unmapped) {
1013 /*
1014 * __collapse_huge_page_swapin will return with mmap_lock
1015 * released when it fails. So we jump out_nolock directly in
1016 * that case. Continuing to collapse causes inconsistency.
1017 */
1018 result = __collapse_huge_page_swapin(mm, vma, address, pmd,
1019 referenced);
1020 if (result != SCAN_SUCCEED)
1021 goto out_nolock;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001022 }
1023
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001024 mmap_read_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001025 /*
1026 * Prevent all access to pagetables with the exception of
1027 * gup_fast later handled by the ptep_clear_flush and the VM
1028 * handled by the anon_vma lock + PG_lock.
1029 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001030 mmap_write_lock(mm);
Zach O'Keefe34488392022-09-22 15:40:39 -07001031 result = hugepage_vma_revalidate(mm, address, true, &vma, cc);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001032 if (result != SCAN_SUCCEED)
Miaohe Lin18d24a72021-05-04 18:34:17 -07001033 goto out_up_write;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001034 /* check if the pmd is still valid */
Zach O'Keefe50722802022-07-06 16:59:26 -07001035 result = check_pmd_still_valid(mm, address, pmd);
1036 if (result != SCAN_SUCCEED)
Miaohe Lin18d24a72021-05-04 18:34:17 -07001037 goto out_up_write;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001038
1039 anon_vma_lock_write(vma->anon_vma);
1040
Jérôme Glisse7269f992019-05-13 17:20:53 -07001041 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001042 address, address + HPAGE_PMD_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001043 mmu_notifier_invalidate_range_start(&range);
Ville Syrjäläec649c9d2019-11-05 21:16:48 -08001044
1045 pte = pte_offset_map(pmd, address);
1046 pte_ptl = pte_lockptr(mm, pmd);
1047
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001048 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1049 /*
Yang Shi70cbc3c2022-09-07 11:01:43 -07001050 * This removes any huge TLB entry from the CPU so we won't allow
1051 * huge and small TLB entries for the same virtual address to
1052 * avoid the risk of CPU bugs in that area.
1053 *
1054 * Parallel fast GUP is fine since fast GUP will back off when
1055 * it detects PMD is changed.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001056 */
1057 _pmd = pmdp_collapse_flush(vma, address, pmd);
1058 spin_unlock(pmd_ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001059 mmu_notifier_invalidate_range_end(&range);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001060
1061 spin_lock(pte_ptl);
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001062 result = __collapse_huge_page_isolate(vma, address, pte, cc,
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001063 &compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001064 spin_unlock(pte_ptl);
1065
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001066 if (unlikely(result != SCAN_SUCCEED)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001067 pte_unmap(pte);
1068 spin_lock(pmd_ptl);
1069 BUG_ON(!pmd_none(*pmd));
1070 /*
1071 * We can only use set_pmd_at when establishing
1072 * hugepmds and never for establishing regular pmds that
1073 * points to regular pagetables. Use pmd_populate for that
1074 */
1075 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1076 spin_unlock(pmd_ptl);
1077 anon_vma_unlock_write(vma->anon_vma);
Miaohe Lin18d24a72021-05-04 18:34:17 -07001078 goto out_up_write;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001079 }
1080
1081 /*
1082 * All pages are isolated and locked so anon_vma rmap
1083 * can't run anymore.
1084 */
1085 anon_vma_unlock_write(vma->anon_vma);
1086
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001087 __collapse_huge_page_copy(pte, hpage, vma, address, pte_ptl,
1088 &compound_pagelist);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001089 pte_unmap(pte);
Miaohe Lin588d01f2021-05-04 18:33:40 -07001090 /*
1091 * spin_lock() below is not the equivalent of smp_wmb(), but
1092 * the smp_wmb() inside __SetPageUptodate() can be reused to
1093 * avoid the copy_huge_page writes to become visible after
1094 * the set_pmd_at() write.
1095 */
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001096 __SetPageUptodate(hpage);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001097 pgtable = pmd_pgtable(_pmd);
1098
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001099 _pmd = mk_huge_pmd(hpage, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001100 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001101
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001102 spin_lock(pmd_ptl);
1103 BUG_ON(!pmd_none(*pmd));
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001104 page_add_new_anon_rmap(hpage, vma, address);
1105 lru_cache_add_inactive_or_unevictable(hpage, vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001106 pgtable_trans_huge_deposit(mm, pmd, pgtable);
1107 set_pmd_at(mm, address, pmd, _pmd);
1108 update_mmu_cache_pmd(vma, address, pmd);
1109 spin_unlock(pmd_ptl);
1110
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001111 hpage = NULL;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001112
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001113 result = SCAN_SUCCEED;
1114out_up_write:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001115 mmap_write_unlock(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001116out_nolock:
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001117 if (hpage) {
1118 mem_cgroup_uncharge(page_folio(hpage));
1119 put_page(hpage);
Yang Shic6a7f442022-07-06 16:59:20 -07001120 }
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001121 trace_mm_collapse_huge_page(mm, result == SCAN_SUCCEED, result);
1122 return result;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001123}
1124
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07001125static int hpage_collapse_scan_pmd(struct mm_struct *mm,
1126 struct vm_area_struct *vma,
1127 unsigned long address, bool *mmap_locked,
1128 struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001129{
1130 pmd_t *pmd;
1131 pte_t *pte, *_pte;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001132 int result = SCAN_FAIL, referenced = 0;
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -07001133 int none_or_zero = 0, shared = 0;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001134 struct page *page = NULL;
1135 unsigned long _address;
1136 spinlock_t *ptl;
1137 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001138 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001139
1140 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1141
Zach O'Keefe50722802022-07-06 16:59:26 -07001142 result = find_pmd_or_thp_or_none(mm, address, &pmd);
1143 if (result != SCAN_SUCCEED)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001144 goto out;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001145
Zach O'Keefe34d6b472022-07-06 16:59:21 -07001146 memset(cc->node_load, 0, sizeof(cc->node_load));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001147 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Miaohe Lin36ee2c72022-06-25 17:28:12 +08001148 for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001149 _pte++, _address += PAGE_SIZE) {
1150 pte_t pteval = *_pte;
1151 if (is_swap_pte(pteval)) {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001152 ++unmapped;
1153 if (!cc->is_khugepaged ||
1154 unmapped <= khugepaged_max_ptes_swap) {
Peter Xue1e267c2020-04-06 20:06:04 -07001155 /*
1156 * Always be strict with uffd-wp
1157 * enabled swap entries. Please see
1158 * comment below for pte_uffd_wp().
1159 */
1160 if (pte_swp_uffd_wp(pteval)) {
1161 result = SCAN_PTE_UFFD_WP;
1162 goto out_unmap;
1163 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001164 continue;
1165 } else {
1166 result = SCAN_EXCEED_SWAP_PTE;
Yang Yange9ea8742022-01-14 14:07:55 -08001167 count_vm_event(THP_SCAN_EXCEED_SWAP_PTE);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001168 goto out_unmap;
1169 }
1170 }
1171 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001172 ++none_or_zero;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001173 if (!userfaultfd_armed(vma) &&
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001174 (!cc->is_khugepaged ||
1175 none_or_zero <= khugepaged_max_ptes_none)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001176 continue;
1177 } else {
1178 result = SCAN_EXCEED_NONE_PTE;
Yang Yange9ea8742022-01-14 14:07:55 -08001179 count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001180 goto out_unmap;
1181 }
1182 }
Peter Xue1e267c2020-04-06 20:06:04 -07001183 if (pte_uffd_wp(pteval)) {
1184 /*
1185 * Don't collapse the page if any of the small
1186 * PTEs are armed with uffd write protection.
1187 * Here we can also mark the new huge pmd as
1188 * write protected if any of the small ones is
Haitao Shi8958b242020-12-15 20:47:26 -08001189 * marked but that could bring unknown
Peter Xue1e267c2020-04-06 20:06:04 -07001190 * userfault messages that falls outside of
1191 * the registered range. So, just be simple.
1192 */
1193 result = SCAN_PTE_UFFD_WP;
1194 goto out_unmap;
1195 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001196 if (pte_write(pteval))
1197 writable = true;
1198
1199 page = vm_normal_page(vma, _address, pteval);
Alex Sierra3218f872022-07-15 10:05:11 -05001200 if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001201 result = SCAN_PAGE_NULL;
1202 goto out_unmap;
1203 }
1204
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001205 if (page_mapcount(page) > 1) {
1206 ++shared;
1207 if (cc->is_khugepaged &&
1208 shared > khugepaged_max_ptes_shared) {
1209 result = SCAN_EXCEED_SHARED_PTE;
1210 count_vm_event(THP_SCAN_EXCEED_SHARED_PTE);
1211 goto out_unmap;
1212 }
Kirill A. Shutemov71a2c112020-06-03 16:00:30 -07001213 }
1214
Kirill A. Shutemov5503fbf2020-06-03 16:00:23 -07001215 page = compound_head(page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001216
1217 /*
1218 * Record which node the original page is from and save this
Zach O'Keefe34d6b472022-07-06 16:59:21 -07001219 * information to cc->node_load[].
Quanfa Fu0b8f0d82022-01-14 14:09:25 -08001220 * Khugepaged will allocate hugepage from the node has the max
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001221 * hit record.
1222 */
1223 node = page_to_nid(page);
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07001224 if (hpage_collapse_scan_abort(node, cc)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001225 result = SCAN_SCAN_ABORT;
1226 goto out_unmap;
1227 }
Zach O'Keefe34d6b472022-07-06 16:59:21 -07001228 cc->node_load[node]++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001229 if (!PageLRU(page)) {
1230 result = SCAN_PAGE_LRU;
1231 goto out_unmap;
1232 }
1233 if (PageLocked(page)) {
1234 result = SCAN_PAGE_LOCK;
1235 goto out_unmap;
1236 }
1237 if (!PageAnon(page)) {
1238 result = SCAN_PAGE_ANON;
1239 goto out_unmap;
1240 }
1241
1242 /*
Kirill A. Shutemov94456892020-06-03 16:00:20 -07001243 * Check if the page has any GUP (or other external) pins.
1244 *
Miaohe Lin36ee2c72022-06-25 17:28:12 +08001245 * Here the check is racy it may see total_mapcount > refcount
Kirill A. Shutemov94456892020-06-03 16:00:20 -07001246 * in some cases.
1247 * For example, one process with one forked child process.
1248 * The parent has the PMD split due to MADV_DONTNEED, then
1249 * the child is trying unmap the whole PMD, but khugepaged
1250 * may be scanning the parent between the child has
1251 * PageDoubleMap flag cleared and dec the mapcount. So
1252 * khugepaged may see total_mapcount > refcount.
1253 *
1254 * But such case is ephemeral we could always retry collapse
1255 * later. However it may report false positive if the page
1256 * has excessive GUP pins (i.e. 512). Anyway the same check
1257 * will be done again later the risk seems low.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001258 */
Kirill A. Shutemov94456892020-06-03 16:00:20 -07001259 if (!is_refcount_suitable(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001260 result = SCAN_PAGE_COUNT;
1261 goto out_unmap;
1262 }
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001263
1264 /*
1265 * If collapse was initiated by khugepaged, check that there is
1266 * enough young pte to justify collapsing the page
1267 */
1268 if (cc->is_khugepaged &&
1269 (pte_young(pteval) || page_is_young(page) ||
1270 PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm,
1271 address)))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001272 referenced++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001273 }
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001274 if (!writable) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001275 result = SCAN_PAGE_RO;
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07001276 } else if (cc->is_khugepaged &&
1277 (!referenced ||
1278 (unmapped && referenced < HPAGE_PMD_NR / 2))) {
Kirill A. Shutemovffe945e2020-06-03 16:00:09 -07001279 result = SCAN_LACK_REFERENCED_PAGE;
1280 } else {
1281 result = SCAN_SUCCEED;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001282 }
1283out_unmap:
1284 pte_unmap_unlock(pte, ptl);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001285 if (result == SCAN_SUCCEED) {
1286 result = collapse_huge_page(mm, address, referenced,
1287 unmapped, cc);
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001288 /* collapse_huge_page will return with the mmap_lock released */
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001289 *mmap_locked = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001290 }
1291out:
1292 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
1293 none_or_zero, result, unmapped);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001294 return result;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001295}
1296
Qi Zhengb26e2702022-08-31 11:19:46 +08001297static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001298{
Qi Zhengb26e2702022-08-31 11:19:46 +08001299 struct mm_slot *slot = &mm_slot->slot;
1300 struct mm_struct *mm = slot->mm;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001301
Lance Roy35f3aa32018-10-04 23:45:47 -07001302 lockdep_assert_held(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001303
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07001304 if (hpage_collapse_test_exit(mm)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001305 /* free mm_slot */
Qi Zhengb26e2702022-08-31 11:19:46 +08001306 hash_del(&slot->hash);
1307 list_del(&slot->mm_node);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001308
1309 /*
1310 * Not strictly needed because the mm exited already.
1311 *
1312 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1313 */
1314
1315 /* khugepaged_mm_lock actually not necessary for the below */
Qi Zhengb26e2702022-08-31 11:19:46 +08001316 mm_slot_free(mm_slot_cache, mm_slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001317 mmdrop(mm);
1318 }
1319}
1320
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001321#ifdef CONFIG_SHMEM
Song Liu27e1f822019-09-23 15:38:30 -07001322/*
1323 * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1324 * khugepaged should try to collapse the page table.
Zach O'Keefe34488392022-09-22 15:40:39 -07001325 *
1326 * Note that following race exists:
1327 * (1) khugepaged calls khugepaged_collapse_pte_mapped_thps() for mm_struct A,
1328 * emptying the A's ->pte_mapped_thp[] array.
1329 * (2) MADV_COLLAPSE collapses some file extent with target mm_struct B, and
1330 * retract_page_tables() finds a VMA in mm_struct A mapping the same extent
1331 * (at virtual address X) and adds an entry (for X) into mm_struct A's
1332 * ->pte-mapped_thp[] array.
1333 * (3) khugepaged calls khugepaged_collapse_scan_file() for mm_struct A at X,
1334 * sees a pte-mapped THP (SCAN_PTE_MAPPED_HUGEPAGE) and adds an entry
1335 * (for X) into mm_struct A's ->pte-mapped_thp[] array.
1336 * Thus, it's possible the same address is added multiple times for the same
1337 * mm_struct. Should this happen, we'll simply attempt
1338 * collapse_pte_mapped_thp() multiple times for the same address, under the same
1339 * exclusive mmap_lock, and assuming the first call is successful, subsequent
1340 * attempts will return quickly (without grabbing any additional locks) when
1341 * a huge pmd is found in find_pmd_or_thp_or_none(). Since this is a cheap
1342 * check, and since this is a rare occurrence, the cost of preventing this
1343 * "multiple-add" is thought to be more expensive than just handling it, should
1344 * it occur.
Song Liu27e1f822019-09-23 15:38:30 -07001345 */
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001346static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
Miaohe Lin081c3252022-06-25 17:28:15 +08001347 unsigned long addr)
Song Liu27e1f822019-09-23 15:38:30 -07001348{
Qi Zhengb26e2702022-08-31 11:19:46 +08001349 struct khugepaged_mm_slot *mm_slot;
1350 struct mm_slot *slot;
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001351 bool ret = false;
Song Liu27e1f822019-09-23 15:38:30 -07001352
1353 VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1354
1355 spin_lock(&khugepaged_mm_lock);
Qi Zhengb26e2702022-08-31 11:19:46 +08001356 slot = mm_slot_lookup(mm_slots_hash, mm);
1357 mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001358 if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP)) {
Song Liu27e1f822019-09-23 15:38:30 -07001359 mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001360 ret = true;
1361 }
Song Liu27e1f822019-09-23 15:38:30 -07001362 spin_unlock(&khugepaged_mm_lock);
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001363 return ret;
Song Liu27e1f822019-09-23 15:38:30 -07001364}
1365
Zach O'Keefe34488392022-09-22 15:40:39 -07001366/* hpage must be locked, and mmap_lock must be held in write */
1367static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
1368 pmd_t *pmdp, struct page *hpage)
1369{
1370 struct vm_fault vmf = {
1371 .vma = vma,
1372 .address = addr,
1373 .flags = 0,
1374 .pmd = pmdp,
1375 };
1376
1377 VM_BUG_ON(!PageTransHuge(hpage));
1378 mmap_assert_write_locked(vma->vm_mm);
1379
1380 if (do_set_pmd(&vmf, hpage))
1381 return SCAN_FAIL;
1382
1383 get_page(hpage);
1384 return SCAN_SUCCEED;
Song Liu27e1f822019-09-23 15:38:30 -07001385}
1386
Pasha Tatashine59a47b82022-02-03 20:49:20 -08001387static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
1388 unsigned long addr, pmd_t *pmdp)
1389{
1390 spinlock_t *ptl;
1391 pmd_t pmd;
1392
Pasha Tatashin80110bb2022-02-03 20:49:24 -08001393 mmap_assert_write_locked(mm);
Pasha Tatashine59a47b82022-02-03 20:49:20 -08001394 ptl = pmd_lock(vma->vm_mm, pmdp);
1395 pmd = pmdp_collapse_flush(vma, addr, pmdp);
1396 spin_unlock(ptl);
1397 mm_dec_nr_ptes(mm);
Pasha Tatashin80110bb2022-02-03 20:49:24 -08001398 page_table_check_pte_clear_range(mm, addr, pmd);
Pasha Tatashine59a47b82022-02-03 20:49:20 -08001399 pte_free(mm, pmd_pgtable(pmd));
1400}
1401
Song Liu27e1f822019-09-23 15:38:30 -07001402/**
Alex Shi336e6b52020-12-14 19:12:01 -08001403 * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
1404 * address haddr.
1405 *
1406 * @mm: process address space where collapse happens
1407 * @addr: THP collapse address
Zach O'Keefe34488392022-09-22 15:40:39 -07001408 * @install_pmd: If a huge PMD should be installed
Song Liu27e1f822019-09-23 15:38:30 -07001409 *
1410 * This function checks whether all the PTEs in the PMD are pointing to the
1411 * right THP. If so, retract the page table so the THP can refault in with
Zach O'Keefe34488392022-09-22 15:40:39 -07001412 * as pmd-mapped. Possibly install a huge PMD mapping the THP.
Song Liu27e1f822019-09-23 15:38:30 -07001413 */
Zach O'Keefe34488392022-09-22 15:40:39 -07001414int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
1415 bool install_pmd)
Song Liu27e1f822019-09-23 15:38:30 -07001416{
1417 unsigned long haddr = addr & HPAGE_PMD_MASK;
Liam R. Howlett94d815b2022-09-06 19:48:50 +00001418 struct vm_area_struct *vma = vma_lookup(mm, haddr);
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001419 struct page *hpage;
Song Liu27e1f822019-09-23 15:38:30 -07001420 pte_t *start_pte, *pte;
Pasha Tatashine59a47b82022-02-03 20:49:20 -08001421 pmd_t *pmd;
Song Liu27e1f822019-09-23 15:38:30 -07001422 spinlock_t *ptl;
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001423 int count = 0, result = SCAN_FAIL;
Song Liu27e1f822019-09-23 15:38:30 -07001424 int i;
1425
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001426 mmap_assert_write_locked(mm);
1427
Zach O'Keefe34488392022-09-22 15:40:39 -07001428 /* Fast check before locking page if already PMD-mapped */
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001429 result = find_pmd_or_thp_or_none(mm, haddr, &pmd);
Zach O'Keefe34488392022-09-22 15:40:39 -07001430 if (result == SCAN_PMD_MAPPED)
1431 return result;
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001432
Song Liu27e1f822019-09-23 15:38:30 -07001433 if (!vma || !vma->vm_file ||
Miaohe Linfef792a2021-05-04 18:34:15 -07001434 !range_in_vma(vma, haddr, haddr + HPAGE_PMD_SIZE))
Zach O'Keefe34488392022-09-22 15:40:39 -07001435 return SCAN_VMA_CHECK;
Song Liu27e1f822019-09-23 15:38:30 -07001436
1437 /*
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -07001438 * If we are here, we've succeeded in replacing all the native pages
1439 * in the page cache with a single hugepage. If a mm were to fault-in
1440 * this memory (mapped by a suitably aligned VMA), we'd get the hugepage
1441 * and map it by a PMD, regardless of sysfs THP settings. As such, let's
1442 * analogously elide sysfs THP settings here.
Song Liu27e1f822019-09-23 15:38:30 -07001443 */
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -07001444 if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false))
Zach O'Keefe34488392022-09-22 15:40:39 -07001445 return SCAN_VMA_CHECK;
Song Liu27e1f822019-09-23 15:38:30 -07001446
Peter Xudeb4c932022-05-12 20:22:55 -07001447 /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
1448 if (userfaultfd_wp(vma))
Zach O'Keefe34488392022-09-22 15:40:39 -07001449 return SCAN_PTE_UFFD_WP;
Peter Xudeb4c932022-05-12 20:22:55 -07001450
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001451 hpage = find_lock_page(vma->vm_file->f_mapping,
1452 linear_page_index(vma, haddr));
1453 if (!hpage)
Zach O'Keefe34488392022-09-22 15:40:39 -07001454 return SCAN_PAGE_NULL;
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001455
Zach O'Keefe34488392022-09-22 15:40:39 -07001456 if (!PageHead(hpage)) {
1457 result = SCAN_FAIL;
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001458 goto drop_hpage;
Zach O'Keefe34488392022-09-22 15:40:39 -07001459 }
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001460
Zach O'Keefe34488392022-09-22 15:40:39 -07001461 if (compound_order(hpage) != HPAGE_PMD_ORDER) {
1462 result = SCAN_PAGE_COMPOUND;
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001463 goto drop_hpage;
Zach O'Keefe34488392022-09-22 15:40:39 -07001464 }
Zach O'Keefe780a4b62022-09-22 15:27:31 -07001465
Zach O'Keefe34488392022-09-22 15:40:39 -07001466 switch (result) {
1467 case SCAN_SUCCEED:
1468 break;
1469 case SCAN_PMD_NONE:
1470 /*
1471 * In MADV_COLLAPSE path, possible race with khugepaged where
1472 * all pte entries have been removed and pmd cleared. If so,
1473 * skip all the pte checks and just update the pmd mapping.
1474 */
1475 goto maybe_install_pmd;
1476 default:
Song Liu27e1f822019-09-23 15:38:30 -07001477 goto drop_hpage;
Zach O'Keefe34488392022-09-22 15:40:39 -07001478 }
Song Liu27e1f822019-09-23 15:38:30 -07001479
1480 start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
Zach O'Keefe34488392022-09-22 15:40:39 -07001481 result = SCAN_FAIL;
Song Liu27e1f822019-09-23 15:38:30 -07001482
1483 /* step 1: check all mapped PTEs are to the right huge page */
1484 for (i = 0, addr = haddr, pte = start_pte;
1485 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1486 struct page *page;
1487
1488 /* empty pte, skip */
1489 if (pte_none(*pte))
1490 continue;
1491
1492 /* page swapped out, abort */
Zach O'Keefe34488392022-09-22 15:40:39 -07001493 if (!pte_present(*pte)) {
1494 result = SCAN_PTE_NON_PRESENT;
Song Liu27e1f822019-09-23 15:38:30 -07001495 goto abort;
Zach O'Keefe34488392022-09-22 15:40:39 -07001496 }
Song Liu27e1f822019-09-23 15:38:30 -07001497
1498 page = vm_normal_page(vma, addr, *pte);
Alex Sierra3218f872022-07-15 10:05:11 -05001499 if (WARN_ON_ONCE(page && is_zone_device_page(page)))
1500 page = NULL;
Song Liu27e1f822019-09-23 15:38:30 -07001501 /*
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001502 * Note that uprobe, debugger, or MAP_PRIVATE may change the
1503 * page table, but the new page will not be a subpage of hpage.
Song Liu27e1f822019-09-23 15:38:30 -07001504 */
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001505 if (hpage + i != page)
Song Liu27e1f822019-09-23 15:38:30 -07001506 goto abort;
1507 count++;
1508 }
1509
1510 /* step 2: adjust rmap */
1511 for (i = 0, addr = haddr, pte = start_pte;
1512 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1513 struct page *page;
1514
1515 if (pte_none(*pte))
1516 continue;
1517 page = vm_normal_page(vma, addr, *pte);
Alex Sierra3218f872022-07-15 10:05:11 -05001518 if (WARN_ON_ONCE(page && is_zone_device_page(page)))
1519 goto abort;
Hugh Dickinscea86fe2022-02-14 18:26:39 -08001520 page_remove_rmap(page, vma, false);
Song Liu27e1f822019-09-23 15:38:30 -07001521 }
1522
1523 pte_unmap_unlock(start_pte, ptl);
1524
1525 /* step 3: set proper refcount and mm_counters. */
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001526 if (count) {
Song Liu27e1f822019-09-23 15:38:30 -07001527 page_ref_sub(hpage, count);
1528 add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1529 }
1530
Zach O'Keefe34488392022-09-22 15:40:39 -07001531 /* step 4: remove pte entries */
Pasha Tatashine59a47b82022-02-03 20:49:20 -08001532 collapse_and_free_pmd(mm, vma, haddr, pmd);
Zach O'Keefe34488392022-09-22 15:40:39 -07001533
1534maybe_install_pmd:
1535 /* step 5: install pmd entry */
1536 result = install_pmd
1537 ? set_huge_pmd(vma, haddr, pmd, hpage)
1538 : SCAN_SUCCEED;
1539
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001540drop_hpage:
1541 unlock_page(hpage);
1542 put_page(hpage);
Zach O'Keefe34488392022-09-22 15:40:39 -07001543 return result;
Song Liu27e1f822019-09-23 15:38:30 -07001544
1545abort:
1546 pte_unmap_unlock(start_pte, ptl);
Hugh Dickins119a5fc2020-08-06 23:26:18 -07001547 goto drop_hpage;
Song Liu27e1f822019-09-23 15:38:30 -07001548}
1549
Qi Zhengb26e2702022-08-31 11:19:46 +08001550static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot)
Song Liu27e1f822019-09-23 15:38:30 -07001551{
Qi Zhengb26e2702022-08-31 11:19:46 +08001552 struct mm_slot *slot = &mm_slot->slot;
1553 struct mm_struct *mm = slot->mm;
Song Liu27e1f822019-09-23 15:38:30 -07001554 int i;
1555
1556 if (likely(mm_slot->nr_pte_mapped_thp == 0))
Miaohe Lin0edf61e2021-05-04 18:33:37 -07001557 return;
Song Liu27e1f822019-09-23 15:38:30 -07001558
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001559 if (!mmap_write_trylock(mm))
Miaohe Lin0edf61e2021-05-04 18:33:37 -07001560 return;
Song Liu27e1f822019-09-23 15:38:30 -07001561
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07001562 if (unlikely(hpage_collapse_test_exit(mm)))
Song Liu27e1f822019-09-23 15:38:30 -07001563 goto out;
1564
1565 for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
Zach O'Keefe34488392022-09-22 15:40:39 -07001566 collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i], false);
Song Liu27e1f822019-09-23 15:38:30 -07001567
1568out:
1569 mm_slot->nr_pte_mapped_thp = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001570 mmap_write_unlock(mm);
Song Liu27e1f822019-09-23 15:38:30 -07001571}
1572
Zach O'Keefe34488392022-09-22 15:40:39 -07001573static int retract_page_tables(struct address_space *mapping, pgoff_t pgoff,
1574 struct mm_struct *target_mm,
1575 unsigned long target_addr, struct page *hpage,
1576 struct collapse_control *cc)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001577{
1578 struct vm_area_struct *vma;
Zach O'Keefe34488392022-09-22 15:40:39 -07001579 int target_result = SCAN_FAIL;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001580
1581 i_mmap_lock_write(mapping);
1582 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Zach O'Keefe34488392022-09-22 15:40:39 -07001583 int result = SCAN_FAIL;
1584 struct mm_struct *mm = NULL;
1585 unsigned long addr = 0;
1586 pmd_t *pmd;
1587 bool is_target = false;
1588
Song Liu27e1f822019-09-23 15:38:30 -07001589 /*
1590 * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1591 * got written to. These VMAs are likely not worth investing
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001592 * mmap_write_lock(mm) as PMD-mapping is likely to be split
Song Liu27e1f822019-09-23 15:38:30 -07001593 * later.
1594 *
Miaohe Lin36ee2c72022-06-25 17:28:12 +08001595 * Note that vma->anon_vma check is racy: it can be set up after
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001596 * the check but before we took mmap_lock by the fault path.
Song Liu27e1f822019-09-23 15:38:30 -07001597 * But page lock would prevent establishing any new ptes of the
1598 * page, so we are safe.
1599 *
1600 * An alternative would be drop the check, but check that page
1601 * table is clear before calling pmdp_collapse_flush() under
1602 * ptl. It has higher chance to recover THP for the VMA, but
1603 * has higher cost too.
1604 */
Zach O'Keefe34488392022-09-22 15:40:39 -07001605 if (vma->anon_vma) {
1606 result = SCAN_PAGE_ANON;
1607 goto next;
1608 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001609 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
Zach O'Keefe34488392022-09-22 15:40:39 -07001610 if (addr & ~HPAGE_PMD_MASK ||
1611 vma->vm_end < addr + HPAGE_PMD_SIZE) {
1612 result = SCAN_VMA_CHECK;
1613 goto next;
1614 }
Hugh Dickins18e77602020-08-06 23:26:22 -07001615 mm = vma->vm_mm;
Zach O'Keefe34488392022-09-22 15:40:39 -07001616 is_target = mm == target_mm && addr == target_addr;
1617 result = find_pmd_or_thp_or_none(mm, addr, &pmd);
1618 if (result != SCAN_SUCCEED)
1619 goto next;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001620 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001621 * We need exclusive mmap_lock to retract page table.
Song Liu27e1f822019-09-23 15:38:30 -07001622 *
1623 * We use trylock due to lock inversion: we need to acquire
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001624 * mmap_lock while holding page lock. Fault path does it in
Song Liu27e1f822019-09-23 15:38:30 -07001625 * reverse order. Trylock is a way to avoid deadlock.
Zach O'Keefe34488392022-09-22 15:40:39 -07001626 *
1627 * Also, it's not MADV_COLLAPSE's job to collapse other
1628 * mappings - let khugepaged take care of them later.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001629 */
Zach O'Keefe34488392022-09-22 15:40:39 -07001630 result = SCAN_PTE_MAPPED_HUGEPAGE;
1631 if ((cc->is_khugepaged || is_target) &&
1632 mmap_write_trylock(mm)) {
Peter Xudeb4c932022-05-12 20:22:55 -07001633 /*
1634 * When a vma is registered with uffd-wp, we can't
1635 * recycle the pmd pgtable because there can be pte
1636 * markers installed. Skip it only, so the rest mm/vma
1637 * can still have the same file mapped hugely, however
1638 * it'll always mapped in small page size for uffd-wp
1639 * registered ranges.
1640 */
Zach O'Keefe34488392022-09-22 15:40:39 -07001641 if (hpage_collapse_test_exit(mm)) {
1642 result = SCAN_ANY_PROCESS;
1643 goto unlock_next;
1644 }
1645 if (userfaultfd_wp(vma)) {
1646 result = SCAN_PTE_UFFD_WP;
1647 goto unlock_next;
1648 }
1649 collapse_and_free_pmd(mm, vma, addr, pmd);
1650 if (!cc->is_khugepaged && is_target)
1651 result = set_huge_pmd(vma, addr, pmd, hpage);
1652 else
1653 result = SCAN_SUCCEED;
1654
1655unlock_next:
Hugh Dickins18e77602020-08-06 23:26:22 -07001656 mmap_write_unlock(mm);
Zach O'Keefe34488392022-09-22 15:40:39 -07001657 goto next;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001658 }
Zach O'Keefe34488392022-09-22 15:40:39 -07001659 /*
1660 * Calling context will handle target mm/addr. Otherwise, let
1661 * khugepaged try again later.
1662 */
1663 if (!is_target) {
1664 khugepaged_add_pte_mapped_thp(mm, addr);
1665 continue;
1666 }
1667next:
1668 if (is_target)
1669 target_result = result;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001670 }
1671 i_mmap_unlock_write(mapping);
Zach O'Keefe34488392022-09-22 15:40:39 -07001672 return target_result;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001673}
1674
1675/**
Song Liu99cb0db2019-09-23 15:38:00 -07001676 * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001677 *
Alex Shi336e6b52020-12-14 19:12:01 -08001678 * @mm: process address space where collapse happens
Zach O'Keefe34488392022-09-22 15:40:39 -07001679 * @addr: virtual collapse start address
Alex Shi336e6b52020-12-14 19:12:01 -08001680 * @file: file that collapse on
1681 * @start: collapse start address
Zach O'Keefe9710a78a2022-07-06 16:59:22 -07001682 * @cc: collapse context and scratchpad
Alex Shi336e6b52020-12-14 19:12:01 -08001683 *
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001684 * Basic scheme is simple, details are more complex:
Hugh Dickins87c460a2018-11-30 14:10:43 -08001685 * - allocate and lock a new huge page;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001686 * - scan page cache replacing old pages with the new one
Song Liu99cb0db2019-09-23 15:38:00 -07001687 * + swap/gup in pages if necessary;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001688 * + fill in gaps;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001689 * + keep old pages around in case rollback is required;
1690 * - if replacing succeeds:
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001691 * + copy data over;
1692 * + free old pages;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001693 * + unlock huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001694 * - if replacing failed;
1695 * + put all pages back and unfreeze them;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001696 * + restore gaps in the page cache;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001697 * + unlock and free huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001698 */
Zach O'Keefe34488392022-09-22 15:40:39 -07001699static int collapse_file(struct mm_struct *mm, unsigned long addr,
1700 struct file *file, pgoff_t start,
1701 struct collapse_control *cc)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001702{
Song Liu579c5712019-09-23 15:37:57 -07001703 struct address_space *mapping = file->f_mapping;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001704 struct page *hpage;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001705 pgoff_t index, end = start + HPAGE_PMD_NR;
1706 LIST_HEAD(pagelist);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001707 XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001708 int nr_none = 0, result = SCAN_SUCCEED;
Song Liu99cb0db2019-09-23 15:38:00 -07001709 bool is_shmem = shmem_file(file);
Muchun Songbf9ecea2021-02-24 12:03:27 -08001710 int nr;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001711
Song Liu99cb0db2019-09-23 15:38:00 -07001712 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001713 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1714
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001715 result = alloc_charge_hpage(&hpage, mm, cc);
Zach O'Keefe9710a78a2022-07-06 16:59:22 -07001716 if (result != SCAN_SUCCEED)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001717 goto out;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001718
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04001719 /*
1720 * Ensure we have slots for all the pages in the range. This is
1721 * almost certainly a no-op because most of the pages must be present
1722 */
Hugh Dickins95feeab2018-11-30 14:10:50 -08001723 do {
1724 xas_lock_irq(&xas);
1725 xas_create_range(&xas);
1726 if (!xas_error(&xas))
1727 break;
1728 xas_unlock_irq(&xas);
1729 if (!xas_nomem(&xas, GFP_KERNEL)) {
Hugh Dickins95feeab2018-11-30 14:10:50 -08001730 result = SCAN_FAIL;
1731 goto out;
1732 }
1733 } while (1);
1734
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001735 __SetPageLocked(hpage);
Song Liu99cb0db2019-09-23 15:38:00 -07001736 if (is_shmem)
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001737 __SetPageSwapBacked(hpage);
1738 hpage->index = start;
1739 hpage->mapping = mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001740
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001741 /*
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001742 * At this point the hpage is locked and not up-to-date.
Hugh Dickins87c460a2018-11-30 14:10:43 -08001743 * It's safe to insert it into the page cache, because nobody would
1744 * be able to map it or use it in another way until we unlock it.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001745 */
1746
Matthew Wilcox77da9382017-12-04 14:56:08 -05001747 xas_set(&xas, start);
1748 for (index = start; index < end; index++) {
1749 struct page *page = xas_next(&xas);
1750
1751 VM_BUG_ON(index != xas.xa_index);
Song Liu99cb0db2019-09-23 15:38:00 -07001752 if (is_shmem) {
1753 if (!page) {
1754 /*
1755 * Stop if extent has been truncated or
1756 * hole-punched, and is now completely
1757 * empty.
1758 */
1759 if (index == start) {
1760 if (!xas_next_entry(&xas, end - 1)) {
1761 result = SCAN_TRUNCATED;
1762 goto xa_locked;
1763 }
1764 xas_set(&xas, index);
1765 }
1766 if (!shmem_charge(mapping->host, 1)) {
1767 result = SCAN_FAIL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001768 goto xa_locked;
Hugh Dickins701270f2018-11-30 14:10:25 -08001769 }
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001770 xas_store(&xas, hpage);
Song Liu99cb0db2019-09-23 15:38:00 -07001771 nr_none++;
1772 continue;
Hugh Dickins701270f2018-11-30 14:10:25 -08001773 }
Song Liu99cb0db2019-09-23 15:38:00 -07001774
1775 if (xa_is_value(page) || !PageUptodate(page)) {
Matthew Wilcox (Oracle)7459c142022-09-02 20:46:27 +01001776 struct folio *folio;
1777
Song Liu99cb0db2019-09-23 15:38:00 -07001778 xas_unlock_irq(&xas);
1779 /* swap in or instantiate fallocated page */
Matthew Wilcox (Oracle)7459c142022-09-02 20:46:27 +01001780 if (shmem_get_folio(mapping->host, index,
1781 &folio, SGP_NOALLOC)) {
Song Liu99cb0db2019-09-23 15:38:00 -07001782 result = SCAN_FAIL;
1783 goto xa_unlocked;
1784 }
Matthew Wilcox (Oracle)7459c142022-09-02 20:46:27 +01001785 page = folio_file_page(folio, index);
Song Liu99cb0db2019-09-23 15:38:00 -07001786 } else if (trylock_page(page)) {
1787 get_page(page);
1788 xas_unlock_irq(&xas);
1789 } else {
1790 result = SCAN_PAGE_LOCK;
Hugh Dickins042a3082018-11-30 14:10:39 -08001791 goto xa_locked;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001792 }
Song Liu99cb0db2019-09-23 15:38:00 -07001793 } else { /* !is_shmem */
1794 if (!page || xa_is_value(page)) {
1795 xas_unlock_irq(&xas);
1796 page_cache_sync_readahead(mapping, &file->f_ra,
1797 file, index,
David Howellse5a59d32020-09-04 16:36:16 -07001798 end - index);
Song Liu99cb0db2019-09-23 15:38:00 -07001799 /* drain pagevecs to help isolate_lru_page() */
1800 lru_add_drain();
1801 page = find_lock_page(mapping, index);
1802 if (unlikely(page == NULL)) {
1803 result = SCAN_FAIL;
1804 goto xa_unlocked;
1805 }
Song Liu75f36062019-11-30 17:57:19 -08001806 } else if (PageDirty(page)) {
1807 /*
1808 * khugepaged only works on read-only fd,
1809 * so this page is dirty because it hasn't
1810 * been flushed since first write. There
1811 * won't be new dirty pages.
1812 *
1813 * Trigger async flush here and hope the
1814 * writeback is done when khugepaged
1815 * revisits this page.
1816 *
1817 * This is a one-off situation. We are not
1818 * forcing writeback in loop.
1819 */
1820 xas_unlock_irq(&xas);
1821 filemap_flush(mapping);
1822 result = SCAN_FAIL;
1823 goto xa_unlocked;
Rongwei Wang74c42e12021-10-28 14:36:27 -07001824 } else if (PageWriteback(page)) {
1825 xas_unlock_irq(&xas);
1826 result = SCAN_FAIL;
1827 goto xa_unlocked;
Song Liu99cb0db2019-09-23 15:38:00 -07001828 } else if (trylock_page(page)) {
1829 get_page(page);
1830 xas_unlock_irq(&xas);
1831 } else {
1832 result = SCAN_PAGE_LOCK;
1833 goto xa_locked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001834 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001835 }
1836
1837 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001838 * The page must be locked, so we can drop the i_pages lock
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001839 * without racing with truncate.
1840 */
1841 VM_BUG_ON_PAGE(!PageLocked(page), page);
Song Liu4655e5e2019-11-15 17:34:53 -08001842
1843 /* make sure the page is up to date */
1844 if (unlikely(!PageUptodate(page))) {
1845 result = SCAN_FAIL;
1846 goto out_unlock;
1847 }
Hugh Dickins06a5e122018-11-30 14:10:47 -08001848
1849 /*
1850 * If file was truncated then extended, or hole-punched, before
1851 * we locked the first page, then a THP might be there already.
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001852 * This will be discovered on the first iteration.
Hugh Dickins06a5e122018-11-30 14:10:47 -08001853 */
1854 if (PageTransCompound(page)) {
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07001855 struct page *head = compound_head(page);
1856
1857 result = compound_order(head) == HPAGE_PMD_ORDER &&
1858 head->index == start
1859 /* Maybe PMD-mapped */
1860 ? SCAN_PTE_MAPPED_HUGEPAGE
1861 : SCAN_PAGE_COMPOUND;
Hugh Dickins06a5e122018-11-30 14:10:47 -08001862 goto out_unlock;
1863 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001864
1865 if (page_mapping(page) != mapping) {
1866 result = SCAN_TRUNCATED;
1867 goto out_unlock;
1868 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001869
Rongwei Wang74c42e12021-10-28 14:36:27 -07001870 if (!is_shmem && (PageDirty(page) ||
1871 PageWriteback(page))) {
Song Liu4655e5e2019-11-15 17:34:53 -08001872 /*
1873 * khugepaged only works on read-only fd, so this
1874 * page is dirty because it hasn't been flushed
1875 * since first write.
1876 */
1877 result = SCAN_FAIL;
1878 goto out_unlock;
1879 }
1880
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001881 if (isolate_lru_page(page)) {
1882 result = SCAN_DEL_PAGE_LRU;
Hugh Dickins042a3082018-11-30 14:10:39 -08001883 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001884 }
1885
Song Liu99cb0db2019-09-23 15:38:00 -07001886 if (page_has_private(page) &&
1887 !try_to_release_page(page, GFP_KERNEL)) {
1888 result = SCAN_PAGE_HAS_PRIVATE;
Hugh Dickins2f33a702020-05-27 22:20:43 -07001889 putback_lru_page(page);
Song Liu99cb0db2019-09-23 15:38:00 -07001890 goto out_unlock;
1891 }
1892
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001893 if (page_mapped(page))
Matthew Wilcox (Oracle)869f7ee2022-02-15 09:28:49 -05001894 try_to_unmap(page_folio(page),
1895 TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001896
Matthew Wilcox77da9382017-12-04 14:56:08 -05001897 xas_lock_irq(&xas);
1898 xas_set(&xas, index);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001899
Matthew Wilcox77da9382017-12-04 14:56:08 -05001900 VM_BUG_ON_PAGE(page != xas_load(&xas), page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001901
1902 /*
1903 * The page is expected to have page_count() == 3:
1904 * - we hold a pin on it;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001905 * - one reference from page cache;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001906 * - one from isolate_lru_page;
1907 */
1908 if (!page_ref_freeze(page, 3)) {
1909 result = SCAN_PAGE_COUNT;
Hugh Dickins042a3082018-11-30 14:10:39 -08001910 xas_unlock_irq(&xas);
1911 putback_lru_page(page);
1912 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001913 }
1914
1915 /*
1916 * Add the page to the list to be able to undo the collapse if
1917 * something go wrong.
1918 */
1919 list_add_tail(&page->lru, &pagelist);
1920
1921 /* Finally, replace with the new page. */
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001922 xas_store(&xas, hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001923 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001924out_unlock:
1925 unlock_page(page);
1926 put_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001927 goto xa_unlocked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001928 }
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001929 nr = thp_nr_pages(hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001930
Song Liu99cb0db2019-09-23 15:38:00 -07001931 if (is_shmem)
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001932 __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr);
Song Liu09d91cd2019-09-23 15:38:03 -07001933 else {
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001934 __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr);
Song Liu09d91cd2019-09-23 15:38:03 -07001935 filemap_nr_thps_inc(mapping);
Collin Fijalkovicheb6ecbe2021-06-30 18:51:32 -07001936 /*
1937 * Paired with smp_mb() in do_dentry_open() to ensure
1938 * i_writecount is up to date and the update to nr_thps is
1939 * visible. Ensures the page cache will be truncated if the
1940 * file is opened writable.
1941 */
1942 smp_mb();
1943 if (inode_is_open_for_write(mapping->host)) {
1944 result = SCAN_FAIL;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001945 __mod_lruvec_page_state(hpage, NR_FILE_THPS, -nr);
Collin Fijalkovicheb6ecbe2021-06-30 18:51:32 -07001946 filemap_nr_thps_dec(mapping);
1947 goto xa_locked;
1948 }
Song Liu09d91cd2019-09-23 15:38:03 -07001949 }
Song Liu99cb0db2019-09-23 15:38:00 -07001950
Hugh Dickins042a3082018-11-30 14:10:39 -08001951 if (nr_none) {
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001952 __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none);
Miaohe Lin2f55f072022-06-25 17:28:13 +08001953 /* nr_none is always 0 for non-shmem. */
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001954 __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none);
Hugh Dickins042a3082018-11-30 14:10:39 -08001955 }
1956
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04001957 /* Join all the small entries into a single multi-index entry */
1958 xas_set_order(&xas, start, HPAGE_PMD_ORDER);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001959 xas_store(&xas, hpage);
Hugh Dickins042a3082018-11-30 14:10:39 -08001960xa_locked:
1961 xas_unlock_irq(&xas);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001962xa_unlocked:
Hugh Dickins042a3082018-11-30 14:10:39 -08001963
Hugh Dickins6d9df8a2022-02-14 18:40:55 -08001964 /*
1965 * If collapse is successful, flush must be done now before copying.
1966 * If collapse is unsuccessful, does flush actually need to be done?
1967 * Do it anyway, to clear the state.
1968 */
1969 try_to_unmap_flush();
1970
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001971 if (result == SCAN_SUCCEED) {
Matthew Wilcox77da9382017-12-04 14:56:08 -05001972 struct page *page, *tmp;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001973
1974 /*
Matthew Wilcox77da9382017-12-04 14:56:08 -05001975 * Replacing old pages with new one has succeeded, now we
1976 * need to copy the content and free the old pages.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001977 */
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001978 index = start;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001979 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001980 while (index < page->index) {
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001981 clear_highpage(hpage + (index % HPAGE_PMD_NR));
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001982 index++;
1983 }
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001984 copy_highpage(hpage + (page->index % HPAGE_PMD_NR),
1985 page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001986 list_del(&page->lru);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001987 page->mapping = NULL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001988 page_ref_unfreeze(page, 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001989 ClearPageActive(page);
1990 ClearPageUnevictable(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001991 unlock_page(page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001992 put_page(page);
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001993 index++;
1994 }
1995 while (index < end) {
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07001996 clear_highpage(hpage + (index % HPAGE_PMD_NR));
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001997 index++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001998 }
1999
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002000 SetPageUptodate(hpage);
2001 page_ref_add(hpage, HPAGE_PMD_NR - 1);
Johannes Weiner6058eae2020-06-03 16:02:40 -07002002 if (is_shmem)
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002003 set_page_dirty(hpage);
2004 lru_cache_add(hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002005
Hugh Dickins042a3082018-11-30 14:10:39 -08002006 /*
2007 * Remove pte page tables, so we can re-fault the page as huge.
2008 */
Zach O'Keefe34488392022-09-22 15:40:39 -07002009 result = retract_page_tables(mapping, start, mm, addr, hpage,
2010 cc);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002011 unlock_page(hpage);
2012 hpage = NULL;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002013 } else {
Matthew Wilcox77da9382017-12-04 14:56:08 -05002014 struct page *page;
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08002015
Matthew Wilcox77da9382017-12-04 14:56:08 -05002016 /* Something went wrong: roll back page cache changes */
Matthew Wilcox77da9382017-12-04 14:56:08 -05002017 xas_lock_irq(&xas);
Miaohe Lin2f55f072022-06-25 17:28:13 +08002018 if (nr_none) {
2019 mapping->nrpages -= nr_none;
Song Liu99cb0db2019-09-23 15:38:00 -07002020 shmem_uncharge(mapping->host, nr_none);
Miaohe Lin2f55f072022-06-25 17:28:13 +08002021 }
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08002022
Matthew Wilcox77da9382017-12-04 14:56:08 -05002023 xas_set(&xas, start);
2024 xas_for_each(&xas, page, end - 1) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002025 page = list_first_entry_or_null(&pagelist,
2026 struct page, lru);
Matthew Wilcox77da9382017-12-04 14:56:08 -05002027 if (!page || xas.xa_index < page->index) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002028 if (!nr_none)
2029 break;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002030 nr_none--;
Johannes Weiner59749e62016-12-12 16:43:35 -08002031 /* Put holes back where they were */
Matthew Wilcox77da9382017-12-04 14:56:08 -05002032 xas_store(&xas, NULL);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002033 continue;
2034 }
2035
Matthew Wilcox77da9382017-12-04 14:56:08 -05002036 VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002037
2038 /* Unfreeze the page. */
2039 list_del(&page->lru);
2040 page_ref_unfreeze(page, 2);
Matthew Wilcox77da9382017-12-04 14:56:08 -05002041 xas_store(&xas, page);
2042 xas_pause(&xas);
2043 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002044 unlock_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08002045 putback_lru_page(page);
Matthew Wilcox77da9382017-12-04 14:56:08 -05002046 xas_lock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002047 }
2048 VM_BUG_ON(nr_none);
Matthew Wilcox77da9382017-12-04 14:56:08 -05002049 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002050
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002051 hpage->mapping = NULL;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002052 }
Hugh Dickins042a3082018-11-30 14:10:39 -08002053
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002054 if (hpage)
2055 unlock_page(hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002056out:
2057 VM_BUG_ON(!list_empty(&pagelist));
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002058 if (hpage) {
2059 mem_cgroup_uncharge(page_folio(hpage));
2060 put_page(hpage);
Yang Shic6a7f442022-07-06 16:59:20 -07002061 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002062 /* TODO: tracepoints */
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002063 return result;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002064}
2065
Zach O'Keefe34488392022-09-22 15:40:39 -07002066static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2067 struct file *file, pgoff_t start,
2068 struct collapse_control *cc)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002069{
2070 struct page *page = NULL;
Song Liu579c5712019-09-23 15:37:57 -07002071 struct address_space *mapping = file->f_mapping;
Matthew Wilcox85b392d2017-12-04 15:06:23 -05002072 XA_STATE(xas, &mapping->i_pages, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002073 int present, swap;
2074 int node = NUMA_NO_NODE;
2075 int result = SCAN_SUCCEED;
2076
2077 present = 0;
2078 swap = 0;
Zach O'Keefe34d6b472022-07-06 16:59:21 -07002079 memset(cc->node_load, 0, sizeof(cc->node_load));
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002080 rcu_read_lock();
Matthew Wilcox85b392d2017-12-04 15:06:23 -05002081 xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
2082 if (xas_retry(&xas, page))
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002083 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002084
Matthew Wilcox85b392d2017-12-04 15:06:23 -05002085 if (xa_is_value(page)) {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07002086 ++swap;
2087 if (cc->is_khugepaged &&
2088 swap > khugepaged_max_ptes_swap) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002089 result = SCAN_EXCEED_SWAP_PTE;
Yang Yange9ea8742022-01-14 14:07:55 -08002090 count_vm_event(THP_SCAN_EXCEED_SWAP_PTE);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002091 break;
2092 }
2093 continue;
2094 }
2095
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002096 /*
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07002097 * TODO: khugepaged should compact smaller compound pages
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002098 * into a PMD sized page
2099 */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002100 if (PageTransCompound(page)) {
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07002101 struct page *head = compound_head(page);
2102
2103 result = compound_order(head) == HPAGE_PMD_ORDER &&
2104 head->index == start
2105 /* Maybe PMD-mapped */
2106 ? SCAN_PTE_MAPPED_HUGEPAGE
2107 : SCAN_PAGE_COMPOUND;
2108 /*
2109 * For SCAN_PTE_MAPPED_HUGEPAGE, further processing
2110 * by the caller won't touch the page cache, and so
2111 * it's safe to skip LRU and refcount checks before
2112 * returning.
2113 */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002114 break;
2115 }
2116
2117 node = page_to_nid(page);
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07002118 if (hpage_collapse_scan_abort(node, cc)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002119 result = SCAN_SCAN_ABORT;
2120 break;
2121 }
Zach O'Keefe34d6b472022-07-06 16:59:21 -07002122 cc->node_load[node]++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002123
2124 if (!PageLRU(page)) {
2125 result = SCAN_PAGE_LRU;
2126 break;
2127 }
2128
Song Liu99cb0db2019-09-23 15:38:00 -07002129 if (page_count(page) !=
2130 1 + page_mapcount(page) + page_has_private(page)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002131 result = SCAN_PAGE_COUNT;
2132 break;
2133 }
2134
2135 /*
2136 * We probably should check if the page is referenced here, but
2137 * nobody would transfer pte_young() to PageReferenced() for us.
2138 * And rmap walk here is just too costly...
2139 */
2140
2141 present++;
2142
2143 if (need_resched()) {
Matthew Wilcox85b392d2017-12-04 15:06:23 -05002144 xas_pause(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002145 cond_resched_rcu();
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002146 }
2147 }
2148 rcu_read_unlock();
2149
2150 if (result == SCAN_SUCCEED) {
Zach O'Keefed8ea7cc2022-07-06 16:59:24 -07002151 if (cc->is_khugepaged &&
2152 present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002153 result = SCAN_EXCEED_NONE_PTE;
Yang Yange9ea8742022-01-14 14:07:55 -08002154 count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002155 } else {
Zach O'Keefe34488392022-09-22 15:40:39 -07002156 result = collapse_file(mm, addr, file, start, cc);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002157 }
2158 }
2159
Zach O'Keefed41fd202022-09-22 15:40:40 -07002160 trace_mm_khugepaged_scan_file(mm, page, file->f_path.dentry->d_iname,
2161 present, swap, result);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002162 return result;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002163}
2164#else
Zach O'Keefe34488392022-09-22 15:40:39 -07002165static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2166 struct file *file, pgoff_t start,
2167 struct collapse_control *cc)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002168{
2169 BUILD_BUG();
2170}
Song Liu27e1f822019-09-23 15:38:30 -07002171
Qi Zhengb26e2702022-08-31 11:19:46 +08002172static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot)
Song Liu27e1f822019-09-23 15:38:30 -07002173{
Song Liu27e1f822019-09-23 15:38:30 -07002174}
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07002175
2176static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
2177 unsigned long addr)
2178{
2179 return false;
2180}
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002181#endif
2182
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002183static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
Zach O'Keefe34d6b472022-07-06 16:59:21 -07002184 struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002185 __releases(&khugepaged_mm_lock)
2186 __acquires(&khugepaged_mm_lock)
2187{
Matthew Wilcox (Oracle)68540502022-09-06 19:49:00 +00002188 struct vma_iterator vmi;
Qi Zhengb26e2702022-08-31 11:19:46 +08002189 struct khugepaged_mm_slot *mm_slot;
2190 struct mm_slot *slot;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002191 struct mm_struct *mm;
2192 struct vm_area_struct *vma;
2193 int progress = 0;
2194
2195 VM_BUG_ON(!pages);
Lance Roy35f3aa32018-10-04 23:45:47 -07002196 lockdep_assert_held(&khugepaged_mm_lock);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002197 *result = SCAN_FAIL;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002198
Qi Zhengb26e2702022-08-31 11:19:46 +08002199 if (khugepaged_scan.mm_slot) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002200 mm_slot = khugepaged_scan.mm_slot;
Qi Zhengb26e2702022-08-31 11:19:46 +08002201 slot = &mm_slot->slot;
2202 } else {
2203 slot = list_entry(khugepaged_scan.mm_head.next,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002204 struct mm_slot, mm_node);
Qi Zhengb26e2702022-08-31 11:19:46 +08002205 mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002206 khugepaged_scan.address = 0;
2207 khugepaged_scan.mm_slot = mm_slot;
2208 }
2209 spin_unlock(&khugepaged_mm_lock);
Song Liu27e1f822019-09-23 15:38:30 -07002210 khugepaged_collapse_pte_mapped_thps(mm_slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002211
Qi Zhengb26e2702022-08-31 11:19:46 +08002212 mm = slot->mm;
Yang Shi3b454ad2018-01-31 16:18:28 -08002213 /*
2214 * Don't wait for semaphore (to avoid long wait times). Just move to
2215 * the next mm on the list.
2216 */
2217 vma = NULL;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002218 if (unlikely(!mmap_read_trylock(mm)))
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002219 goto breakouterloop_mmap_lock;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002220
2221 progress++;
Matthew Wilcox (Oracle)68540502022-09-06 19:49:00 +00002222 if (unlikely(hpage_collapse_test_exit(mm)))
2223 goto breakouterloop;
2224
2225 vma_iter_init(&vmi, mm, khugepaged_scan.address);
2226 for_each_vma(vmi, vma) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002227 unsigned long hstart, hend;
2228
2229 cond_resched();
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07002230 if (unlikely(hpage_collapse_test_exit(mm))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002231 progress++;
2232 break;
2233 }
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -07002234 if (!hugepage_vma_check(vma, vma->vm_flags, false, false, true)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002235skip:
2236 progress++;
2237 continue;
2238 }
Yang Shi4fa68932022-06-16 10:48:35 -07002239 hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
2240 hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002241 if (khugepaged_scan.address > hend)
2242 goto skip;
2243 if (khugepaged_scan.address < hstart)
2244 khugepaged_scan.address = hstart;
2245 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2246
2247 while (khugepaged_scan.address < hend) {
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002248 bool mmap_locked = true;
2249
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002250 cond_resched();
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07002251 if (unlikely(hpage_collapse_test_exit(mm)))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002252 goto breakouterloop;
2253
2254 VM_BUG_ON(khugepaged_scan.address < hstart ||
2255 khugepaged_scan.address + HPAGE_PMD_SIZE >
2256 hend);
Song Liu99cb0db2019-09-23 15:38:00 -07002257 if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002258 struct file *file = get_file(vma->vm_file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002259 pgoff_t pgoff = linear_page_index(vma,
2260 khugepaged_scan.address);
Song Liu99cb0db2019-09-23 15:38:00 -07002261
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002262 mmap_read_unlock(mm);
Zach O'Keefe34488392022-09-22 15:40:39 -07002263 *result = hpage_collapse_scan_file(mm,
2264 khugepaged_scan.address,
2265 file, pgoff, cc);
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002266 mmap_locked = false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002267 fput(file);
2268 } else {
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07002269 *result = hpage_collapse_scan_pmd(mm, vma,
2270 khugepaged_scan.address,
2271 &mmap_locked,
2272 cc);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002273 }
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07002274 switch (*result) {
2275 case SCAN_PTE_MAPPED_HUGEPAGE: {
2276 pmd_t *pmd;
2277
2278 *result = find_pmd_or_thp_or_none(mm,
2279 khugepaged_scan.address,
2280 &pmd);
2281 if (*result != SCAN_SUCCEED)
2282 break;
2283 if (!khugepaged_add_pte_mapped_thp(mm,
2284 khugepaged_scan.address))
2285 break;
2286 } fallthrough;
2287 case SCAN_SUCCEED:
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002288 ++khugepaged_pages_collapsed;
Zach O'Keefe58ac9a82022-09-22 15:40:38 -07002289 break;
2290 default:
2291 break;
2292 }
2293
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002294 /* move to next address */
2295 khugepaged_scan.address += HPAGE_PMD_SIZE;
2296 progress += HPAGE_PMD_NR;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002297 if (!mmap_locked)
2298 /*
2299 * We released mmap_lock so break loop. Note
2300 * that we drop mmap_lock before all hugepage
2301 * allocations, so if allocation fails, we are
2302 * guaranteed to break here and report the
2303 * correct result back to caller.
2304 */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002305 goto breakouterloop_mmap_lock;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002306 if (progress >= pages)
2307 goto breakouterloop;
2308 }
2309 }
2310breakouterloop:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002311 mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002312breakouterloop_mmap_lock:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002313
2314 spin_lock(&khugepaged_mm_lock);
2315 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2316 /*
2317 * Release the current mm_slot if this mm is about to die, or
2318 * if we scanned all vmas of this mm.
2319 */
Zach O'Keefe7d2c4382022-07-06 16:59:28 -07002320 if (hpage_collapse_test_exit(mm) || !vma) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002321 /*
2322 * Make sure that if mm_users is reaching zero while
2323 * khugepaged runs here, khugepaged_exit will find
2324 * mm_slot not pointing to the exiting mm.
2325 */
Qi Zhengb26e2702022-08-31 11:19:46 +08002326 if (slot->mm_node.next != &khugepaged_scan.mm_head) {
2327 slot = list_entry(slot->mm_node.next,
2328 struct mm_slot, mm_node);
2329 khugepaged_scan.mm_slot =
2330 mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002331 khugepaged_scan.address = 0;
2332 } else {
2333 khugepaged_scan.mm_slot = NULL;
2334 khugepaged_full_scans++;
2335 }
2336
2337 collect_mm_slot(mm_slot);
2338 }
2339
2340 return progress;
2341}
2342
2343static int khugepaged_has_work(void)
2344{
2345 return !list_empty(&khugepaged_scan.mm_head) &&
Yang Shi10640262022-06-16 10:48:39 -07002346 hugepage_flags_enabled();
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002347}
2348
2349static int khugepaged_wait_event(void)
2350{
2351 return !list_empty(&khugepaged_scan.mm_head) ||
2352 kthread_should_stop();
2353}
2354
Zach O'Keefe34d6b472022-07-06 16:59:21 -07002355static void khugepaged_do_scan(struct collapse_control *cc)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002356{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002357 unsigned int progress = 0, pass_through_head = 0;
Yanfei Xu89dc6a92021-05-04 18:34:12 -07002358 unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002359 bool wait = true;
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002360 int result = SCAN_SUCCEED;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002361
Kirill A. Shutemova980df32020-06-03 16:00:12 -07002362 lru_add_drain_all();
2363
Yang Shic6a7f442022-07-06 16:59:20 -07002364 while (true) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002365 cond_resched();
2366
2367 if (unlikely(kthread_should_stop() || try_to_freeze()))
2368 break;
2369
2370 spin_lock(&khugepaged_mm_lock);
2371 if (!khugepaged_scan.mm_slot)
2372 pass_through_head++;
2373 if (khugepaged_has_work() &&
2374 pass_through_head < 2)
2375 progress += khugepaged_scan_mm_slot(pages - progress,
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002376 &result, cc);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002377 else
2378 progress = pages;
2379 spin_unlock(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002380
Yang Shic6a7f442022-07-06 16:59:20 -07002381 if (progress >= pages)
2382 break;
2383
Zach O'Keefe50ad2f22022-07-06 16:59:23 -07002384 if (result == SCAN_ALLOC_HUGE_PAGE_FAIL) {
Yang Shic6a7f442022-07-06 16:59:20 -07002385 /*
2386 * If fail to allocate the first time, try to sleep for
2387 * a while. When hit again, cancel the scan.
2388 */
2389 if (!wait)
2390 break;
2391 wait = false;
Yang Shic6a7f442022-07-06 16:59:20 -07002392 khugepaged_alloc_sleep();
2393 }
2394 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002395}
2396
2397static bool khugepaged_should_wakeup(void)
2398{
2399 return kthread_should_stop() ||
2400 time_after_eq(jiffies, khugepaged_sleep_expire);
2401}
2402
2403static void khugepaged_wait_work(void)
2404{
2405 if (khugepaged_has_work()) {
2406 const unsigned long scan_sleep_jiffies =
2407 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2408
2409 if (!scan_sleep_jiffies)
2410 return;
2411
2412 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
2413 wait_event_freezable_timeout(khugepaged_wait,
2414 khugepaged_should_wakeup(),
2415 scan_sleep_jiffies);
2416 return;
2417 }
2418
Yang Shi10640262022-06-16 10:48:39 -07002419 if (hugepage_flags_enabled())
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002420 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2421}
2422
2423static int khugepaged(void *none)
2424{
Qi Zhengb26e2702022-08-31 11:19:46 +08002425 struct khugepaged_mm_slot *mm_slot;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002426
2427 set_freezable();
2428 set_user_nice(current, MAX_NICE);
2429
2430 while (!kthread_should_stop()) {
Zach O'Keefe34d6b472022-07-06 16:59:21 -07002431 khugepaged_do_scan(&khugepaged_collapse_control);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002432 khugepaged_wait_work();
2433 }
2434
2435 spin_lock(&khugepaged_mm_lock);
2436 mm_slot = khugepaged_scan.mm_slot;
2437 khugepaged_scan.mm_slot = NULL;
2438 if (mm_slot)
2439 collect_mm_slot(mm_slot);
2440 spin_unlock(&khugepaged_mm_lock);
2441 return 0;
2442}
2443
2444static void set_recommended_min_free_kbytes(void)
2445{
2446 struct zone *zone;
2447 int nr_zones = 0;
2448 unsigned long recommended_min;
2449
Yang Shi10640262022-06-16 10:48:39 -07002450 if (!hugepage_flags_enabled()) {
Liangcai Fanbd3400e2021-11-05 13:41:36 -07002451 calculate_min_free_kbytes();
2452 goto update_wmarks;
2453 }
2454
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002455 for_each_populated_zone(zone) {
2456 /*
2457 * We don't need to worry about fragmentation of
2458 * ZONE_MOVABLE since it only has movable pages.
2459 */
2460 if (zone_idx(zone) > gfp_zone(GFP_USER))
2461 continue;
2462
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002463 nr_zones++;
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002464 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002465
2466 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
2467 recommended_min = pageblock_nr_pages * nr_zones * 2;
2468
2469 /*
2470 * Make sure that on average at least two pageblocks are almost free
2471 * of another type, one for a migratetype to fall back to and a
2472 * second to avoid subsequent fallbacks of other types There are 3
2473 * MIGRATE_TYPES we care about.
2474 */
2475 recommended_min += pageblock_nr_pages * nr_zones *
2476 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
2477
2478 /* don't ever allow to reserve more than 5% of the lowmem */
2479 recommended_min = min(recommended_min,
2480 (unsigned long) nr_free_buffer_pages() / 20);
2481 recommended_min <<= (PAGE_SHIFT-10);
2482
2483 if (recommended_min > min_free_kbytes) {
2484 if (user_min_free_kbytes >= 0)
2485 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
2486 min_free_kbytes, recommended_min);
2487
2488 min_free_kbytes = recommended_min;
2489 }
Liangcai Fanbd3400e2021-11-05 13:41:36 -07002490
2491update_wmarks:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002492 setup_per_zone_wmarks();
2493}
2494
2495int start_stop_khugepaged(void)
2496{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002497 int err = 0;
2498
2499 mutex_lock(&khugepaged_mutex);
Yang Shi10640262022-06-16 10:48:39 -07002500 if (hugepage_flags_enabled()) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002501 if (!khugepaged_thread)
2502 khugepaged_thread = kthread_run(khugepaged, NULL,
2503 "khugepaged");
2504 if (IS_ERR(khugepaged_thread)) {
2505 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
2506 err = PTR_ERR(khugepaged_thread);
2507 khugepaged_thread = NULL;
2508 goto fail;
2509 }
2510
2511 if (!list_empty(&khugepaged_scan.mm_head))
2512 wake_up_interruptible(&khugepaged_wait);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002513 } else if (khugepaged_thread) {
2514 kthread_stop(khugepaged_thread);
2515 khugepaged_thread = NULL;
2516 }
Liangcai Fanbd3400e2021-11-05 13:41:36 -07002517 set_recommended_min_free_kbytes();
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002518fail:
2519 mutex_unlock(&khugepaged_mutex);
2520 return err;
2521}
Vijay Balakrishna4aab2be2020-10-10 23:16:40 -07002522
2523void khugepaged_min_free_kbytes_update(void)
2524{
2525 mutex_lock(&khugepaged_mutex);
Yang Shi10640262022-06-16 10:48:39 -07002526 if (hugepage_flags_enabled() && khugepaged_thread)
Vijay Balakrishna4aab2be2020-10-10 23:16:40 -07002527 set_recommended_min_free_kbytes();
2528 mutex_unlock(&khugepaged_mutex);
2529}
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002530
2531static int madvise_collapse_errno(enum scan_result r)
2532{
2533 /*
2534 * MADV_COLLAPSE breaks from existing madvise(2) conventions to provide
2535 * actionable feedback to caller, so they may take an appropriate
2536 * fallback measure depending on the nature of the failure.
2537 */
2538 switch (r) {
2539 case SCAN_ALLOC_HUGE_PAGE_FAIL:
2540 return -ENOMEM;
2541 case SCAN_CGROUP_CHARGE_FAIL:
2542 return -EBUSY;
2543 /* Resource temporary unavailable - trying again might succeed */
2544 case SCAN_PAGE_LOCK:
2545 case SCAN_PAGE_LRU:
Zach O'Keefe0f3e2a22022-09-22 11:46:50 -07002546 case SCAN_DEL_PAGE_LRU:
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002547 return -EAGAIN;
2548 /*
2549 * Other: Trying again likely not to succeed / error intrinsic to
2550 * specified memory range. khugepaged likely won't be able to collapse
2551 * either.
2552 */
2553 default:
2554 return -EINVAL;
2555 }
2556}
2557
2558int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev,
2559 unsigned long start, unsigned long end)
2560{
2561 struct collapse_control *cc;
2562 struct mm_struct *mm = vma->vm_mm;
2563 unsigned long hstart, hend, addr;
2564 int thps = 0, last_fail = SCAN_FAIL;
2565 bool mmap_locked = true;
2566
2567 BUG_ON(vma->vm_start > start);
2568 BUG_ON(vma->vm_end < end);
2569
2570 *prev = vma;
2571
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002572 if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false))
2573 return -EINVAL;
2574
2575 cc = kmalloc(sizeof(*cc), GFP_KERNEL);
2576 if (!cc)
2577 return -ENOMEM;
2578 cc->is_khugepaged = false;
2579 cc->last_target_node = NUMA_NO_NODE;
2580
2581 mmgrab(mm);
2582 lru_add_drain_all();
2583
2584 hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2585 hend = end & HPAGE_PMD_MASK;
2586
2587 for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
2588 int result = SCAN_FAIL;
2589
2590 if (!mmap_locked) {
2591 cond_resched();
2592 mmap_read_lock(mm);
2593 mmap_locked = true;
Zach O'Keefe34488392022-09-22 15:40:39 -07002594 result = hugepage_vma_revalidate(mm, addr, false, &vma,
2595 cc);
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002596 if (result != SCAN_SUCCEED) {
2597 last_fail = result;
2598 goto out_nolock;
2599 }
Yang Shi4d24de92022-09-14 09:22:20 -07002600
2601 hend = vma->vm_end & HPAGE_PMD_MASK;
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002602 }
2603 mmap_assert_locked(mm);
2604 memset(cc->node_load, 0, sizeof(cc->node_load));
Zach O'Keefe34488392022-09-22 15:40:39 -07002605 if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
2606 struct file *file = get_file(vma->vm_file);
2607 pgoff_t pgoff = linear_page_index(vma, addr);
2608
2609 mmap_read_unlock(mm);
2610 mmap_locked = false;
2611 result = hpage_collapse_scan_file(mm, addr, file, pgoff,
2612 cc);
2613 fput(file);
2614 } else {
2615 result = hpage_collapse_scan_pmd(mm, vma, addr,
2616 &mmap_locked, cc);
2617 }
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002618 if (!mmap_locked)
2619 *prev = NULL; /* Tell caller we dropped mmap_lock */
2620
Zach O'Keefe34488392022-09-22 15:40:39 -07002621handle_result:
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002622 switch (result) {
2623 case SCAN_SUCCEED:
2624 case SCAN_PMD_MAPPED:
2625 ++thps;
2626 break;
Zach O'Keefe34488392022-09-22 15:40:39 -07002627 case SCAN_PTE_MAPPED_HUGEPAGE:
2628 BUG_ON(mmap_locked);
2629 BUG_ON(*prev);
2630 mmap_write_lock(mm);
2631 result = collapse_pte_mapped_thp(mm, addr, true);
2632 mmap_write_unlock(mm);
2633 goto handle_result;
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002634 /* Whitelisted set of results where continuing OK */
2635 case SCAN_PMD_NULL:
2636 case SCAN_PTE_NON_PRESENT:
2637 case SCAN_PTE_UFFD_WP:
2638 case SCAN_PAGE_RO:
2639 case SCAN_LACK_REFERENCED_PAGE:
2640 case SCAN_PAGE_NULL:
2641 case SCAN_PAGE_COUNT:
2642 case SCAN_PAGE_LOCK:
2643 case SCAN_PAGE_COMPOUND:
2644 case SCAN_PAGE_LRU:
Zach O'Keefe0f3e2a22022-09-22 11:46:50 -07002645 case SCAN_DEL_PAGE_LRU:
Zach O'Keefe7d8faaf2022-07-06 16:59:27 -07002646 last_fail = result;
2647 break;
2648 default:
2649 last_fail = result;
2650 /* Other error, exit */
2651 goto out_maybelock;
2652 }
2653 }
2654
2655out_maybelock:
2656 /* Caller expects us to hold mmap_lock on return */
2657 if (!mmap_locked)
2658 mmap_read_lock(mm);
2659out_nolock:
2660 mmap_assert_locked(mm);
2661 mmdrop(mm);
2662 kfree(cc);
2663
2664 return thps == ((hend - hstart) >> HPAGE_PMD_SHIFT) ? 0
2665 : madvise_collapse_errno(last_fail);
2666}