Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 3 | |
| 4 | #include <linux/mm.h> |
| 5 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 6 | #include <linux/sched/mm.h> |
Ingo Molnar | f7ccbae | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 7 | #include <linux/sched/coredump.h> |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 8 | #include <linux/mmu_notifier.h> |
| 9 | #include <linux/rmap.h> |
| 10 | #include <linux/swap.h> |
| 11 | #include <linux/mm_inline.h> |
| 12 | #include <linux/kthread.h> |
| 13 | #include <linux/khugepaged.h> |
| 14 | #include <linux/freezer.h> |
| 15 | #include <linux/mman.h> |
| 16 | #include <linux/hashtable.h> |
| 17 | #include <linux/userfaultfd_k.h> |
| 18 | #include <linux/page_idle.h> |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 19 | #include <linux/page_table_check.h> |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 20 | #include <linux/swapops.h> |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 21 | #include <linux/shmem_fs.h> |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/tlb.h> |
| 24 | #include <asm/pgalloc.h> |
| 25 | #include "internal.h" |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 26 | #include "mm_slot.h" |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 27 | |
| 28 | enum scan_result { |
| 29 | SCAN_FAIL, |
| 30 | SCAN_SUCCEED, |
| 31 | SCAN_PMD_NULL, |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 32 | SCAN_PMD_NONE, |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 33 | SCAN_PMD_MAPPED, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 34 | SCAN_EXCEED_NONE_PTE, |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 35 | SCAN_EXCEED_SWAP_PTE, |
| 36 | SCAN_EXCEED_SHARED_PTE, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 37 | SCAN_PTE_NON_PRESENT, |
Peter Xu | e1e267c | 2020-04-06 20:06:04 -0700 | [diff] [blame] | 38 | SCAN_PTE_UFFD_WP, |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 39 | SCAN_PTE_MAPPED_HUGEPAGE, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 40 | SCAN_PAGE_RO, |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 41 | SCAN_LACK_REFERENCED_PAGE, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 42 | 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. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 53 | SCAN_DEL_PAGE_LRU, |
| 54 | SCAN_ALLOC_HUGE_PAGE_FAIL, |
| 55 | SCAN_CGROUP_CHARGE_FAIL, |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 56 | SCAN_TRUNCATED, |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 57 | SCAN_PAGE_HAS_PRIVATE, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #define CREATE_TRACE_POINTS |
| 61 | #include <trace/events/huge_memory.h> |
| 62 | |
Vijay Balakrishna | 4aab2be | 2020-10-10 23:16:40 -0700 | [diff] [blame] | 63 | static struct task_struct *khugepaged_thread __read_mostly; |
| 64 | static DEFINE_MUTEX(khugepaged_mutex); |
| 65 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 66 | /* default scan 8*512 pte (or vmas) every 30 second */ |
| 67 | static unsigned int khugepaged_pages_to_scan __read_mostly; |
| 68 | static unsigned int khugepaged_pages_collapsed; |
| 69 | static unsigned int khugepaged_full_scans; |
| 70 | static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000; |
| 71 | /* during fragmentation poll the hugepage allocator once every minute */ |
| 72 | static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000; |
| 73 | static unsigned long khugepaged_sleep_expire; |
| 74 | static DEFINE_SPINLOCK(khugepaged_mm_lock); |
| 75 | static 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'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 80 | * |
| 81 | * Note that these are only respected if collapse was initiated by khugepaged. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 82 | */ |
| 83 | static unsigned int khugepaged_max_ptes_none __read_mostly; |
| 84 | static unsigned int khugepaged_max_ptes_swap __read_mostly; |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 85 | static unsigned int khugepaged_max_ptes_shared __read_mostly; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 86 | |
| 87 | #define MM_SLOTS_HASH_BITS 10 |
| 88 | static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); |
| 89 | |
| 90 | static struct kmem_cache *mm_slot_cache __read_mostly; |
| 91 | |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 92 | #define MAX_PTE_MAPPED_THP 8 |
| 93 | |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 94 | struct collapse_control { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 95 | bool is_khugepaged; |
| 96 | |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 97 | /* Num pages scanned per node */ |
| 98 | u32 node_load[MAX_NUMNODES]; |
| 99 | |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 100 | /* nodemask for allocation fallback */ |
| 101 | nodemask_t alloc_nmask; |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 104 | /** |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 105 | * struct khugepaged_mm_slot - khugepaged information per mm that is being scanned |
| 106 | * @slot: hash lookup from mm to mm_slot |
Alex Shi | 336e6b5 | 2020-12-14 19:12:01 -0800 | [diff] [blame] | 107 | * @nr_pte_mapped_thp: number of pte mapped THP |
| 108 | * @pte_mapped_thp: address array corresponding pte mapped THP |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 109 | */ |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 110 | struct khugepaged_mm_slot { |
| 111 | struct mm_slot slot; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 112 | |
| 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. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 116 | }; |
| 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 | */ |
| 126 | struct khugepaged_scan { |
| 127 | struct list_head mm_head; |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 128 | struct khugepaged_mm_slot *mm_slot; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 129 | unsigned long address; |
| 130 | }; |
| 131 | |
| 132 | static struct khugepaged_scan khugepaged_scan = { |
| 133 | .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), |
| 134 | }; |
| 135 | |
Jérémy Lefaure | e1465d1 | 2016-11-30 15:54:02 -0800 | [diff] [blame] | 136 | #ifdef CONFIG_SYSFS |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 137 | static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, |
| 138 | struct kobj_attribute *attr, |
| 139 | char *buf) |
| 140 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 141 | return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, |
| 145 | struct kobj_attribute *attr, |
| 146 | const char *buf, size_t count) |
| 147 | { |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 148 | unsigned int msecs; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 149 | int err; |
| 150 | |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 151 | err = kstrtouint(buf, 10, &msecs); |
| 152 | if (err) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 153 | 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 | } |
| 161 | static struct kobj_attribute scan_sleep_millisecs_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 162 | __ATTR_RW(scan_sleep_millisecs); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 163 | |
| 164 | static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj, |
| 165 | struct kobj_attribute *attr, |
| 166 | char *buf) |
| 167 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 168 | return sysfs_emit(buf, "%u\n", khugepaged_alloc_sleep_millisecs); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, |
| 172 | struct kobj_attribute *attr, |
| 173 | const char *buf, size_t count) |
| 174 | { |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 175 | unsigned int msecs; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 176 | int err; |
| 177 | |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 178 | err = kstrtouint(buf, 10, &msecs); |
| 179 | if (err) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 180 | 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 | } |
| 188 | static struct kobj_attribute alloc_sleep_millisecs_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 189 | __ATTR_RW(alloc_sleep_millisecs); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 190 | |
| 191 | static ssize_t pages_to_scan_show(struct kobject *kobj, |
| 192 | struct kobj_attribute *attr, |
| 193 | char *buf) |
| 194 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 195 | return sysfs_emit(buf, "%u\n", khugepaged_pages_to_scan); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 196 | } |
| 197 | static ssize_t pages_to_scan_store(struct kobject *kobj, |
| 198 | struct kobj_attribute *attr, |
| 199 | const char *buf, size_t count) |
| 200 | { |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 201 | unsigned int pages; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 202 | int err; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 203 | |
Alexey Dobriyan | dfefd22 | 2020-12-14 19:15:03 -0800 | [diff] [blame] | 204 | err = kstrtouint(buf, 10, &pages); |
| 205 | if (err || !pages) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 206 | return -EINVAL; |
| 207 | |
| 208 | khugepaged_pages_to_scan = pages; |
| 209 | |
| 210 | return count; |
| 211 | } |
| 212 | static struct kobj_attribute pages_to_scan_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 213 | __ATTR_RW(pages_to_scan); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 214 | |
| 215 | static ssize_t pages_collapsed_show(struct kobject *kobj, |
| 216 | struct kobj_attribute *attr, |
| 217 | char *buf) |
| 218 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 219 | return sysfs_emit(buf, "%u\n", khugepaged_pages_collapsed); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 220 | } |
| 221 | static struct kobj_attribute pages_collapsed_attr = |
| 222 | __ATTR_RO(pages_collapsed); |
| 223 | |
| 224 | static ssize_t full_scans_show(struct kobject *kobj, |
| 225 | struct kobj_attribute *attr, |
| 226 | char *buf) |
| 227 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 228 | return sysfs_emit(buf, "%u\n", khugepaged_full_scans); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 229 | } |
| 230 | static struct kobj_attribute full_scans_attr = |
| 231 | __ATTR_RO(full_scans); |
| 232 | |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 233 | static ssize_t defrag_show(struct kobject *kobj, |
| 234 | struct kobj_attribute *attr, char *buf) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 235 | { |
| 236 | return single_hugepage_flag_show(kobj, attr, buf, |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 237 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 238 | } |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 239 | static ssize_t defrag_store(struct kobject *kobj, |
| 240 | struct kobj_attribute *attr, |
| 241 | const char *buf, size_t count) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 242 | { |
| 243 | return single_hugepage_flag_store(kobj, attr, buf, count, |
| 244 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
| 245 | } |
| 246 | static struct kobj_attribute khugepaged_defrag_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 247 | __ATTR_RW(defrag); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 248 | |
| 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 Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 257 | static ssize_t max_ptes_none_show(struct kobject *kobj, |
| 258 | struct kobj_attribute *attr, |
| 259 | char *buf) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 260 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 261 | return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_none); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 262 | } |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 263 | static ssize_t max_ptes_none_store(struct kobject *kobj, |
| 264 | struct kobj_attribute *attr, |
| 265 | const char *buf, size_t count) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 266 | { |
| 267 | int err; |
| 268 | unsigned long max_ptes_none; |
| 269 | |
| 270 | err = kstrtoul(buf, 10, &max_ptes_none); |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 271 | if (err || max_ptes_none > HPAGE_PMD_NR - 1) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 272 | return -EINVAL; |
| 273 | |
| 274 | khugepaged_max_ptes_none = max_ptes_none; |
| 275 | |
| 276 | return count; |
| 277 | } |
| 278 | static struct kobj_attribute khugepaged_max_ptes_none_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 279 | __ATTR_RW(max_ptes_none); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 280 | |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 281 | static ssize_t max_ptes_swap_show(struct kobject *kobj, |
| 282 | struct kobj_attribute *attr, |
| 283 | char *buf) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 284 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 285 | return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_swap); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 288 | static ssize_t max_ptes_swap_store(struct kobject *kobj, |
| 289 | struct kobj_attribute *attr, |
| 290 | const char *buf, size_t count) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 291 | { |
| 292 | int err; |
| 293 | unsigned long max_ptes_swap; |
| 294 | |
| 295 | err = kstrtoul(buf, 10, &max_ptes_swap); |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 296 | if (err || max_ptes_swap > HPAGE_PMD_NR - 1) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | |
| 299 | khugepaged_max_ptes_swap = max_ptes_swap; |
| 300 | |
| 301 | return count; |
| 302 | } |
| 303 | |
| 304 | static struct kobj_attribute khugepaged_max_ptes_swap_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 305 | __ATTR_RW(max_ptes_swap); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 306 | |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 307 | static ssize_t max_ptes_shared_show(struct kobject *kobj, |
| 308 | struct kobj_attribute *attr, |
| 309 | char *buf) |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 310 | { |
Joe Perches | ae7a927 | 2020-12-14 19:14:42 -0800 | [diff] [blame] | 311 | return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_shared); |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 314 | static ssize_t max_ptes_shared_store(struct kobject *kobj, |
| 315 | struct kobj_attribute *attr, |
| 316 | const char *buf, size_t count) |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 317 | { |
| 318 | int err; |
| 319 | unsigned long max_ptes_shared; |
| 320 | |
| 321 | err = kstrtoul(buf, 10, &max_ptes_shared); |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 322 | if (err || max_ptes_shared > HPAGE_PMD_NR - 1) |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 323 | return -EINVAL; |
| 324 | |
| 325 | khugepaged_max_ptes_shared = max_ptes_shared; |
| 326 | |
| 327 | return count; |
| 328 | } |
| 329 | |
| 330 | static struct kobj_attribute khugepaged_max_ptes_shared_attr = |
Miaohe Lin | 6dcdc94 | 2022-06-25 17:28:14 +0800 | [diff] [blame] | 331 | __ATTR_RW(max_ptes_shared); |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 332 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 333 | static struct attribute *khugepaged_attr[] = { |
| 334 | &khugepaged_defrag_attr.attr, |
| 335 | &khugepaged_max_ptes_none_attr.attr, |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 336 | &khugepaged_max_ptes_swap_attr.attr, |
| 337 | &khugepaged_max_ptes_shared_attr.attr, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 338 | &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. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 343 | NULL, |
| 344 | }; |
| 345 | |
| 346 | struct attribute_group khugepaged_attr_group = { |
| 347 | .attrs = khugepaged_attr, |
| 348 | .name = "khugepaged", |
| 349 | }; |
Jérémy Lefaure | e1465d1 | 2016-11-30 15:54:02 -0800 | [diff] [blame] | 350 | #endif /* CONFIG_SYSFS */ |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 351 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 352 | int 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 Shi | c791576 | 2022-05-19 14:08:50 -0700 | [diff] [blame] | 373 | khugepaged_enter_vma(vma, *vm_flags); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 374 | 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 | |
| 389 | int __init khugepaged_init(void) |
| 390 | { |
| 391 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 392 | sizeof(struct khugepaged_mm_slot), |
| 393 | __alignof__(struct khugepaged_mm_slot), |
| 394 | 0, NULL); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 395 | 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. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 401 | khugepaged_max_ptes_shared = HPAGE_PMD_NR / 2; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | void __init khugepaged_destroy(void) |
| 407 | { |
| 408 | kmem_cache_destroy(mm_slot_cache); |
| 409 | } |
| 410 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 411 | static inline int hpage_collapse_test_exit(struct mm_struct *mm) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 412 | { |
Jann Horn | 4d45e75 | 2020-10-15 20:13:00 -0700 | [diff] [blame] | 413 | return atomic_read(&mm->mm_users) == 0; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Yang Shi | d2081b2 | 2022-05-19 14:08:49 -0700 | [diff] [blame] | 416 | void __khugepaged_enter(struct mm_struct *mm) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 417 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 418 | struct khugepaged_mm_slot *mm_slot; |
| 419 | struct mm_slot *slot; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 420 | int wakeup; |
| 421 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 422 | mm_slot = mm_slot_alloc(mm_slot_cache); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 423 | if (!mm_slot) |
Yang Shi | d2081b2 | 2022-05-19 14:08:49 -0700 | [diff] [blame] | 424 | return; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 425 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 426 | slot = &mm_slot->slot; |
| 427 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 428 | /* __khugepaged_exit() must not run from under us */ |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 429 | VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 430 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 431 | mm_slot_free(mm_slot_cache, mm_slot); |
Yang Shi | d2081b2 | 2022-05-19 14:08:49 -0700 | [diff] [blame] | 432 | return; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | spin_lock(&khugepaged_mm_lock); |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 436 | mm_slot_insert(mm_slots_hash, mm, slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 437 | /* |
| 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 Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 442 | list_add_tail(&slot->mm_node, &khugepaged_scan.mm_head); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 443 | spin_unlock(&khugepaged_mm_lock); |
| 444 | |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 445 | mmgrab(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 446 | if (wakeup) |
| 447 | wake_up_interruptible(&khugepaged_wait); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Yang Shi | c791576 | 2022-05-19 14:08:50 -0700 | [diff] [blame] | 450 | void khugepaged_enter_vma(struct vm_area_struct *vma, |
| 451 | unsigned long vm_flags) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 452 | { |
Yang Shi | 2647d11 | 2022-05-19 14:08:49 -0700 | [diff] [blame] | 453 | if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 454 | hugepage_flags_enabled()) { |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 455 | if (hugepage_vma_check(vma, vm_flags, false, false, true)) |
Yang Shi | 2647d11 | 2022-05-19 14:08:49 -0700 | [diff] [blame] | 456 | __khugepaged_enter(vma->vm_mm); |
| 457 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void __khugepaged_exit(struct mm_struct *mm) |
| 461 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 462 | struct khugepaged_mm_slot *mm_slot; |
| 463 | struct mm_slot *slot; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 464 | int free = 0; |
| 465 | |
| 466 | spin_lock(&khugepaged_mm_lock); |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 467 | slot = mm_slot_lookup(mm_slots_hash, mm); |
| 468 | mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 469 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 470 | hash_del(&slot->hash); |
| 471 | list_del(&slot->mm_node); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 472 | free = 1; |
| 473 | } |
| 474 | spin_unlock(&khugepaged_mm_lock); |
| 475 | |
| 476 | if (free) { |
| 477 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 478 | mm_slot_free(mm_slot_cache, mm_slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 479 | mmdrop(mm); |
| 480 | } else if (mm_slot) { |
| 481 | /* |
| 482 | * This is required to serialize against |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 483 | * 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. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 487 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 488 | mmap_write_lock(mm); |
| 489 | mmap_write_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Vishal Moola (Oracle) | 92644f5 | 2023-01-13 16:15:55 -0800 | [diff] [blame] | 493 | static void release_pte_folio(struct folio *folio) |
| 494 | { |
| 495 | node_stat_mod_folio(folio, |
| 496 | NR_ISOLATED_ANON + folio_is_file_lru(folio), |
| 497 | -folio_nr_pages(folio)); |
| 498 | folio_unlock(folio); |
| 499 | folio_putback_lru(folio); |
| 500 | } |
| 501 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 502 | static void release_pte_page(struct page *page) |
| 503 | { |
Vishal Moola (Oracle) | 92644f5 | 2023-01-13 16:15:55 -0800 | [diff] [blame] | 504 | release_pte_folio(page_folio(page)); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 507 | static void release_pte_pages(pte_t *pte, pte_t *_pte, |
| 508 | struct list_head *compound_pagelist) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 509 | { |
Vishal Moola (Oracle) | 9bdfeea | 2023-01-13 16:15:56 -0800 | [diff] [blame] | 510 | struct folio *folio, *tmp; |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 511 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 512 | while (--_pte >= pte) { |
| 513 | pte_t pteval = *_pte; |
Vishal Moola (Oracle) | f528260 | 2023-02-13 13:43:24 -0800 | [diff] [blame] | 514 | unsigned long pfn; |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 515 | |
Vishal Moola (Oracle) | f528260 | 2023-02-13 13:43:24 -0800 | [diff] [blame] | 516 | if (pte_none(pteval)) |
| 517 | continue; |
| 518 | pfn = pte_pfn(pteval); |
| 519 | if (is_zero_pfn(pfn)) |
| 520 | continue; |
| 521 | folio = pfn_folio(pfn); |
| 522 | if (folio_test_large(folio)) |
| 523 | continue; |
| 524 | release_pte_folio(folio); |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Vishal Moola (Oracle) | 9bdfeea | 2023-01-13 16:15:56 -0800 | [diff] [blame] | 527 | list_for_each_entry_safe(folio, tmp, compound_pagelist, lru) { |
| 528 | list_del(&folio->lru); |
| 529 | release_pte_folio(folio); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 533 | static bool is_refcount_suitable(struct page *page) |
| 534 | { |
| 535 | int expected_refcount; |
| 536 | |
| 537 | expected_refcount = total_mapcount(page); |
| 538 | if (PageSwapCache(page)) |
| 539 | expected_refcount += compound_nr(page); |
| 540 | |
| 541 | return page_count(page) == expected_refcount; |
| 542 | } |
| 543 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 544 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, |
| 545 | unsigned long address, |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 546 | pte_t *pte, |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 547 | struct collapse_control *cc, |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 548 | struct list_head *compound_pagelist) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 549 | { |
| 550 | struct page *page = NULL; |
| 551 | pte_t *_pte; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 552 | int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0; |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 553 | bool writable = false; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 554 | |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 555 | for (_pte = pte; _pte < pte + HPAGE_PMD_NR; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 556 | _pte++, address += PAGE_SIZE) { |
| 557 | pte_t pteval = *_pte; |
| 558 | if (pte_none(pteval) || (pte_present(pteval) && |
| 559 | is_zero_pfn(pte_pfn(pteval)))) { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 560 | ++none_or_zero; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 561 | if (!userfaultfd_armed(vma) && |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 562 | (!cc->is_khugepaged || |
| 563 | none_or_zero <= khugepaged_max_ptes_none)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 564 | continue; |
| 565 | } else { |
| 566 | result = SCAN_EXCEED_NONE_PTE; |
Yang Yang | e9ea874 | 2022-01-14 14:07:55 -0800 | [diff] [blame] | 567 | count_vm_event(THP_SCAN_EXCEED_NONE_PTE); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 568 | goto out; |
| 569 | } |
| 570 | } |
| 571 | if (!pte_present(pteval)) { |
| 572 | result = SCAN_PTE_NON_PRESENT; |
| 573 | goto out; |
| 574 | } |
| 575 | page = vm_normal_page(vma, address, pteval); |
Alex Sierra | 3218f87 | 2022-07-15 10:05:11 -0500 | [diff] [blame] | 576 | if (unlikely(!page) || unlikely(is_zone_device_page(page))) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 577 | result = SCAN_PAGE_NULL; |
| 578 | goto out; |
| 579 | } |
| 580 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 581 | VM_BUG_ON_PAGE(!PageAnon(page), page); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 582 | |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 583 | if (page_mapcount(page) > 1) { |
| 584 | ++shared; |
| 585 | if (cc->is_khugepaged && |
| 586 | shared > khugepaged_max_ptes_shared) { |
| 587 | result = SCAN_EXCEED_SHARED_PTE; |
| 588 | count_vm_event(THP_SCAN_EXCEED_SHARED_PTE); |
| 589 | goto out; |
| 590 | } |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 593 | if (PageCompound(page)) { |
| 594 | struct page *p; |
| 595 | page = compound_head(page); |
| 596 | |
| 597 | /* |
| 598 | * Check if we have dealt with the compound page |
| 599 | * already |
| 600 | */ |
| 601 | list_for_each_entry(p, compound_pagelist, lru) { |
| 602 | if (page == p) |
| 603 | goto next; |
| 604 | } |
| 605 | } |
| 606 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 607 | /* |
| 608 | * We can do it before isolate_lru_page because the |
| 609 | * page can't be freed from under us. NOTE: PG_lock |
| 610 | * is needed to serialize against split_huge_page |
| 611 | * when invoked from the VM. |
| 612 | */ |
| 613 | if (!trylock_page(page)) { |
| 614 | result = SCAN_PAGE_LOCK; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | /* |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 619 | * Check if the page has any GUP (or other external) pins. |
| 620 | * |
| 621 | * The page table that maps the page has been already unlinked |
| 622 | * from the page table tree and this process cannot get |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 623 | * an additional pin on the page. |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 624 | * |
| 625 | * New pins can come later if the page is shared across fork, |
| 626 | * but not from this process. The other process cannot write to |
| 627 | * the page, only trigger CoW. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 628 | */ |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 629 | if (!is_refcount_suitable(page)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 630 | unlock_page(page); |
| 631 | result = SCAN_PAGE_COUNT; |
| 632 | goto out; |
| 633 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 634 | |
| 635 | /* |
| 636 | * Isolate the page to avoid collapsing an hugepage |
| 637 | * currently in use by the VM. |
| 638 | */ |
Baolin Wang | f7f9c00 | 2023-02-15 18:39:35 +0800 | [diff] [blame] | 639 | if (!isolate_lru_page(page)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 640 | unlock_page(page); |
| 641 | result = SCAN_DEL_PAGE_LRU; |
| 642 | goto out; |
| 643 | } |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 644 | mod_node_page_state(page_pgdat(page), |
| 645 | NR_ISOLATED_ANON + page_is_file_lru(page), |
| 646 | compound_nr(page)); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 647 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 648 | VM_BUG_ON_PAGE(PageLRU(page), page); |
| 649 | |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 650 | if (PageCompound(page)) |
| 651 | list_add_tail(&page->lru, compound_pagelist); |
| 652 | next: |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 653 | /* |
| 654 | * If collapse was initiated by khugepaged, check that there is |
| 655 | * enough young pte to justify collapsing the page |
| 656 | */ |
| 657 | if (cc->is_khugepaged && |
| 658 | (pte_young(pteval) || page_is_young(page) || |
| 659 | PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm, |
| 660 | address))) |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 661 | referenced++; |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 662 | |
| 663 | if (pte_write(pteval)) |
| 664 | writable = true; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 665 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 666 | |
Miaohe Lin | 74e579b | 2021-05-04 18:33:46 -0700 | [diff] [blame] | 667 | if (unlikely(!writable)) { |
| 668 | result = SCAN_PAGE_RO; |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 669 | } else if (unlikely(cc->is_khugepaged && !referenced)) { |
Miaohe Lin | 74e579b | 2021-05-04 18:33:46 -0700 | [diff] [blame] | 670 | result = SCAN_LACK_REFERENCED_PAGE; |
| 671 | } else { |
| 672 | result = SCAN_SUCCEED; |
| 673 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, |
| 674 | referenced, writable, result); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 675 | return result; |
Miaohe Lin | 74e579b | 2021-05-04 18:33:46 -0700 | [diff] [blame] | 676 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 677 | out: |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 678 | release_pte_pages(pte, _pte, compound_pagelist); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 679 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, |
| 680 | referenced, writable, result); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 681 | return result; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, |
| 685 | struct vm_area_struct *vma, |
| 686 | unsigned long address, |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 687 | spinlock_t *ptl, |
| 688 | struct list_head *compound_pagelist) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 689 | { |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 690 | struct page *src_page, *tmp; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 691 | pte_t *_pte; |
David Rientjes | 338a16b | 2017-05-12 15:47:03 -0700 | [diff] [blame] | 692 | for (_pte = pte; _pte < pte + HPAGE_PMD_NR; |
| 693 | _pte++, page++, address += PAGE_SIZE) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 694 | pte_t pteval = *_pte; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 695 | |
| 696 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
| 697 | clear_user_highpage(page, address); |
| 698 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); |
| 699 | if (is_zero_pfn(pte_pfn(pteval))) { |
| 700 | /* |
| 701 | * ptl mostly unnecessary. |
| 702 | */ |
| 703 | spin_lock(ptl); |
Pasha Tatashin | 08d5b29 | 2022-01-14 14:06:33 -0800 | [diff] [blame] | 704 | ptep_clear(vma->vm_mm, address, _pte); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 705 | spin_unlock(ptl); |
| 706 | } |
| 707 | } else { |
| 708 | src_page = pte_page(pteval); |
| 709 | copy_user_highpage(page, src_page, address, vma); |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 710 | if (!PageCompound(src_page)) |
| 711 | release_pte_page(src_page); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 712 | /* |
| 713 | * ptl mostly unnecessary, but preempt has to |
| 714 | * be disabled to update the per-cpu stats |
| 715 | * inside page_remove_rmap(). |
| 716 | */ |
| 717 | spin_lock(ptl); |
Pasha Tatashin | 08d5b29 | 2022-01-14 14:06:33 -0800 | [diff] [blame] | 718 | ptep_clear(vma->vm_mm, address, _pte); |
Hugh Dickins | cea86fe | 2022-02-14 18:26:39 -0800 | [diff] [blame] | 719 | page_remove_rmap(src_page, vma, false); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 720 | spin_unlock(ptl); |
| 721 | free_page_and_swap_cache(src_page); |
| 722 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 723 | } |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 724 | |
| 725 | list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) { |
| 726 | list_del(&src_page->lru); |
Miaohe Lin | 1baec20 | 2022-06-25 17:28:16 +0800 | [diff] [blame] | 727 | mod_node_page_state(page_pgdat(src_page), |
| 728 | NR_ISOLATED_ANON + page_is_file_lru(src_page), |
| 729 | -compound_nr(src_page)); |
| 730 | unlock_page(src_page); |
| 731 | free_swap_cache(src_page); |
| 732 | putback_lru_page(src_page); |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 733 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void khugepaged_alloc_sleep(void) |
| 737 | { |
| 738 | DEFINE_WAIT(wait); |
| 739 | |
| 740 | add_wait_queue(&khugepaged_wait, &wait); |
Peter Zijlstra | f5d39b0 | 2022-08-22 13:18:22 +0200 | [diff] [blame] | 741 | __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE); |
| 742 | schedule_timeout(msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 743 | remove_wait_queue(&khugepaged_wait, &wait); |
| 744 | } |
| 745 | |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 746 | struct collapse_control khugepaged_collapse_control = { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 747 | .is_khugepaged = true, |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 748 | }; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 749 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 750 | static bool hpage_collapse_scan_abort(int nid, struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 751 | { |
| 752 | int i; |
| 753 | |
| 754 | /* |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 755 | * If node_reclaim_mode is disabled, then no extra effort is made to |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 756 | * allocate memory locally. |
| 757 | */ |
Dave Hansen | 202e35d | 2021-05-04 18:36:04 -0700 | [diff] [blame] | 758 | if (!node_reclaim_enabled()) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 759 | return false; |
| 760 | |
| 761 | /* If there is a count for this node already, it must be acceptable */ |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 762 | if (cc->node_load[nid]) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 763 | return false; |
| 764 | |
| 765 | for (i = 0; i < MAX_NUMNODES; i++) { |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 766 | if (!cc->node_load[i]) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 767 | continue; |
Matt Fleming | a55c745 | 2019-08-08 20:53:01 +0100 | [diff] [blame] | 768 | if (node_distance(nid, i) > node_reclaim_distance) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | return false; |
| 772 | } |
| 773 | |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 774 | #define khugepaged_defrag() \ |
| 775 | (transparent_hugepage_flags & \ |
| 776 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)) |
| 777 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 778 | /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */ |
| 779 | static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void) |
| 780 | { |
Vlastimil Babka | 2516035 | 2016-07-28 15:49:25 -0700 | [diff] [blame] | 781 | return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | #ifdef CONFIG_NUMA |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 785 | static int hpage_collapse_find_target_node(struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 786 | { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 787 | int nid, target_node = 0, max_value = 0; |
| 788 | |
| 789 | /* find first node with max normal pages hit */ |
| 790 | for (nid = 0; nid < MAX_NUMNODES; nid++) |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 791 | if (cc->node_load[nid] > max_value) { |
| 792 | max_value = cc->node_load[nid]; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 793 | target_node = nid; |
| 794 | } |
| 795 | |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 796 | for_each_online_node(nid) { |
| 797 | if (max_value == cc->node_load[nid]) |
| 798 | node_set(nid, cc->alloc_nmask); |
| 799 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 800 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 801 | return target_node; |
| 802 | } |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 803 | #else |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 804 | static int hpage_collapse_find_target_node(struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 805 | { |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 806 | return 0; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 807 | } |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 808 | #endif |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 809 | |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 810 | static bool hpage_collapse_alloc_page(struct page **hpage, gfp_t gfp, int node, |
| 811 | nodemask_t *nmask) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 812 | { |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 813 | *hpage = __alloc_pages(gfp, HPAGE_PMD_ORDER, node, nmask); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 814 | if (unlikely(!*hpage)) { |
| 815 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 816 | return false; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | prep_transhuge_page(*hpage); |
| 820 | count_vm_event(THP_COLLAPSE_ALLOC); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 821 | return true; |
| 822 | } |
| 823 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 824 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 825 | * If mmap_lock temporarily dropped, revalidate vma |
| 826 | * before taking mmap_lock. |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 827 | * Returns enum scan_result value. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 828 | */ |
| 829 | |
Kirill A. Shutemov | c131f75 | 2016-09-19 14:44:01 -0700 | [diff] [blame] | 830 | static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address, |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 831 | bool expect_anon, |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 832 | struct vm_area_struct **vmap, |
| 833 | struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 834 | { |
| 835 | struct vm_area_struct *vma; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 836 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 837 | if (unlikely(hpage_collapse_test_exit(mm))) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 838 | return SCAN_ANY_PROCESS; |
| 839 | |
Kirill A. Shutemov | c131f75 | 2016-09-19 14:44:01 -0700 | [diff] [blame] | 840 | *vmap = vma = find_vma(mm, address); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 841 | if (!vma) |
| 842 | return SCAN_VMA_NULL; |
| 843 | |
Yang Shi | 4fa6893 | 2022-06-16 10:48:35 -0700 | [diff] [blame] | 844 | if (!transhuge_vma_suitable(vma, address)) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 845 | return SCAN_ADDRESS_RANGE; |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 846 | if (!hugepage_vma_check(vma, vma->vm_flags, false, false, |
| 847 | cc->is_khugepaged)) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 848 | return SCAN_VMA_CHECK; |
Yang Shi | f707fa4 | 2022-06-16 10:48:36 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Anon VMA expected, the address may be unmapped then |
| 851 | * remapped to file after khugepaged reaquired the mmap_lock. |
| 852 | * |
| 853 | * hugepage_vma_check may return true for qualified file |
| 854 | * vmas. |
| 855 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 856 | if (expect_anon && (!(*vmap)->anon_vma || !vma_is_anonymous(*vmap))) |
| 857 | return SCAN_PAGE_ANON; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 858 | return SCAN_SUCCEED; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Zach O'Keefe | edb5d0c | 2023-01-25 14:53:58 -0800 | [diff] [blame] | 861 | /* |
| 862 | * See pmd_trans_unstable() for how the result may change out from |
| 863 | * underneath us, even if we hold mmap_lock in read. |
| 864 | */ |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 865 | static int find_pmd_or_thp_or_none(struct mm_struct *mm, |
| 866 | unsigned long address, |
| 867 | pmd_t **pmd) |
| 868 | { |
| 869 | pmd_t pmde; |
| 870 | |
| 871 | *pmd = mm_find_pmd(mm, address); |
| 872 | if (!*pmd) |
| 873 | return SCAN_PMD_NULL; |
| 874 | |
Peter Zijlstra | dab6e71 | 2020-11-26 17:20:28 +0100 | [diff] [blame] | 875 | pmde = pmdp_get_lockless(*pmd); |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 876 | |
| 877 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 878 | /* See comments in pmd_none_or_trans_huge_or_clear_bad() */ |
| 879 | barrier(); |
| 880 | #endif |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 881 | if (pmd_none(pmde)) |
| 882 | return SCAN_PMD_NONE; |
Zach O'Keefe | edb5d0c | 2023-01-25 14:53:58 -0800 | [diff] [blame] | 883 | if (!pmd_present(pmde)) |
| 884 | return SCAN_PMD_NULL; |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 885 | if (pmd_trans_huge(pmde)) |
| 886 | return SCAN_PMD_MAPPED; |
Zach O'Keefe | edb5d0c | 2023-01-25 14:53:58 -0800 | [diff] [blame] | 887 | if (pmd_devmap(pmde)) |
| 888 | return SCAN_PMD_NULL; |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 889 | if (pmd_bad(pmde)) |
| 890 | return SCAN_PMD_NULL; |
| 891 | return SCAN_SUCCEED; |
| 892 | } |
| 893 | |
| 894 | static int check_pmd_still_valid(struct mm_struct *mm, |
| 895 | unsigned long address, |
| 896 | pmd_t *pmd) |
| 897 | { |
| 898 | pmd_t *new_pmd; |
| 899 | int result = find_pmd_or_thp_or_none(mm, address, &new_pmd); |
| 900 | |
| 901 | if (result != SCAN_SUCCEED) |
| 902 | return result; |
| 903 | if (new_pmd != pmd) |
| 904 | return SCAN_FAIL; |
| 905 | return SCAN_SUCCEED; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Bring missing pages in from swap, to complete THP collapse. |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 910 | * Only done if hpage_collapse_scan_pmd believes it is worthwhile. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 911 | * |
Miaohe Lin | 4d928e2 | 2022-06-25 17:28:11 +0800 | [diff] [blame] | 912 | * Called and returns without pte mapped or spinlocks held. |
| 913 | * Note that if false is returned, mmap_lock will be released. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 914 | */ |
| 915 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 916 | static int __collapse_huge_page_swapin(struct mm_struct *mm, |
| 917 | struct vm_area_struct *vma, |
| 918 | unsigned long haddr, pmd_t *pmd, |
| 919 | int referenced) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 920 | { |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 921 | int swapped_in = 0; |
| 922 | vm_fault_t ret = 0; |
Will Deacon | 2b635dd | 2021-01-14 15:33:49 +0000 | [diff] [blame] | 923 | unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 924 | |
Will Deacon | 2b635dd | 2021-01-14 15:33:49 +0000 | [diff] [blame] | 925 | for (address = haddr; address < end; address += PAGE_SIZE) { |
| 926 | struct vm_fault vmf = { |
| 927 | .vma = vma, |
| 928 | .address = address, |
| 929 | .pgoff = linear_page_index(vma, haddr), |
| 930 | .flags = FAULT_FLAG_ALLOW_RETRY, |
| 931 | .pmd = pmd, |
| 932 | }; |
| 933 | |
| 934 | vmf.pte = pte_offset_map(pmd, address); |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 935 | vmf.orig_pte = *vmf.pte; |
Will Deacon | 2b635dd | 2021-01-14 15:33:49 +0000 | [diff] [blame] | 936 | if (!is_swap_pte(vmf.orig_pte)) { |
| 937 | pte_unmap(vmf.pte); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 938 | continue; |
Will Deacon | 2b635dd | 2021-01-14 15:33:49 +0000 | [diff] [blame] | 939 | } |
Jan Kara | 2994302 | 2016-12-14 15:07:16 -0800 | [diff] [blame] | 940 | ret = do_swap_page(&vmf); |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 941 | |
Miaohe Lin | 4d928e2 | 2022-06-25 17:28:11 +0800 | [diff] [blame] | 942 | /* |
| 943 | * do_swap_page returns VM_FAULT_RETRY with released mmap_lock. |
| 944 | * Note we treat VM_FAULT_RETRY as VM_FAULT_ERROR here because |
| 945 | * we do not retry here and swap entry will remain in pagetable |
| 946 | * resulting in later failure. |
| 947 | */ |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 948 | if (ret & VM_FAULT_RETRY) { |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 949 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 950 | /* Likely, but not guaranteed, that page lock failed */ |
| 951 | return SCAN_PAGE_LOCK; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 952 | } |
Miaohe Lin | 4d928e2 | 2022-06-25 17:28:11 +0800 | [diff] [blame] | 953 | if (ret & VM_FAULT_ERROR) { |
| 954 | mmap_read_unlock(mm); |
| 955 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 956 | return SCAN_FAIL; |
Miaohe Lin | 4d928e2 | 2022-06-25 17:28:11 +0800 | [diff] [blame] | 957 | } |
| 958 | swapped_in++; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 959 | } |
Kirill A. Shutemov | ae2c5d8 | 2020-06-03 16:00:17 -0700 | [diff] [blame] | 960 | |
| 961 | /* Drain LRU add pagevec to remove extra pin on the swapped in pages */ |
| 962 | if (swapped_in) |
| 963 | lru_add_drain(); |
| 964 | |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 965 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 966 | return SCAN_SUCCEED; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 969 | static int alloc_charge_hpage(struct page **hpage, struct mm_struct *mm, |
| 970 | struct collapse_control *cc) |
| 971 | { |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 972 | gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() : |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 973 | GFP_TRANSHUGE); |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 974 | int node = hpage_collapse_find_target_node(cc); |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 975 | |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 976 | if (!hpage_collapse_alloc_page(hpage, gfp, node, &cc->alloc_nmask)) |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 977 | return SCAN_ALLOC_HUGE_PAGE_FAIL; |
| 978 | if (unlikely(mem_cgroup_charge(page_folio(*hpage), mm, gfp))) |
| 979 | return SCAN_CGROUP_CHARGE_FAIL; |
| 980 | count_memcg_page_event(*hpage, THP_COLLAPSE_ALLOC); |
| 981 | return SCAN_SUCCEED; |
| 982 | } |
| 983 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 984 | static int collapse_huge_page(struct mm_struct *mm, unsigned long address, |
| 985 | int referenced, int unmapped, |
| 986 | struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 987 | { |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 988 | LIST_HEAD(compound_pagelist); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 989 | pmd_t *pmd, _pmd; |
| 990 | pte_t *pte; |
| 991 | pgtable_t pgtable; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 992 | struct page *hpage; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 993 | spinlock_t *pmd_ptl, *pte_ptl; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 994 | int result = SCAN_FAIL; |
Kirill A. Shutemov | c131f75 | 2016-09-19 14:44:01 -0700 | [diff] [blame] | 995 | struct vm_area_struct *vma; |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 996 | struct mmu_notifier_range range; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 997 | |
| 998 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 999 | |
Kirill A. Shutemov | 988ddb7 | 2016-07-26 15:26:26 -0700 | [diff] [blame] | 1000 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1001 | * Before allocating the hugepage, release the mmap_lock read lock. |
Kirill A. Shutemov | 988ddb7 | 2016-07-26 15:26:26 -0700 | [diff] [blame] | 1002 | * The allocation can take potentially a long time if it involves |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1003 | * sync compaction, and we do not need to hold the mmap_lock during |
Kirill A. Shutemov | 988ddb7 | 2016-07-26 15:26:26 -0700 | [diff] [blame] | 1004 | * that. We will recheck the vma after taking it again in write mode. |
| 1005 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1006 | mmap_read_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1007 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1008 | result = alloc_charge_hpage(&hpage, mm, cc); |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 1009 | if (result != SCAN_SUCCEED) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1010 | goto out_nolock; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1011 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1012 | mmap_read_lock(mm); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1013 | result = hugepage_vma_revalidate(mm, address, true, &vma, cc); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1014 | if (result != SCAN_SUCCEED) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1015 | mmap_read_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1016 | goto out_nolock; |
| 1017 | } |
| 1018 | |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 1019 | result = find_pmd_or_thp_or_none(mm, address, &pmd); |
| 1020 | if (result != SCAN_SUCCEED) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1021 | mmap_read_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1022 | goto out_nolock; |
| 1023 | } |
| 1024 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1025 | if (unmapped) { |
| 1026 | /* |
| 1027 | * __collapse_huge_page_swapin will return with mmap_lock |
| 1028 | * released when it fails. So we jump out_nolock directly in |
| 1029 | * that case. Continuing to collapse causes inconsistency. |
| 1030 | */ |
| 1031 | result = __collapse_huge_page_swapin(mm, vma, address, pmd, |
| 1032 | referenced); |
| 1033 | if (result != SCAN_SUCCEED) |
| 1034 | goto out_nolock; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1037 | mmap_read_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1038 | /* |
| 1039 | * Prevent all access to pagetables with the exception of |
| 1040 | * gup_fast later handled by the ptep_clear_flush and the VM |
| 1041 | * handled by the anon_vma lock + PG_lock. |
| 1042 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1043 | mmap_write_lock(mm); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1044 | result = hugepage_vma_revalidate(mm, address, true, &vma, cc); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1045 | if (result != SCAN_SUCCEED) |
Miaohe Lin | 18d24a7 | 2021-05-04 18:34:17 -0700 | [diff] [blame] | 1046 | goto out_up_write; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1047 | /* check if the pmd is still valid */ |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 1048 | result = check_pmd_still_valid(mm, address, pmd); |
| 1049 | if (result != SCAN_SUCCEED) |
Miaohe Lin | 18d24a7 | 2021-05-04 18:34:17 -0700 | [diff] [blame] | 1050 | goto out_up_write; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1051 | |
| 1052 | anon_vma_lock_write(vma->anon_vma); |
| 1053 | |
Alistair Popple | 7d4a8be | 2023-01-10 13:57:22 +1100 | [diff] [blame] | 1054 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, address, |
| 1055 | address + HPAGE_PMD_SIZE); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1056 | mmu_notifier_invalidate_range_start(&range); |
Ville Syrjälä | ec649c9d | 2019-11-05 21:16:48 -0800 | [diff] [blame] | 1057 | |
| 1058 | pte = pte_offset_map(pmd, address); |
| 1059 | pte_ptl = pte_lockptr(mm, pmd); |
| 1060 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1061 | pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ |
| 1062 | /* |
Yang Shi | 70cbc3c | 2022-09-07 11:01:43 -0700 | [diff] [blame] | 1063 | * This removes any huge TLB entry from the CPU so we won't allow |
| 1064 | * huge and small TLB entries for the same virtual address to |
| 1065 | * avoid the risk of CPU bugs in that area. |
| 1066 | * |
| 1067 | * Parallel fast GUP is fine since fast GUP will back off when |
| 1068 | * it detects PMD is changed. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1069 | */ |
| 1070 | _pmd = pmdp_collapse_flush(vma, address, pmd); |
| 1071 | spin_unlock(pmd_ptl); |
Jérôme Glisse | ac46d4f | 2018-12-28 00:38:09 -0800 | [diff] [blame] | 1072 | mmu_notifier_invalidate_range_end(&range); |
Jann Horn | 2ba99c5 | 2022-11-25 22:37:13 +0100 | [diff] [blame] | 1073 | tlb_remove_table_sync_one(); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1074 | |
| 1075 | spin_lock(pte_ptl); |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1076 | result = __collapse_huge_page_isolate(vma, address, pte, cc, |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1077 | &compound_pagelist); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1078 | spin_unlock(pte_ptl); |
| 1079 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1080 | if (unlikely(result != SCAN_SUCCEED)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1081 | pte_unmap(pte); |
| 1082 | spin_lock(pmd_ptl); |
| 1083 | BUG_ON(!pmd_none(*pmd)); |
| 1084 | /* |
| 1085 | * We can only use set_pmd_at when establishing |
| 1086 | * hugepmds and never for establishing regular pmds that |
| 1087 | * points to regular pagetables. Use pmd_populate for that |
| 1088 | */ |
| 1089 | pmd_populate(mm, pmd, pmd_pgtable(_pmd)); |
| 1090 | spin_unlock(pmd_ptl); |
| 1091 | anon_vma_unlock_write(vma->anon_vma); |
Miaohe Lin | 18d24a7 | 2021-05-04 18:34:17 -0700 | [diff] [blame] | 1092 | goto out_up_write; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * All pages are isolated and locked so anon_vma rmap |
| 1097 | * can't run anymore. |
| 1098 | */ |
| 1099 | anon_vma_unlock_write(vma->anon_vma); |
| 1100 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1101 | __collapse_huge_page_copy(pte, hpage, vma, address, pte_ptl, |
| 1102 | &compound_pagelist); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1103 | pte_unmap(pte); |
Miaohe Lin | 588d01f | 2021-05-04 18:33:40 -0700 | [diff] [blame] | 1104 | /* |
| 1105 | * spin_lock() below is not the equivalent of smp_wmb(), but |
| 1106 | * the smp_wmb() inside __SetPageUptodate() can be reused to |
| 1107 | * avoid the copy_huge_page writes to become visible after |
| 1108 | * the set_pmd_at() write. |
| 1109 | */ |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1110 | __SetPageUptodate(hpage); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1111 | pgtable = pmd_pgtable(_pmd); |
| 1112 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1113 | _pmd = mk_huge_pmd(hpage, vma->vm_page_prot); |
Linus Torvalds | f55e101 | 2017-11-29 09:01:01 -0800 | [diff] [blame] | 1114 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1115 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1116 | spin_lock(pmd_ptl); |
| 1117 | BUG_ON(!pmd_none(*pmd)); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1118 | page_add_new_anon_rmap(hpage, vma, address); |
| 1119 | lru_cache_add_inactive_or_unevictable(hpage, vma); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1120 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
| 1121 | set_pmd_at(mm, address, pmd, _pmd); |
| 1122 | update_mmu_cache_pmd(vma, address, pmd); |
| 1123 | spin_unlock(pmd_ptl); |
| 1124 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1125 | hpage = NULL; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1126 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1127 | result = SCAN_SUCCEED; |
| 1128 | out_up_write: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1129 | mmap_write_unlock(mm); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1130 | out_nolock: |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1131 | if (hpage) { |
| 1132 | mem_cgroup_uncharge(page_folio(hpage)); |
| 1133 | put_page(hpage); |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 1134 | } |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1135 | trace_mm_collapse_huge_page(mm, result == SCAN_SUCCEED, result); |
| 1136 | return result; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 1139 | static int hpage_collapse_scan_pmd(struct mm_struct *mm, |
| 1140 | struct vm_area_struct *vma, |
| 1141 | unsigned long address, bool *mmap_locked, |
| 1142 | struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1143 | { |
| 1144 | pmd_t *pmd; |
| 1145 | pte_t *pte, *_pte; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1146 | int result = SCAN_FAIL, referenced = 0; |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 1147 | int none_or_zero = 0, shared = 0; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1148 | struct page *page = NULL; |
| 1149 | unsigned long _address; |
| 1150 | spinlock_t *ptl; |
| 1151 | int node = NUMA_NO_NODE, unmapped = 0; |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 1152 | bool writable = false; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1153 | |
| 1154 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 1155 | |
Zach O'Keefe | 5072280 | 2022-07-06 16:59:26 -0700 | [diff] [blame] | 1156 | result = find_pmd_or_thp_or_none(mm, address, &pmd); |
| 1157 | if (result != SCAN_SUCCEED) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1158 | goto out; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1159 | |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 1160 | memset(cc->node_load, 0, sizeof(cc->node_load)); |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 1161 | nodes_clear(cc->alloc_nmask); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1162 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 1163 | for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1164 | _pte++, _address += PAGE_SIZE) { |
| 1165 | pte_t pteval = *_pte; |
| 1166 | if (is_swap_pte(pteval)) { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1167 | ++unmapped; |
| 1168 | if (!cc->is_khugepaged || |
| 1169 | unmapped <= khugepaged_max_ptes_swap) { |
Peter Xu | e1e267c | 2020-04-06 20:06:04 -0700 | [diff] [blame] | 1170 | /* |
| 1171 | * Always be strict with uffd-wp |
| 1172 | * enabled swap entries. Please see |
| 1173 | * comment below for pte_uffd_wp(). |
| 1174 | */ |
| 1175 | if (pte_swp_uffd_wp(pteval)) { |
| 1176 | result = SCAN_PTE_UFFD_WP; |
| 1177 | goto out_unmap; |
| 1178 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1179 | continue; |
| 1180 | } else { |
| 1181 | result = SCAN_EXCEED_SWAP_PTE; |
Yang Yang | e9ea874 | 2022-01-14 14:07:55 -0800 | [diff] [blame] | 1182 | count_vm_event(THP_SCAN_EXCEED_SWAP_PTE); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1183 | goto out_unmap; |
| 1184 | } |
| 1185 | } |
| 1186 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1187 | ++none_or_zero; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1188 | if (!userfaultfd_armed(vma) && |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1189 | (!cc->is_khugepaged || |
| 1190 | none_or_zero <= khugepaged_max_ptes_none)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1191 | continue; |
| 1192 | } else { |
| 1193 | result = SCAN_EXCEED_NONE_PTE; |
Yang Yang | e9ea874 | 2022-01-14 14:07:55 -0800 | [diff] [blame] | 1194 | count_vm_event(THP_SCAN_EXCEED_NONE_PTE); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1195 | goto out_unmap; |
| 1196 | } |
| 1197 | } |
Peter Xu | e1e267c | 2020-04-06 20:06:04 -0700 | [diff] [blame] | 1198 | if (pte_uffd_wp(pteval)) { |
| 1199 | /* |
| 1200 | * Don't collapse the page if any of the small |
| 1201 | * PTEs are armed with uffd write protection. |
| 1202 | * Here we can also mark the new huge pmd as |
| 1203 | * write protected if any of the small ones is |
Haitao Shi | 8958b24 | 2020-12-15 20:47:26 -0800 | [diff] [blame] | 1204 | * marked but that could bring unknown |
Peter Xu | e1e267c | 2020-04-06 20:06:04 -0700 | [diff] [blame] | 1205 | * userfault messages that falls outside of |
| 1206 | * the registered range. So, just be simple. |
| 1207 | */ |
| 1208 | result = SCAN_PTE_UFFD_WP; |
| 1209 | goto out_unmap; |
| 1210 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1211 | if (pte_write(pteval)) |
| 1212 | writable = true; |
| 1213 | |
| 1214 | page = vm_normal_page(vma, _address, pteval); |
Alex Sierra | 3218f87 | 2022-07-15 10:05:11 -0500 | [diff] [blame] | 1215 | if (unlikely(!page) || unlikely(is_zone_device_page(page))) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1216 | result = SCAN_PAGE_NULL; |
| 1217 | goto out_unmap; |
| 1218 | } |
| 1219 | |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1220 | if (page_mapcount(page) > 1) { |
| 1221 | ++shared; |
| 1222 | if (cc->is_khugepaged && |
| 1223 | shared > khugepaged_max_ptes_shared) { |
| 1224 | result = SCAN_EXCEED_SHARED_PTE; |
| 1225 | count_vm_event(THP_SCAN_EXCEED_SHARED_PTE); |
| 1226 | goto out_unmap; |
| 1227 | } |
Kirill A. Shutemov | 71a2c11 | 2020-06-03 16:00:30 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Kirill A. Shutemov | 5503fbf | 2020-06-03 16:00:23 -0700 | [diff] [blame] | 1230 | page = compound_head(page); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * Record which node the original page is from and save this |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 1234 | * information to cc->node_load[]. |
Quanfa Fu | 0b8f0d8 | 2022-01-14 14:09:25 -0800 | [diff] [blame] | 1235 | * Khugepaged will allocate hugepage from the node has the max |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1236 | * hit record. |
| 1237 | */ |
| 1238 | node = page_to_nid(page); |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 1239 | if (hpage_collapse_scan_abort(node, cc)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1240 | result = SCAN_SCAN_ABORT; |
| 1241 | goto out_unmap; |
| 1242 | } |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 1243 | cc->node_load[node]++; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1244 | if (!PageLRU(page)) { |
| 1245 | result = SCAN_PAGE_LRU; |
| 1246 | goto out_unmap; |
| 1247 | } |
| 1248 | if (PageLocked(page)) { |
| 1249 | result = SCAN_PAGE_LOCK; |
| 1250 | goto out_unmap; |
| 1251 | } |
| 1252 | if (!PageAnon(page)) { |
| 1253 | result = SCAN_PAGE_ANON; |
| 1254 | goto out_unmap; |
| 1255 | } |
| 1256 | |
| 1257 | /* |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 1258 | * Check if the page has any GUP (or other external) pins. |
| 1259 | * |
Hugh Dickins | cb67f42 | 2022-11-02 18:51:38 -0700 | [diff] [blame] | 1260 | * Here the check may be racy: |
| 1261 | * it may see total_mapcount > refcount in some cases? |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 1262 | * But such case is ephemeral we could always retry collapse |
| 1263 | * later. However it may report false positive if the page |
| 1264 | * has excessive GUP pins (i.e. 512). Anyway the same check |
| 1265 | * will be done again later the risk seems low. |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1266 | */ |
Kirill A. Shutemov | 9445689 | 2020-06-03 16:00:20 -0700 | [diff] [blame] | 1267 | if (!is_refcount_suitable(page)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1268 | result = SCAN_PAGE_COUNT; |
| 1269 | goto out_unmap; |
| 1270 | } |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* |
| 1273 | * If collapse was initiated by khugepaged, check that there is |
| 1274 | * enough young pte to justify collapsing the page |
| 1275 | */ |
| 1276 | if (cc->is_khugepaged && |
| 1277 | (pte_young(pteval) || page_is_young(page) || |
| 1278 | PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm, |
| 1279 | address))) |
Ebru Akagunduz | 0db501f | 2016-07-26 15:26:46 -0700 | [diff] [blame] | 1280 | referenced++; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1281 | } |
Kirill A. Shutemov | ffe945e | 2020-06-03 16:00:09 -0700 | [diff] [blame] | 1282 | if (!writable) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1283 | result = SCAN_PAGE_RO; |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 1284 | } else if (cc->is_khugepaged && |
| 1285 | (!referenced || |
| 1286 | (unmapped && referenced < HPAGE_PMD_NR / 2))) { |
Kirill A. Shutemov | ffe945e | 2020-06-03 16:00:09 -0700 | [diff] [blame] | 1287 | result = SCAN_LACK_REFERENCED_PAGE; |
| 1288 | } else { |
| 1289 | result = SCAN_SUCCEED; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1290 | } |
| 1291 | out_unmap: |
| 1292 | pte_unmap_unlock(pte, ptl); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1293 | if (result == SCAN_SUCCEED) { |
| 1294 | result = collapse_huge_page(mm, address, referenced, |
| 1295 | unmapped, cc); |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1296 | /* collapse_huge_page will return with the mmap_lock released */ |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1297 | *mmap_locked = false; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1298 | } |
| 1299 | out: |
| 1300 | trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced, |
| 1301 | none_or_zero, result, unmapped); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1302 | return result; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1305 | static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1306 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1307 | struct mm_slot *slot = &mm_slot->slot; |
| 1308 | struct mm_struct *mm = slot->mm; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1309 | |
Lance Roy | 35f3aa3 | 2018-10-04 23:45:47 -0700 | [diff] [blame] | 1310 | lockdep_assert_held(&khugepaged_mm_lock); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1311 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 1312 | if (hpage_collapse_test_exit(mm)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1313 | /* free mm_slot */ |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1314 | hash_del(&slot->hash); |
| 1315 | list_del(&slot->mm_node); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | * Not strictly needed because the mm exited already. |
| 1319 | * |
| 1320 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 1321 | */ |
| 1322 | |
| 1323 | /* khugepaged_mm_lock actually not necessary for the below */ |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1324 | mm_slot_free(mm_slot_cache, mm_slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 1325 | mmdrop(mm); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 1329 | #ifdef CONFIG_SHMEM |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1330 | /* |
| 1331 | * Notify khugepaged that given addr of the mm is pte-mapped THP. Then |
| 1332 | * khugepaged should try to collapse the page table. |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1333 | * |
| 1334 | * Note that following race exists: |
| 1335 | * (1) khugepaged calls khugepaged_collapse_pte_mapped_thps() for mm_struct A, |
| 1336 | * emptying the A's ->pte_mapped_thp[] array. |
| 1337 | * (2) MADV_COLLAPSE collapses some file extent with target mm_struct B, and |
| 1338 | * retract_page_tables() finds a VMA in mm_struct A mapping the same extent |
| 1339 | * (at virtual address X) and adds an entry (for X) into mm_struct A's |
| 1340 | * ->pte-mapped_thp[] array. |
| 1341 | * (3) khugepaged calls khugepaged_collapse_scan_file() for mm_struct A at X, |
| 1342 | * sees a pte-mapped THP (SCAN_PTE_MAPPED_HUGEPAGE) and adds an entry |
| 1343 | * (for X) into mm_struct A's ->pte-mapped_thp[] array. |
| 1344 | * Thus, it's possible the same address is added multiple times for the same |
| 1345 | * mm_struct. Should this happen, we'll simply attempt |
| 1346 | * collapse_pte_mapped_thp() multiple times for the same address, under the same |
| 1347 | * exclusive mmap_lock, and assuming the first call is successful, subsequent |
| 1348 | * attempts will return quickly (without grabbing any additional locks) when |
| 1349 | * a huge pmd is found in find_pmd_or_thp_or_none(). Since this is a cheap |
| 1350 | * check, and since this is a rare occurrence, the cost of preventing this |
| 1351 | * "multiple-add" is thought to be more expensive than just handling it, should |
| 1352 | * it occur. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1353 | */ |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1354 | static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm, |
Miaohe Lin | 081c325 | 2022-06-25 17:28:15 +0800 | [diff] [blame] | 1355 | unsigned long addr) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1356 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1357 | struct khugepaged_mm_slot *mm_slot; |
| 1358 | struct mm_slot *slot; |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1359 | bool ret = false; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1360 | |
| 1361 | VM_BUG_ON(addr & ~HPAGE_PMD_MASK); |
| 1362 | |
| 1363 | spin_lock(&khugepaged_mm_lock); |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1364 | slot = mm_slot_lookup(mm_slots_hash, mm); |
| 1365 | mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot); |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1366 | if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP)) { |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1367 | mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr; |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1368 | ret = true; |
| 1369 | } |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1370 | spin_unlock(&khugepaged_mm_lock); |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1371 | return ret; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1374 | /* hpage must be locked, and mmap_lock must be held in write */ |
| 1375 | static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr, |
| 1376 | pmd_t *pmdp, struct page *hpage) |
| 1377 | { |
| 1378 | struct vm_fault vmf = { |
| 1379 | .vma = vma, |
| 1380 | .address = addr, |
| 1381 | .flags = 0, |
| 1382 | .pmd = pmdp, |
| 1383 | }; |
| 1384 | |
| 1385 | VM_BUG_ON(!PageTransHuge(hpage)); |
| 1386 | mmap_assert_write_locked(vma->vm_mm); |
| 1387 | |
| 1388 | if (do_set_pmd(&vmf, hpage)) |
| 1389 | return SCAN_FAIL; |
| 1390 | |
| 1391 | get_page(hpage); |
| 1392 | return SCAN_SUCCEED; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1395 | /* |
| 1396 | * A note about locking: |
| 1397 | * Trying to take the page table spinlocks would be useless here because those |
| 1398 | * are only used to synchronize: |
| 1399 | * |
| 1400 | * - modifying terminal entries (ones that point to a data page, not to another |
| 1401 | * page table) |
| 1402 | * - installing *new* non-terminal entries |
| 1403 | * |
| 1404 | * Instead, we need roughly the same kind of protection as free_pgtables() or |
| 1405 | * mm_take_all_locks() (but only for a single VMA): |
| 1406 | * The mmap lock together with this VMA's rmap locks covers all paths towards |
| 1407 | * the page table entries we're messing with here, except for hardware page |
| 1408 | * table walks and lockless_pages_from_mm(). |
| 1409 | */ |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1410 | static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1411 | unsigned long addr, pmd_t *pmdp) |
| 1412 | { |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1413 | pmd_t pmd; |
Jann Horn | f268f6c | 2022-11-25 22:37:14 +0100 | [diff] [blame] | 1414 | struct mmu_notifier_range range; |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1415 | |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 1416 | mmap_assert_write_locked(mm); |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1417 | if (vma->vm_file) |
| 1418 | lockdep_assert_held_write(&vma->vm_file->f_mapping->i_mmap_rwsem); |
| 1419 | /* |
| 1420 | * All anon_vmas attached to the VMA have the same root and are |
| 1421 | * therefore locked by the same lock. |
| 1422 | */ |
| 1423 | if (vma->anon_vma) |
| 1424 | lockdep_assert_held_write(&vma->anon_vma->root->rwsem); |
| 1425 | |
Alistair Popple | 7d4a8be | 2023-01-10 13:57:22 +1100 | [diff] [blame] | 1426 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr, |
Jann Horn | f268f6c | 2022-11-25 22:37:14 +0100 | [diff] [blame] | 1427 | addr + HPAGE_PMD_SIZE); |
| 1428 | mmu_notifier_invalidate_range_start(&range); |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1429 | pmd = pmdp_collapse_flush(vma, addr, pmdp); |
Jann Horn | 2ba99c5 | 2022-11-25 22:37:13 +0100 | [diff] [blame] | 1430 | tlb_remove_table_sync_one(); |
Jann Horn | f268f6c | 2022-11-25 22:37:14 +0100 | [diff] [blame] | 1431 | mmu_notifier_invalidate_range_end(&range); |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1432 | mm_dec_nr_ptes(mm); |
Pasha Tatashin | 80110bb | 2022-02-03 20:49:24 -0800 | [diff] [blame] | 1433 | page_table_check_pte_clear_range(mm, addr, pmd); |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1434 | pte_free(mm, pmd_pgtable(pmd)); |
| 1435 | } |
| 1436 | |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1437 | /** |
Alex Shi | 336e6b5 | 2020-12-14 19:12:01 -0800 | [diff] [blame] | 1438 | * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at |
| 1439 | * address haddr. |
| 1440 | * |
| 1441 | * @mm: process address space where collapse happens |
| 1442 | * @addr: THP collapse address |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1443 | * @install_pmd: If a huge PMD should be installed |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1444 | * |
| 1445 | * This function checks whether all the PTEs in the PMD are pointing to the |
| 1446 | * right THP. If so, retract the page table so the THP can refault in with |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1447 | * as pmd-mapped. Possibly install a huge PMD mapping the THP. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1448 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1449 | int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, |
| 1450 | bool install_pmd) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1451 | { |
| 1452 | unsigned long haddr = addr & HPAGE_PMD_MASK; |
Liam R. Howlett | 94d815b | 2022-09-06 19:48:50 +0000 | [diff] [blame] | 1453 | struct vm_area_struct *vma = vma_lookup(mm, haddr); |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1454 | struct page *hpage; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1455 | pte_t *start_pte, *pte; |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1456 | pmd_t *pmd; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1457 | spinlock_t *ptl; |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1458 | int count = 0, result = SCAN_FAIL; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1459 | int i; |
| 1460 | |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1461 | mmap_assert_write_locked(mm); |
| 1462 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1463 | /* Fast check before locking page if already PMD-mapped */ |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1464 | result = find_pmd_or_thp_or_none(mm, haddr, &pmd); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1465 | if (result == SCAN_PMD_MAPPED) |
| 1466 | return result; |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1467 | |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1468 | if (!vma || !vma->vm_file || |
Miaohe Lin | fef792a | 2021-05-04 18:34:15 -0700 | [diff] [blame] | 1469 | !range_in_vma(vma, haddr, haddr + HPAGE_PMD_SIZE)) |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1470 | return SCAN_VMA_CHECK; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1471 | |
| 1472 | /* |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 1473 | * If we are here, we've succeeded in replacing all the native pages |
| 1474 | * in the page cache with a single hugepage. If a mm were to fault-in |
| 1475 | * this memory (mapped by a suitably aligned VMA), we'd get the hugepage |
| 1476 | * and map it by a PMD, regardless of sysfs THP settings. As such, let's |
| 1477 | * analogously elide sysfs THP settings here. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1478 | */ |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 1479 | if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false)) |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1480 | return SCAN_VMA_CHECK; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1481 | |
Peter Xu | deb4c93 | 2022-05-12 20:22:55 -0700 | [diff] [blame] | 1482 | /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */ |
| 1483 | if (userfaultfd_wp(vma)) |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1484 | return SCAN_PTE_UFFD_WP; |
Peter Xu | deb4c93 | 2022-05-12 20:22:55 -0700 | [diff] [blame] | 1485 | |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1486 | hpage = find_lock_page(vma->vm_file->f_mapping, |
| 1487 | linear_page_index(vma, haddr)); |
| 1488 | if (!hpage) |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1489 | return SCAN_PAGE_NULL; |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1490 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1491 | if (!PageHead(hpage)) { |
| 1492 | result = SCAN_FAIL; |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1493 | goto drop_hpage; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1494 | } |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1495 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1496 | if (compound_order(hpage) != HPAGE_PMD_ORDER) { |
| 1497 | result = SCAN_PAGE_COMPOUND; |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1498 | goto drop_hpage; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1499 | } |
Zach O'Keefe | 780a4b6 | 2022-09-22 15:27:31 -0700 | [diff] [blame] | 1500 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1501 | switch (result) { |
| 1502 | case SCAN_SUCCEED: |
| 1503 | break; |
| 1504 | case SCAN_PMD_NONE: |
| 1505 | /* |
| 1506 | * In MADV_COLLAPSE path, possible race with khugepaged where |
| 1507 | * all pte entries have been removed and pmd cleared. If so, |
| 1508 | * skip all the pte checks and just update the pmd mapping. |
| 1509 | */ |
| 1510 | goto maybe_install_pmd; |
| 1511 | default: |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1512 | goto drop_hpage; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1513 | } |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1514 | |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1515 | /* |
| 1516 | * We need to lock the mapping so that from here on, only GUP-fast and |
| 1517 | * hardware page walks can access the parts of the page tables that |
| 1518 | * we're operating on. |
| 1519 | * See collapse_and_free_pmd(). |
| 1520 | */ |
| 1521 | i_mmap_lock_write(vma->vm_file->f_mapping); |
| 1522 | |
| 1523 | /* |
| 1524 | * This spinlock should be unnecessary: Nobody else should be accessing |
| 1525 | * the page tables under spinlock protection here, only |
| 1526 | * lockless_pages_from_mm() and the hardware page walker can access page |
| 1527 | * tables while all the high-level locks are held in write mode. |
| 1528 | */ |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1529 | start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1530 | result = SCAN_FAIL; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* step 1: check all mapped PTEs are to the right huge page */ |
| 1533 | for (i = 0, addr = haddr, pte = start_pte; |
| 1534 | i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) { |
| 1535 | struct page *page; |
| 1536 | |
| 1537 | /* empty pte, skip */ |
| 1538 | if (pte_none(*pte)) |
| 1539 | continue; |
| 1540 | |
| 1541 | /* page swapped out, abort */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1542 | if (!pte_present(*pte)) { |
| 1543 | result = SCAN_PTE_NON_PRESENT; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1544 | goto abort; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1545 | } |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1546 | |
| 1547 | page = vm_normal_page(vma, addr, *pte); |
Alex Sierra | 3218f87 | 2022-07-15 10:05:11 -0500 | [diff] [blame] | 1548 | if (WARN_ON_ONCE(page && is_zone_device_page(page))) |
| 1549 | page = NULL; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1550 | /* |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1551 | * Note that uprobe, debugger, or MAP_PRIVATE may change the |
| 1552 | * page table, but the new page will not be a subpage of hpage. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1553 | */ |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1554 | if (hpage + i != page) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1555 | goto abort; |
| 1556 | count++; |
| 1557 | } |
| 1558 | |
| 1559 | /* step 2: adjust rmap */ |
| 1560 | for (i = 0, addr = haddr, pte = start_pte; |
| 1561 | i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) { |
| 1562 | struct page *page; |
| 1563 | |
| 1564 | if (pte_none(*pte)) |
| 1565 | continue; |
| 1566 | page = vm_normal_page(vma, addr, *pte); |
Alex Sierra | 3218f87 | 2022-07-15 10:05:11 -0500 | [diff] [blame] | 1567 | if (WARN_ON_ONCE(page && is_zone_device_page(page))) |
| 1568 | goto abort; |
Hugh Dickins | cea86fe | 2022-02-14 18:26:39 -0800 | [diff] [blame] | 1569 | page_remove_rmap(page, vma, false); |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | pte_unmap_unlock(start_pte, ptl); |
| 1573 | |
| 1574 | /* step 3: set proper refcount and mm_counters. */ |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1575 | if (count) { |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1576 | page_ref_sub(hpage, count); |
| 1577 | add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count); |
| 1578 | } |
| 1579 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1580 | /* step 4: remove pte entries */ |
Hugh Dickins | ab0c3f1 | 2022-12-22 12:41:50 -0800 | [diff] [blame] | 1581 | /* we make no change to anon, but protect concurrent anon page lookup */ |
| 1582 | if (vma->anon_vma) |
| 1583 | anon_vma_lock_write(vma->anon_vma); |
| 1584 | |
Pasha Tatashin | e59a47b8 | 2022-02-03 20:49:20 -0800 | [diff] [blame] | 1585 | collapse_and_free_pmd(mm, vma, haddr, pmd); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1586 | |
Hugh Dickins | ab0c3f1 | 2022-12-22 12:41:50 -0800 | [diff] [blame] | 1587 | if (vma->anon_vma) |
| 1588 | anon_vma_unlock_write(vma->anon_vma); |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1589 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
| 1590 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1591 | maybe_install_pmd: |
| 1592 | /* step 5: install pmd entry */ |
| 1593 | result = install_pmd |
| 1594 | ? set_huge_pmd(vma, haddr, pmd, hpage) |
| 1595 | : SCAN_SUCCEED; |
| 1596 | |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1597 | drop_hpage: |
| 1598 | unlock_page(hpage); |
| 1599 | put_page(hpage); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1600 | return result; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1601 | |
| 1602 | abort: |
| 1603 | pte_unmap_unlock(start_pte, ptl); |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1604 | i_mmap_unlock_write(vma->vm_file->f_mapping); |
Hugh Dickins | 119a5fc | 2020-08-06 23:26:18 -0700 | [diff] [blame] | 1605 | goto drop_hpage; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1608 | static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1609 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 1610 | struct mm_slot *slot = &mm_slot->slot; |
| 1611 | struct mm_struct *mm = slot->mm; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1612 | int i; |
| 1613 | |
| 1614 | if (likely(mm_slot->nr_pte_mapped_thp == 0)) |
Miaohe Lin | 0edf61e | 2021-05-04 18:33:37 -0700 | [diff] [blame] | 1615 | return; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1616 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1617 | if (!mmap_write_trylock(mm)) |
Miaohe Lin | 0edf61e | 2021-05-04 18:33:37 -0700 | [diff] [blame] | 1618 | return; |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1619 | |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 1620 | if (unlikely(hpage_collapse_test_exit(mm))) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1621 | goto out; |
| 1622 | |
| 1623 | for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++) |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1624 | collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i], false); |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1625 | |
| 1626 | out: |
| 1627 | mm_slot->nr_pte_mapped_thp = 0; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1628 | mmap_write_unlock(mm); |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1631 | static int retract_page_tables(struct address_space *mapping, pgoff_t pgoff, |
| 1632 | struct mm_struct *target_mm, |
| 1633 | unsigned long target_addr, struct page *hpage, |
| 1634 | struct collapse_control *cc) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1635 | { |
| 1636 | struct vm_area_struct *vma; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1637 | int target_result = SCAN_FAIL; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1638 | |
| 1639 | i_mmap_lock_write(mapping); |
| 1640 | vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1641 | int result = SCAN_FAIL; |
| 1642 | struct mm_struct *mm = NULL; |
| 1643 | unsigned long addr = 0; |
| 1644 | pmd_t *pmd; |
| 1645 | bool is_target = false; |
| 1646 | |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1647 | /* |
| 1648 | * Check vma->anon_vma to exclude MAP_PRIVATE mappings that |
| 1649 | * got written to. These VMAs are likely not worth investing |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 1650 | * mmap_write_lock(mm) as PMD-mapping is likely to be split |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1651 | * later. |
| 1652 | * |
Miaohe Lin | 36ee2c7 | 2022-06-25 17:28:12 +0800 | [diff] [blame] | 1653 | * Note that vma->anon_vma check is racy: it can be set up after |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1654 | * the check but before we took mmap_lock by the fault path. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1655 | * But page lock would prevent establishing any new ptes of the |
| 1656 | * page, so we are safe. |
| 1657 | * |
| 1658 | * An alternative would be drop the check, but check that page |
| 1659 | * table is clear before calling pmdp_collapse_flush() under |
| 1660 | * ptl. It has higher chance to recover THP for the VMA, but |
Jann Horn | 8d3c106 | 2022-11-25 22:37:12 +0100 | [diff] [blame] | 1661 | * has higher cost too. It would also probably require locking |
| 1662 | * the anon_vma. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1663 | */ |
Jann Horn | 023f47a | 2023-01-11 14:33:51 +0100 | [diff] [blame] | 1664 | if (READ_ONCE(vma->anon_vma)) { |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1665 | result = SCAN_PAGE_ANON; |
| 1666 | goto next; |
| 1667 | } |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1668 | addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1669 | if (addr & ~HPAGE_PMD_MASK || |
| 1670 | vma->vm_end < addr + HPAGE_PMD_SIZE) { |
| 1671 | result = SCAN_VMA_CHECK; |
| 1672 | goto next; |
| 1673 | } |
Hugh Dickins | 18e7760 | 2020-08-06 23:26:22 -0700 | [diff] [blame] | 1674 | mm = vma->vm_mm; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1675 | is_target = mm == target_mm && addr == target_addr; |
| 1676 | result = find_pmd_or_thp_or_none(mm, addr, &pmd); |
| 1677 | if (result != SCAN_SUCCEED) |
| 1678 | goto next; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1679 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1680 | * We need exclusive mmap_lock to retract page table. |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1681 | * |
| 1682 | * We use trylock due to lock inversion: we need to acquire |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1683 | * mmap_lock while holding page lock. Fault path does it in |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 1684 | * reverse order. Trylock is a way to avoid deadlock. |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1685 | * |
| 1686 | * Also, it's not MADV_COLLAPSE's job to collapse other |
| 1687 | * mappings - let khugepaged take care of them later. |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1688 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1689 | result = SCAN_PTE_MAPPED_HUGEPAGE; |
| 1690 | if ((cc->is_khugepaged || is_target) && |
| 1691 | mmap_write_trylock(mm)) { |
Peter Xu | deb4c93 | 2022-05-12 20:22:55 -0700 | [diff] [blame] | 1692 | /* |
Jann Horn | 023f47a | 2023-01-11 14:33:51 +0100 | [diff] [blame] | 1693 | * Re-check whether we have an ->anon_vma, because |
| 1694 | * collapse_and_free_pmd() requires that either no |
| 1695 | * ->anon_vma exists or the anon_vma is locked. |
| 1696 | * We already checked ->anon_vma above, but that check |
| 1697 | * is racy because ->anon_vma can be populated under the |
| 1698 | * mmap lock in read mode. |
| 1699 | */ |
| 1700 | if (vma->anon_vma) { |
| 1701 | result = SCAN_PAGE_ANON; |
| 1702 | goto unlock_next; |
| 1703 | } |
| 1704 | /* |
Peter Xu | deb4c93 | 2022-05-12 20:22:55 -0700 | [diff] [blame] | 1705 | * When a vma is registered with uffd-wp, we can't |
| 1706 | * recycle the pmd pgtable because there can be pte |
| 1707 | * markers installed. Skip it only, so the rest mm/vma |
| 1708 | * can still have the same file mapped hugely, however |
| 1709 | * it'll always mapped in small page size for uffd-wp |
| 1710 | * registered ranges. |
| 1711 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1712 | if (hpage_collapse_test_exit(mm)) { |
| 1713 | result = SCAN_ANY_PROCESS; |
| 1714 | goto unlock_next; |
| 1715 | } |
| 1716 | if (userfaultfd_wp(vma)) { |
| 1717 | result = SCAN_PTE_UFFD_WP; |
| 1718 | goto unlock_next; |
| 1719 | } |
| 1720 | collapse_and_free_pmd(mm, vma, addr, pmd); |
| 1721 | if (!cc->is_khugepaged && is_target) |
| 1722 | result = set_huge_pmd(vma, addr, pmd, hpage); |
| 1723 | else |
| 1724 | result = SCAN_SUCCEED; |
| 1725 | |
| 1726 | unlock_next: |
Hugh Dickins | 18e7760 | 2020-08-06 23:26:22 -0700 | [diff] [blame] | 1727 | mmap_write_unlock(mm); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1728 | goto next; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1729 | } |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Calling context will handle target mm/addr. Otherwise, let |
| 1732 | * khugepaged try again later. |
| 1733 | */ |
| 1734 | if (!is_target) { |
| 1735 | khugepaged_add_pte_mapped_thp(mm, addr); |
| 1736 | continue; |
| 1737 | } |
| 1738 | next: |
| 1739 | if (is_target) |
| 1740 | target_result = result; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1741 | } |
| 1742 | i_mmap_unlock_write(mapping); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1743 | return target_result; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /** |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1747 | * collapse_file - collapse filemap/tmpfs/shmem pages into huge one. |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1748 | * |
Alex Shi | 336e6b5 | 2020-12-14 19:12:01 -0800 | [diff] [blame] | 1749 | * @mm: process address space where collapse happens |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1750 | * @addr: virtual collapse start address |
Alex Shi | 336e6b5 | 2020-12-14 19:12:01 -0800 | [diff] [blame] | 1751 | * @file: file that collapse on |
| 1752 | * @start: collapse start address |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 1753 | * @cc: collapse context and scratchpad |
Alex Shi | 336e6b5 | 2020-12-14 19:12:01 -0800 | [diff] [blame] | 1754 | * |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1755 | * Basic scheme is simple, details are more complex: |
Hugh Dickins | 87c460a | 2018-11-30 14:10:43 -0800 | [diff] [blame] | 1756 | * - allocate and lock a new huge page; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1757 | * - scan page cache replacing old pages with the new one |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1758 | * + swap/gup in pages if necessary; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1759 | * + fill in gaps; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1760 | * + keep old pages around in case rollback is required; |
| 1761 | * - if replacing succeeds: |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1762 | * + copy data over; |
| 1763 | * + free old pages; |
Hugh Dickins | 87c460a | 2018-11-30 14:10:43 -0800 | [diff] [blame] | 1764 | * + unlock huge page; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1765 | * - if replacing failed; |
| 1766 | * + put all pages back and unfreeze them; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1767 | * + restore gaps in the page cache; |
Hugh Dickins | 87c460a | 2018-11-30 14:10:43 -0800 | [diff] [blame] | 1768 | * + unlock and free huge page; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1769 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 1770 | static int collapse_file(struct mm_struct *mm, unsigned long addr, |
| 1771 | struct file *file, pgoff_t start, |
| 1772 | struct collapse_control *cc) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1773 | { |
Song Liu | 579c571 | 2019-09-23 15:37:57 -0700 | [diff] [blame] | 1774 | struct address_space *mapping = file->f_mapping; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1775 | struct page *hpage; |
Gautam Menghani | 4c9473e | 2022-10-26 10:52:18 +0530 | [diff] [blame] | 1776 | pgoff_t index = 0, end = start + HPAGE_PMD_NR; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1777 | LIST_HEAD(pagelist); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1778 | XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1779 | int nr_none = 0, result = SCAN_SUCCEED; |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1780 | bool is_shmem = shmem_file(file); |
Gautam Menghani | 4c9473e | 2022-10-26 10:52:18 +0530 | [diff] [blame] | 1781 | int nr = 0; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1782 | |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1783 | VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1784 | VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); |
| 1785 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1786 | result = alloc_charge_hpage(&hpage, mm, cc); |
Zach O'Keefe | 9710a78a | 2022-07-06 16:59:22 -0700 | [diff] [blame] | 1787 | if (result != SCAN_SUCCEED) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1788 | goto out; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1789 | |
Matthew Wilcox (Oracle) | 6b24ca4 | 2020-06-27 22:19:08 -0400 | [diff] [blame] | 1790 | /* |
| 1791 | * Ensure we have slots for all the pages in the range. This is |
| 1792 | * almost certainly a no-op because most of the pages must be present |
| 1793 | */ |
Hugh Dickins | 95feeab | 2018-11-30 14:10:50 -0800 | [diff] [blame] | 1794 | do { |
| 1795 | xas_lock_irq(&xas); |
| 1796 | xas_create_range(&xas); |
| 1797 | if (!xas_error(&xas)) |
| 1798 | break; |
| 1799 | xas_unlock_irq(&xas); |
| 1800 | if (!xas_nomem(&xas, GFP_KERNEL)) { |
Hugh Dickins | 95feeab | 2018-11-30 14:10:50 -0800 | [diff] [blame] | 1801 | result = SCAN_FAIL; |
| 1802 | goto out; |
| 1803 | } |
| 1804 | } while (1); |
| 1805 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1806 | __SetPageLocked(hpage); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1807 | if (is_shmem) |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1808 | __SetPageSwapBacked(hpage); |
| 1809 | hpage->index = start; |
| 1810 | hpage->mapping = mapping; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1811 | |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1812 | /* |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1813 | * At this point the hpage is locked and not up-to-date. |
Hugh Dickins | 87c460a | 2018-11-30 14:10:43 -0800 | [diff] [blame] | 1814 | * It's safe to insert it into the page cache, because nobody would |
| 1815 | * be able to map it or use it in another way until we unlock it. |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1816 | */ |
| 1817 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1818 | xas_set(&xas, start); |
| 1819 | for (index = start; index < end; index++) { |
| 1820 | struct page *page = xas_next(&xas); |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1821 | struct folio *folio; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1822 | |
| 1823 | VM_BUG_ON(index != xas.xa_index); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1824 | if (is_shmem) { |
| 1825 | if (!page) { |
| 1826 | /* |
| 1827 | * Stop if extent has been truncated or |
| 1828 | * hole-punched, and is now completely |
| 1829 | * empty. |
| 1830 | */ |
| 1831 | if (index == start) { |
| 1832 | if (!xas_next_entry(&xas, end - 1)) { |
| 1833 | result = SCAN_TRUNCATED; |
| 1834 | goto xa_locked; |
| 1835 | } |
| 1836 | xas_set(&xas, index); |
| 1837 | } |
| 1838 | if (!shmem_charge(mapping->host, 1)) { |
| 1839 | result = SCAN_FAIL; |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 1840 | goto xa_locked; |
Hugh Dickins | 701270f | 2018-11-30 14:10:25 -0800 | [diff] [blame] | 1841 | } |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1842 | xas_store(&xas, hpage); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1843 | nr_none++; |
| 1844 | continue; |
Hugh Dickins | 701270f | 2018-11-30 14:10:25 -0800 | [diff] [blame] | 1845 | } |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1846 | |
| 1847 | if (xa_is_value(page) || !PageUptodate(page)) { |
| 1848 | xas_unlock_irq(&xas); |
| 1849 | /* swap in or instantiate fallocated page */ |
Matthew Wilcox (Oracle) | 7459c14 | 2022-09-02 20:46:27 +0100 | [diff] [blame] | 1850 | if (shmem_get_folio(mapping->host, index, |
| 1851 | &folio, SGP_NOALLOC)) { |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1852 | result = SCAN_FAIL; |
| 1853 | goto xa_unlocked; |
| 1854 | } |
Matthew Wilcox (Oracle) | 7459c14 | 2022-09-02 20:46:27 +0100 | [diff] [blame] | 1855 | page = folio_file_page(folio, index); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1856 | } else if (trylock_page(page)) { |
| 1857 | get_page(page); |
| 1858 | xas_unlock_irq(&xas); |
| 1859 | } else { |
| 1860 | result = SCAN_PAGE_LOCK; |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 1861 | goto xa_locked; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1862 | } |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1863 | } else { /* !is_shmem */ |
| 1864 | if (!page || xa_is_value(page)) { |
| 1865 | xas_unlock_irq(&xas); |
| 1866 | page_cache_sync_readahead(mapping, &file->f_ra, |
| 1867 | file, index, |
David Howells | e5a59d3 | 2020-09-04 16:36:16 -0700 | [diff] [blame] | 1868 | end - index); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1869 | /* drain pagevecs to help isolate_lru_page() */ |
| 1870 | lru_add_drain(); |
| 1871 | page = find_lock_page(mapping, index); |
| 1872 | if (unlikely(page == NULL)) { |
| 1873 | result = SCAN_FAIL; |
| 1874 | goto xa_unlocked; |
| 1875 | } |
Song Liu | 75f3606 | 2019-11-30 17:57:19 -0800 | [diff] [blame] | 1876 | } else if (PageDirty(page)) { |
| 1877 | /* |
| 1878 | * khugepaged only works on read-only fd, |
| 1879 | * so this page is dirty because it hasn't |
| 1880 | * been flushed since first write. There |
| 1881 | * won't be new dirty pages. |
| 1882 | * |
| 1883 | * Trigger async flush here and hope the |
| 1884 | * writeback is done when khugepaged |
| 1885 | * revisits this page. |
| 1886 | * |
| 1887 | * This is a one-off situation. We are not |
| 1888 | * forcing writeback in loop. |
| 1889 | */ |
| 1890 | xas_unlock_irq(&xas); |
| 1891 | filemap_flush(mapping); |
| 1892 | result = SCAN_FAIL; |
| 1893 | goto xa_unlocked; |
Rongwei Wang | 74c42e1 | 2021-10-28 14:36:27 -0700 | [diff] [blame] | 1894 | } else if (PageWriteback(page)) { |
| 1895 | xas_unlock_irq(&xas); |
| 1896 | result = SCAN_FAIL; |
| 1897 | goto xa_unlocked; |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1898 | } else if (trylock_page(page)) { |
| 1899 | get_page(page); |
| 1900 | xas_unlock_irq(&xas); |
| 1901 | } else { |
| 1902 | result = SCAN_PAGE_LOCK; |
| 1903 | goto xa_locked; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1904 | } |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1908 | * The page must be locked, so we can drop the i_pages lock |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1909 | * without racing with truncate. |
| 1910 | */ |
| 1911 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Song Liu | 4655e5e | 2019-11-15 17:34:53 -0800 | [diff] [blame] | 1912 | |
| 1913 | /* make sure the page is up to date */ |
| 1914 | if (unlikely(!PageUptodate(page))) { |
| 1915 | result = SCAN_FAIL; |
| 1916 | goto out_unlock; |
| 1917 | } |
Hugh Dickins | 06a5e12 | 2018-11-30 14:10:47 -0800 | [diff] [blame] | 1918 | |
| 1919 | /* |
| 1920 | * If file was truncated then extended, or hole-punched, before |
| 1921 | * we locked the first page, then a THP might be there already. |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1922 | * This will be discovered on the first iteration. |
Hugh Dickins | 06a5e12 | 2018-11-30 14:10:47 -0800 | [diff] [blame] | 1923 | */ |
| 1924 | if (PageTransCompound(page)) { |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 1925 | struct page *head = compound_head(page); |
| 1926 | |
| 1927 | result = compound_order(head) == HPAGE_PMD_ORDER && |
| 1928 | head->index == start |
| 1929 | /* Maybe PMD-mapped */ |
| 1930 | ? SCAN_PTE_MAPPED_HUGEPAGE |
| 1931 | : SCAN_PAGE_COMPOUND; |
Hugh Dickins | 06a5e12 | 2018-11-30 14:10:47 -0800 | [diff] [blame] | 1932 | goto out_unlock; |
| 1933 | } |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1934 | |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1935 | folio = page_folio(page); |
| 1936 | |
| 1937 | if (folio_mapping(folio) != mapping) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1938 | result = SCAN_TRUNCATED; |
| 1939 | goto out_unlock; |
| 1940 | } |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1941 | |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1942 | if (!is_shmem && (folio_test_dirty(folio) || |
| 1943 | folio_test_writeback(folio))) { |
Song Liu | 4655e5e | 2019-11-15 17:34:53 -0800 | [diff] [blame] | 1944 | /* |
| 1945 | * khugepaged only works on read-only fd, so this |
| 1946 | * page is dirty because it hasn't been flushed |
| 1947 | * since first write. |
| 1948 | */ |
| 1949 | result = SCAN_FAIL; |
| 1950 | goto out_unlock; |
| 1951 | } |
| 1952 | |
Baolin Wang | be2d575 | 2023-02-15 18:39:34 +0800 | [diff] [blame] | 1953 | if (!folio_isolate_lru(folio)) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1954 | result = SCAN_DEL_PAGE_LRU; |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 1955 | goto out_unlock; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1958 | if (folio_has_private(folio) && |
| 1959 | !filemap_release_folio(folio, GFP_KERNEL)) { |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1960 | result = SCAN_PAGE_HAS_PRIVATE; |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1961 | folio_putback_lru(folio); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 1962 | goto out_unlock; |
| 1963 | } |
| 1964 | |
Vishal Moola (Oracle) | 64ab319 | 2022-11-17 23:30:53 -0800 | [diff] [blame] | 1965 | if (folio_mapped(folio)) |
| 1966 | try_to_unmap(folio, |
Matthew Wilcox (Oracle) | 869f7ee | 2022-02-15 09:28:49 -0500 | [diff] [blame] | 1967 | TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1968 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1969 | xas_lock_irq(&xas); |
| 1970 | xas_set(&xas, index); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1971 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1972 | VM_BUG_ON_PAGE(page != xas_load(&xas), page); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1973 | |
| 1974 | /* |
| 1975 | * The page is expected to have page_count() == 3: |
| 1976 | * - we hold a pin on it; |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 1977 | * - one reference from page cache; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1978 | * - one from isolate_lru_page; |
| 1979 | */ |
| 1980 | if (!page_ref_freeze(page, 3)) { |
| 1981 | result = SCAN_PAGE_COUNT; |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 1982 | xas_unlock_irq(&xas); |
| 1983 | putback_lru_page(page); |
| 1984 | goto out_unlock; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Add the page to the list to be able to undo the collapse if |
| 1989 | * something go wrong. |
| 1990 | */ |
| 1991 | list_add_tail(&page->lru, &pagelist); |
| 1992 | |
| 1993 | /* Finally, replace with the new page. */ |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 1994 | xas_store(&xas, hpage); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1995 | continue; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 1996 | out_unlock: |
| 1997 | unlock_page(page); |
| 1998 | put_page(page); |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 1999 | goto xa_unlocked; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2000 | } |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2001 | nr = thp_nr_pages(hpage); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2002 | |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2003 | if (is_shmem) |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2004 | __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); |
Song Liu | 09d91cd | 2019-09-23 15:38:03 -0700 | [diff] [blame] | 2005 | else { |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2006 | __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); |
Song Liu | 09d91cd | 2019-09-23 15:38:03 -0700 | [diff] [blame] | 2007 | filemap_nr_thps_inc(mapping); |
Collin Fijalkovich | eb6ecbe | 2021-06-30 18:51:32 -0700 | [diff] [blame] | 2008 | /* |
| 2009 | * Paired with smp_mb() in do_dentry_open() to ensure |
| 2010 | * i_writecount is up to date and the update to nr_thps is |
| 2011 | * visible. Ensures the page cache will be truncated if the |
| 2012 | * file is opened writable. |
| 2013 | */ |
| 2014 | smp_mb(); |
| 2015 | if (inode_is_open_for_write(mapping->host)) { |
| 2016 | result = SCAN_FAIL; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2017 | __mod_lruvec_page_state(hpage, NR_FILE_THPS, -nr); |
Collin Fijalkovich | eb6ecbe | 2021-06-30 18:51:32 -0700 | [diff] [blame] | 2018 | filemap_nr_thps_dec(mapping); |
| 2019 | goto xa_locked; |
| 2020 | } |
Song Liu | 09d91cd | 2019-09-23 15:38:03 -0700 | [diff] [blame] | 2021 | } |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2022 | |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2023 | if (nr_none) { |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2024 | __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); |
Miaohe Lin | 2f55f07 | 2022-06-25 17:28:13 +0800 | [diff] [blame] | 2025 | /* nr_none is always 0 for non-shmem. */ |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2026 | __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none); |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Matthew Wilcox (Oracle) | 6b24ca4 | 2020-06-27 22:19:08 -0400 | [diff] [blame] | 2029 | /* Join all the small entries into a single multi-index entry */ |
| 2030 | xas_set_order(&xas, start, HPAGE_PMD_ORDER); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2031 | xas_store(&xas, hpage); |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2032 | xa_locked: |
| 2033 | xas_unlock_irq(&xas); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2034 | xa_unlocked: |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2035 | |
Hugh Dickins | 6d9df8a | 2022-02-14 18:40:55 -0800 | [diff] [blame] | 2036 | /* |
| 2037 | * If collapse is successful, flush must be done now before copying. |
| 2038 | * If collapse is unsuccessful, does flush actually need to be done? |
| 2039 | * Do it anyway, to clear the state. |
| 2040 | */ |
| 2041 | try_to_unmap_flush(); |
| 2042 | |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2043 | if (result == SCAN_SUCCEED) { |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2044 | struct page *page, *tmp; |
Vishal Moola (Oracle) | 284a344 | 2022-11-01 10:53:25 -0700 | [diff] [blame] | 2045 | struct folio *folio; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2046 | |
| 2047 | /* |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2048 | * Replacing old pages with new one has succeeded, now we |
| 2049 | * need to copy the content and free the old pages. |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2050 | */ |
Hugh Dickins | 2af8ff2 | 2018-11-30 14:10:35 -0800 | [diff] [blame] | 2051 | index = start; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2052 | list_for_each_entry_safe(page, tmp, &pagelist, lru) { |
Hugh Dickins | 2af8ff2 | 2018-11-30 14:10:35 -0800 | [diff] [blame] | 2053 | while (index < page->index) { |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2054 | clear_highpage(hpage + (index % HPAGE_PMD_NR)); |
Hugh Dickins | 2af8ff2 | 2018-11-30 14:10:35 -0800 | [diff] [blame] | 2055 | index++; |
| 2056 | } |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2057 | copy_highpage(hpage + (page->index % HPAGE_PMD_NR), |
| 2058 | page); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2059 | list_del(&page->lru); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2060 | page->mapping = NULL; |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2061 | page_ref_unfreeze(page, 1); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2062 | ClearPageActive(page); |
| 2063 | ClearPageUnevictable(page); |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2064 | unlock_page(page); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2065 | put_page(page); |
Hugh Dickins | 2af8ff2 | 2018-11-30 14:10:35 -0800 | [diff] [blame] | 2066 | index++; |
| 2067 | } |
| 2068 | while (index < end) { |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2069 | clear_highpage(hpage + (index % HPAGE_PMD_NR)); |
Hugh Dickins | 2af8ff2 | 2018-11-30 14:10:35 -0800 | [diff] [blame] | 2070 | index++; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
Vishal Moola (Oracle) | 284a344 | 2022-11-01 10:53:25 -0700 | [diff] [blame] | 2073 | folio = page_folio(hpage); |
| 2074 | folio_mark_uptodate(folio); |
| 2075 | folio_ref_add(folio, HPAGE_PMD_NR - 1); |
| 2076 | |
Johannes Weiner | 6058eae | 2020-06-03 16:02:40 -0700 | [diff] [blame] | 2077 | if (is_shmem) |
Vishal Moola (Oracle) | 284a344 | 2022-11-01 10:53:25 -0700 | [diff] [blame] | 2078 | folio_mark_dirty(folio); |
| 2079 | folio_add_lru(folio); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2080 | |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2081 | /* |
| 2082 | * Remove pte page tables, so we can re-fault the page as huge. |
| 2083 | */ |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2084 | result = retract_page_tables(mapping, start, mm, addr, hpage, |
| 2085 | cc); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2086 | unlock_page(hpage); |
| 2087 | hpage = NULL; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2088 | } else { |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2089 | struct page *page; |
Hugh Dickins | aaa52e3 | 2018-11-30 14:10:29 -0800 | [diff] [blame] | 2090 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2091 | /* Something went wrong: roll back page cache changes */ |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2092 | xas_lock_irq(&xas); |
Miaohe Lin | 2f55f07 | 2022-06-25 17:28:13 +0800 | [diff] [blame] | 2093 | if (nr_none) { |
| 2094 | mapping->nrpages -= nr_none; |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2095 | shmem_uncharge(mapping->host, nr_none); |
Miaohe Lin | 2f55f07 | 2022-06-25 17:28:13 +0800 | [diff] [blame] | 2096 | } |
Hugh Dickins | aaa52e3 | 2018-11-30 14:10:29 -0800 | [diff] [blame] | 2097 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2098 | xas_set(&xas, start); |
| 2099 | xas_for_each(&xas, page, end - 1) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2100 | page = list_first_entry_or_null(&pagelist, |
| 2101 | struct page, lru); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2102 | if (!page || xas.xa_index < page->index) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2103 | if (!nr_none) |
| 2104 | break; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2105 | nr_none--; |
Johannes Weiner | 59749e6 | 2016-12-12 16:43:35 -0800 | [diff] [blame] | 2106 | /* Put holes back where they were */ |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2107 | xas_store(&xas, NULL); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2108 | continue; |
| 2109 | } |
| 2110 | |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2111 | VM_BUG_ON_PAGE(page->index != xas.xa_index, page); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2112 | |
| 2113 | /* Unfreeze the page. */ |
| 2114 | list_del(&page->lru); |
| 2115 | page_ref_unfreeze(page, 2); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2116 | xas_store(&xas, page); |
| 2117 | xas_pause(&xas); |
| 2118 | xas_unlock_irq(&xas); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2119 | unlock_page(page); |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2120 | putback_lru_page(page); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2121 | xas_lock_irq(&xas); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2122 | } |
| 2123 | VM_BUG_ON(nr_none); |
Matthew Wilcox | 77da938 | 2017-12-04 14:56:08 -0500 | [diff] [blame] | 2124 | xas_unlock_irq(&xas); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2125 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2126 | hpage->mapping = NULL; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2127 | } |
Hugh Dickins | 042a308 | 2018-11-30 14:10:39 -0800 | [diff] [blame] | 2128 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2129 | if (hpage) |
| 2130 | unlock_page(hpage); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2131 | out: |
| 2132 | VM_BUG_ON(!list_empty(&pagelist)); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2133 | if (hpage) { |
| 2134 | mem_cgroup_uncharge(page_folio(hpage)); |
| 2135 | put_page(hpage); |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 2136 | } |
Gautam Menghani | 4c9473e | 2022-10-26 10:52:18 +0530 | [diff] [blame] | 2137 | |
| 2138 | trace_mm_khugepaged_collapse_file(mm, hpage, index, is_shmem, addr, file, nr, result); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2139 | return result; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2142 | static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, |
| 2143 | struct file *file, pgoff_t start, |
| 2144 | struct collapse_control *cc) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2145 | { |
| 2146 | struct page *page = NULL; |
Song Liu | 579c571 | 2019-09-23 15:37:57 -0700 | [diff] [blame] | 2147 | struct address_space *mapping = file->f_mapping; |
Matthew Wilcox | 85b392d | 2017-12-04 15:06:23 -0500 | [diff] [blame] | 2148 | XA_STATE(xas, &mapping->i_pages, start); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2149 | int present, swap; |
| 2150 | int node = NUMA_NO_NODE; |
| 2151 | int result = SCAN_SUCCEED; |
| 2152 | |
| 2153 | present = 0; |
| 2154 | swap = 0; |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 2155 | memset(cc->node_load, 0, sizeof(cc->node_load)); |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 2156 | nodes_clear(cc->alloc_nmask); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2157 | rcu_read_lock(); |
Matthew Wilcox | 85b392d | 2017-12-04 15:06:23 -0500 | [diff] [blame] | 2158 | xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) { |
| 2159 | if (xas_retry(&xas, page)) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2160 | continue; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2161 | |
Matthew Wilcox | 85b392d | 2017-12-04 15:06:23 -0500 | [diff] [blame] | 2162 | if (xa_is_value(page)) { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 2163 | ++swap; |
| 2164 | if (cc->is_khugepaged && |
| 2165 | swap > khugepaged_max_ptes_swap) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2166 | result = SCAN_EXCEED_SWAP_PTE; |
Yang Yang | e9ea874 | 2022-01-14 14:07:55 -0800 | [diff] [blame] | 2167 | count_vm_event(THP_SCAN_EXCEED_SWAP_PTE); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2168 | break; |
| 2169 | } |
| 2170 | continue; |
| 2171 | } |
| 2172 | |
Matthew Wilcox (Oracle) | 6b24ca4 | 2020-06-27 22:19:08 -0400 | [diff] [blame] | 2173 | /* |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 2174 | * TODO: khugepaged should compact smaller compound pages |
Matthew Wilcox (Oracle) | 6b24ca4 | 2020-06-27 22:19:08 -0400 | [diff] [blame] | 2175 | * into a PMD sized page |
| 2176 | */ |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2177 | if (PageTransCompound(page)) { |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 2178 | struct page *head = compound_head(page); |
| 2179 | |
| 2180 | result = compound_order(head) == HPAGE_PMD_ORDER && |
| 2181 | head->index == start |
| 2182 | /* Maybe PMD-mapped */ |
| 2183 | ? SCAN_PTE_MAPPED_HUGEPAGE |
| 2184 | : SCAN_PAGE_COMPOUND; |
| 2185 | /* |
| 2186 | * For SCAN_PTE_MAPPED_HUGEPAGE, further processing |
| 2187 | * by the caller won't touch the page cache, and so |
| 2188 | * it's safe to skip LRU and refcount checks before |
| 2189 | * returning. |
| 2190 | */ |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2191 | break; |
| 2192 | } |
| 2193 | |
| 2194 | node = page_to_nid(page); |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 2195 | if (hpage_collapse_scan_abort(node, cc)) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2196 | result = SCAN_SCAN_ABORT; |
| 2197 | break; |
| 2198 | } |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 2199 | cc->node_load[node]++; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2200 | |
| 2201 | if (!PageLRU(page)) { |
| 2202 | result = SCAN_PAGE_LRU; |
| 2203 | break; |
| 2204 | } |
| 2205 | |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2206 | if (page_count(page) != |
| 2207 | 1 + page_mapcount(page) + page_has_private(page)) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2208 | result = SCAN_PAGE_COUNT; |
| 2209 | break; |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * We probably should check if the page is referenced here, but |
| 2214 | * nobody would transfer pte_young() to PageReferenced() for us. |
| 2215 | * And rmap walk here is just too costly... |
| 2216 | */ |
| 2217 | |
| 2218 | present++; |
| 2219 | |
| 2220 | if (need_resched()) { |
Matthew Wilcox | 85b392d | 2017-12-04 15:06:23 -0500 | [diff] [blame] | 2221 | xas_pause(&xas); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2222 | cond_resched_rcu(); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | rcu_read_unlock(); |
| 2226 | |
| 2227 | if (result == SCAN_SUCCEED) { |
Zach O'Keefe | d8ea7cc | 2022-07-06 16:59:24 -0700 | [diff] [blame] | 2228 | if (cc->is_khugepaged && |
| 2229 | present < HPAGE_PMD_NR - khugepaged_max_ptes_none) { |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2230 | result = SCAN_EXCEED_NONE_PTE; |
Yang Yang | e9ea874 | 2022-01-14 14:07:55 -0800 | [diff] [blame] | 2231 | count_vm_event(THP_SCAN_EXCEED_NONE_PTE); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2232 | } else { |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2233 | result = collapse_file(mm, addr, file, start, cc); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
Gautam Menghani | 045634f | 2022-10-26 10:15:24 +0530 | [diff] [blame] | 2237 | trace_mm_khugepaged_scan_file(mm, page, file, present, swap, result); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2238 | return result; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2239 | } |
| 2240 | #else |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2241 | static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr, |
| 2242 | struct file *file, pgoff_t start, |
| 2243 | struct collapse_control *cc) |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2244 | { |
| 2245 | BUILD_BUG(); |
| 2246 | } |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 2247 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2248 | static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot) |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 2249 | { |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 2250 | } |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 2251 | |
| 2252 | static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm, |
| 2253 | unsigned long addr) |
| 2254 | { |
| 2255 | return false; |
| 2256 | } |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2257 | #endif |
| 2258 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2259 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 2260 | struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2261 | __releases(&khugepaged_mm_lock) |
| 2262 | __acquires(&khugepaged_mm_lock) |
| 2263 | { |
Matthew Wilcox (Oracle) | 6854050 | 2022-09-06 19:49:00 +0000 | [diff] [blame] | 2264 | struct vma_iterator vmi; |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2265 | struct khugepaged_mm_slot *mm_slot; |
| 2266 | struct mm_slot *slot; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2267 | struct mm_struct *mm; |
| 2268 | struct vm_area_struct *vma; |
| 2269 | int progress = 0; |
| 2270 | |
| 2271 | VM_BUG_ON(!pages); |
Lance Roy | 35f3aa3 | 2018-10-04 23:45:47 -0700 | [diff] [blame] | 2272 | lockdep_assert_held(&khugepaged_mm_lock); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2273 | *result = SCAN_FAIL; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2274 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2275 | if (khugepaged_scan.mm_slot) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2276 | mm_slot = khugepaged_scan.mm_slot; |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2277 | slot = &mm_slot->slot; |
| 2278 | } else { |
| 2279 | slot = list_entry(khugepaged_scan.mm_head.next, |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2280 | struct mm_slot, mm_node); |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2281 | mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2282 | khugepaged_scan.address = 0; |
| 2283 | khugepaged_scan.mm_slot = mm_slot; |
| 2284 | } |
| 2285 | spin_unlock(&khugepaged_mm_lock); |
Song Liu | 27e1f82 | 2019-09-23 15:38:30 -0700 | [diff] [blame] | 2286 | khugepaged_collapse_pte_mapped_thps(mm_slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2287 | |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2288 | mm = slot->mm; |
Yang Shi | 3b454ad | 2018-01-31 16:18:28 -0800 | [diff] [blame] | 2289 | /* |
| 2290 | * Don't wait for semaphore (to avoid long wait times). Just move to |
| 2291 | * the next mm on the list. |
| 2292 | */ |
| 2293 | vma = NULL; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2294 | if (unlikely(!mmap_read_trylock(mm))) |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2295 | goto breakouterloop_mmap_lock; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2296 | |
| 2297 | progress++; |
Matthew Wilcox (Oracle) | 6854050 | 2022-09-06 19:49:00 +0000 | [diff] [blame] | 2298 | if (unlikely(hpage_collapse_test_exit(mm))) |
| 2299 | goto breakouterloop; |
| 2300 | |
| 2301 | vma_iter_init(&vmi, mm, khugepaged_scan.address); |
| 2302 | for_each_vma(vmi, vma) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2303 | unsigned long hstart, hend; |
| 2304 | |
| 2305 | cond_resched(); |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 2306 | if (unlikely(hpage_collapse_test_exit(mm))) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2307 | progress++; |
| 2308 | break; |
| 2309 | } |
Zach O'Keefe | a7f4e6e | 2022-07-06 16:59:25 -0700 | [diff] [blame] | 2310 | if (!hugepage_vma_check(vma, vma->vm_flags, false, false, true)) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2311 | skip: |
| 2312 | progress++; |
| 2313 | continue; |
| 2314 | } |
Yang Shi | 4fa6893 | 2022-06-16 10:48:35 -0700 | [diff] [blame] | 2315 | hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE); |
| 2316 | hend = round_down(vma->vm_end, HPAGE_PMD_SIZE); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2317 | if (khugepaged_scan.address > hend) |
| 2318 | goto skip; |
| 2319 | if (khugepaged_scan.address < hstart) |
| 2320 | khugepaged_scan.address = hstart; |
| 2321 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); |
| 2322 | |
| 2323 | while (khugepaged_scan.address < hend) { |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2324 | bool mmap_locked = true; |
| 2325 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2326 | cond_resched(); |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 2327 | if (unlikely(hpage_collapse_test_exit(mm))) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2328 | goto breakouterloop; |
| 2329 | |
| 2330 | VM_BUG_ON(khugepaged_scan.address < hstart || |
| 2331 | khugepaged_scan.address + HPAGE_PMD_SIZE > |
| 2332 | hend); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2333 | if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) { |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 2334 | struct file *file = get_file(vma->vm_file); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2335 | pgoff_t pgoff = linear_page_index(vma, |
| 2336 | khugepaged_scan.address); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 2337 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2338 | mmap_read_unlock(mm); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2339 | *result = hpage_collapse_scan_file(mm, |
| 2340 | khugepaged_scan.address, |
| 2341 | file, pgoff, cc); |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2342 | mmap_locked = false; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2343 | fput(file); |
| 2344 | } else { |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 2345 | *result = hpage_collapse_scan_pmd(mm, vma, |
| 2346 | khugepaged_scan.address, |
| 2347 | &mmap_locked, |
| 2348 | cc); |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2349 | } |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 2350 | switch (*result) { |
| 2351 | case SCAN_PTE_MAPPED_HUGEPAGE: { |
| 2352 | pmd_t *pmd; |
| 2353 | |
| 2354 | *result = find_pmd_or_thp_or_none(mm, |
| 2355 | khugepaged_scan.address, |
| 2356 | &pmd); |
| 2357 | if (*result != SCAN_SUCCEED) |
| 2358 | break; |
| 2359 | if (!khugepaged_add_pte_mapped_thp(mm, |
| 2360 | khugepaged_scan.address)) |
| 2361 | break; |
| 2362 | } fallthrough; |
| 2363 | case SCAN_SUCCEED: |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2364 | ++khugepaged_pages_collapsed; |
Zach O'Keefe | 58ac9a8 | 2022-09-22 15:40:38 -0700 | [diff] [blame] | 2365 | break; |
| 2366 | default: |
| 2367 | break; |
| 2368 | } |
| 2369 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2370 | /* move to next address */ |
| 2371 | khugepaged_scan.address += HPAGE_PMD_SIZE; |
| 2372 | progress += HPAGE_PMD_NR; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2373 | if (!mmap_locked) |
| 2374 | /* |
| 2375 | * We released mmap_lock so break loop. Note |
| 2376 | * that we drop mmap_lock before all hugepage |
| 2377 | * allocations, so if allocation fails, we are |
| 2378 | * guaranteed to break here and report the |
| 2379 | * correct result back to caller. |
| 2380 | */ |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2381 | goto breakouterloop_mmap_lock; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2382 | if (progress >= pages) |
| 2383 | goto breakouterloop; |
| 2384 | } |
| 2385 | } |
| 2386 | breakouterloop: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2387 | mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */ |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2388 | breakouterloop_mmap_lock: |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2389 | |
| 2390 | spin_lock(&khugepaged_mm_lock); |
| 2391 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); |
| 2392 | /* |
| 2393 | * Release the current mm_slot if this mm is about to die, or |
| 2394 | * if we scanned all vmas of this mm. |
| 2395 | */ |
Zach O'Keefe | 7d2c438 | 2022-07-06 16:59:28 -0700 | [diff] [blame] | 2396 | if (hpage_collapse_test_exit(mm) || !vma) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2397 | /* |
| 2398 | * Make sure that if mm_users is reaching zero while |
| 2399 | * khugepaged runs here, khugepaged_exit will find |
| 2400 | * mm_slot not pointing to the exiting mm. |
| 2401 | */ |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2402 | if (slot->mm_node.next != &khugepaged_scan.mm_head) { |
| 2403 | slot = list_entry(slot->mm_node.next, |
| 2404 | struct mm_slot, mm_node); |
| 2405 | khugepaged_scan.mm_slot = |
| 2406 | mm_slot_entry(slot, struct khugepaged_mm_slot, slot); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2407 | khugepaged_scan.address = 0; |
| 2408 | } else { |
| 2409 | khugepaged_scan.mm_slot = NULL; |
| 2410 | khugepaged_full_scans++; |
| 2411 | } |
| 2412 | |
| 2413 | collect_mm_slot(mm_slot); |
| 2414 | } |
| 2415 | |
| 2416 | return progress; |
| 2417 | } |
| 2418 | |
| 2419 | static int khugepaged_has_work(void) |
| 2420 | { |
| 2421 | return !list_empty(&khugepaged_scan.mm_head) && |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 2422 | hugepage_flags_enabled(); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | static int khugepaged_wait_event(void) |
| 2426 | { |
| 2427 | return !list_empty(&khugepaged_scan.mm_head) || |
| 2428 | kthread_should_stop(); |
| 2429 | } |
| 2430 | |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 2431 | static void khugepaged_do_scan(struct collapse_control *cc) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2432 | { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2433 | unsigned int progress = 0, pass_through_head = 0; |
Yanfei Xu | 89dc6a9 | 2021-05-04 18:34:12 -0700 | [diff] [blame] | 2434 | unsigned int pages = READ_ONCE(khugepaged_pages_to_scan); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2435 | bool wait = true; |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2436 | int result = SCAN_SUCCEED; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2437 | |
Kirill A. Shutemov | a980df3 | 2020-06-03 16:00:12 -0700 | [diff] [blame] | 2438 | lru_add_drain_all(); |
| 2439 | |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 2440 | while (true) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2441 | cond_resched(); |
| 2442 | |
| 2443 | if (unlikely(kthread_should_stop() || try_to_freeze())) |
| 2444 | break; |
| 2445 | |
| 2446 | spin_lock(&khugepaged_mm_lock); |
| 2447 | if (!khugepaged_scan.mm_slot) |
| 2448 | pass_through_head++; |
| 2449 | if (khugepaged_has_work() && |
| 2450 | pass_through_head < 2) |
| 2451 | progress += khugepaged_scan_mm_slot(pages - progress, |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2452 | &result, cc); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2453 | else |
| 2454 | progress = pages; |
| 2455 | spin_unlock(&khugepaged_mm_lock); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2456 | |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 2457 | if (progress >= pages) |
| 2458 | break; |
| 2459 | |
Zach O'Keefe | 50ad2f2 | 2022-07-06 16:59:23 -0700 | [diff] [blame] | 2460 | if (result == SCAN_ALLOC_HUGE_PAGE_FAIL) { |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 2461 | /* |
| 2462 | * If fail to allocate the first time, try to sleep for |
| 2463 | * a while. When hit again, cancel the scan. |
| 2464 | */ |
| 2465 | if (!wait) |
| 2466 | break; |
| 2467 | wait = false; |
Yang Shi | c6a7f44 | 2022-07-06 16:59:20 -0700 | [diff] [blame] | 2468 | khugepaged_alloc_sleep(); |
| 2469 | } |
| 2470 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static bool khugepaged_should_wakeup(void) |
| 2474 | { |
| 2475 | return kthread_should_stop() || |
| 2476 | time_after_eq(jiffies, khugepaged_sleep_expire); |
| 2477 | } |
| 2478 | |
| 2479 | static void khugepaged_wait_work(void) |
| 2480 | { |
| 2481 | if (khugepaged_has_work()) { |
| 2482 | const unsigned long scan_sleep_jiffies = |
| 2483 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs); |
| 2484 | |
| 2485 | if (!scan_sleep_jiffies) |
| 2486 | return; |
| 2487 | |
| 2488 | khugepaged_sleep_expire = jiffies + scan_sleep_jiffies; |
| 2489 | wait_event_freezable_timeout(khugepaged_wait, |
| 2490 | khugepaged_should_wakeup(), |
| 2491 | scan_sleep_jiffies); |
| 2492 | return; |
| 2493 | } |
| 2494 | |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 2495 | if (hugepage_flags_enabled()) |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2496 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); |
| 2497 | } |
| 2498 | |
| 2499 | static int khugepaged(void *none) |
| 2500 | { |
Qi Zheng | b26e270 | 2022-08-31 11:19:46 +0800 | [diff] [blame] | 2501 | struct khugepaged_mm_slot *mm_slot; |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2502 | |
| 2503 | set_freezable(); |
| 2504 | set_user_nice(current, MAX_NICE); |
| 2505 | |
| 2506 | while (!kthread_should_stop()) { |
Zach O'Keefe | 34d6b47 | 2022-07-06 16:59:21 -0700 | [diff] [blame] | 2507 | khugepaged_do_scan(&khugepaged_collapse_control); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2508 | khugepaged_wait_work(); |
| 2509 | } |
| 2510 | |
| 2511 | spin_lock(&khugepaged_mm_lock); |
| 2512 | mm_slot = khugepaged_scan.mm_slot; |
| 2513 | khugepaged_scan.mm_slot = NULL; |
| 2514 | if (mm_slot) |
| 2515 | collect_mm_slot(mm_slot); |
| 2516 | spin_unlock(&khugepaged_mm_lock); |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
| 2520 | static void set_recommended_min_free_kbytes(void) |
| 2521 | { |
| 2522 | struct zone *zone; |
| 2523 | int nr_zones = 0; |
| 2524 | unsigned long recommended_min; |
| 2525 | |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 2526 | if (!hugepage_flags_enabled()) { |
Liangcai Fan | bd3400e | 2021-11-05 13:41:36 -0700 | [diff] [blame] | 2527 | calculate_min_free_kbytes(); |
| 2528 | goto update_wmarks; |
| 2529 | } |
| 2530 | |
Joonsoo Kim | b7d349c | 2018-04-10 16:30:27 -0700 | [diff] [blame] | 2531 | for_each_populated_zone(zone) { |
| 2532 | /* |
| 2533 | * We don't need to worry about fragmentation of |
| 2534 | * ZONE_MOVABLE since it only has movable pages. |
| 2535 | */ |
| 2536 | if (zone_idx(zone) > gfp_zone(GFP_USER)) |
| 2537 | continue; |
| 2538 | |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2539 | nr_zones++; |
Joonsoo Kim | b7d349c | 2018-04-10 16:30:27 -0700 | [diff] [blame] | 2540 | } |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2541 | |
| 2542 | /* Ensure 2 pageblocks are free to assist fragmentation avoidance */ |
| 2543 | recommended_min = pageblock_nr_pages * nr_zones * 2; |
| 2544 | |
| 2545 | /* |
| 2546 | * Make sure that on average at least two pageblocks are almost free |
| 2547 | * of another type, one for a migratetype to fall back to and a |
| 2548 | * second to avoid subsequent fallbacks of other types There are 3 |
| 2549 | * MIGRATE_TYPES we care about. |
| 2550 | */ |
| 2551 | recommended_min += pageblock_nr_pages * nr_zones * |
| 2552 | MIGRATE_PCPTYPES * MIGRATE_PCPTYPES; |
| 2553 | |
| 2554 | /* don't ever allow to reserve more than 5% of the lowmem */ |
| 2555 | recommended_min = min(recommended_min, |
| 2556 | (unsigned long) nr_free_buffer_pages() / 20); |
| 2557 | recommended_min <<= (PAGE_SHIFT-10); |
| 2558 | |
| 2559 | if (recommended_min > min_free_kbytes) { |
| 2560 | if (user_min_free_kbytes >= 0) |
| 2561 | pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n", |
| 2562 | min_free_kbytes, recommended_min); |
| 2563 | |
| 2564 | min_free_kbytes = recommended_min; |
| 2565 | } |
Liangcai Fan | bd3400e | 2021-11-05 13:41:36 -0700 | [diff] [blame] | 2566 | |
| 2567 | update_wmarks: |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2568 | setup_per_zone_wmarks(); |
| 2569 | } |
| 2570 | |
| 2571 | int start_stop_khugepaged(void) |
| 2572 | { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2573 | int err = 0; |
| 2574 | |
| 2575 | mutex_lock(&khugepaged_mutex); |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 2576 | if (hugepage_flags_enabled()) { |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2577 | if (!khugepaged_thread) |
| 2578 | khugepaged_thread = kthread_run(khugepaged, NULL, |
| 2579 | "khugepaged"); |
| 2580 | if (IS_ERR(khugepaged_thread)) { |
| 2581 | pr_err("khugepaged: kthread_run(khugepaged) failed\n"); |
| 2582 | err = PTR_ERR(khugepaged_thread); |
| 2583 | khugepaged_thread = NULL; |
| 2584 | goto fail; |
| 2585 | } |
| 2586 | |
| 2587 | if (!list_empty(&khugepaged_scan.mm_head)) |
| 2588 | wake_up_interruptible(&khugepaged_wait); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2589 | } else if (khugepaged_thread) { |
| 2590 | kthread_stop(khugepaged_thread); |
| 2591 | khugepaged_thread = NULL; |
| 2592 | } |
Liangcai Fan | bd3400e | 2021-11-05 13:41:36 -0700 | [diff] [blame] | 2593 | set_recommended_min_free_kbytes(); |
Kirill A. Shutemov | b46e756 | 2016-07-26 15:26:24 -0700 | [diff] [blame] | 2594 | fail: |
| 2595 | mutex_unlock(&khugepaged_mutex); |
| 2596 | return err; |
| 2597 | } |
Vijay Balakrishna | 4aab2be | 2020-10-10 23:16:40 -0700 | [diff] [blame] | 2598 | |
| 2599 | void khugepaged_min_free_kbytes_update(void) |
| 2600 | { |
| 2601 | mutex_lock(&khugepaged_mutex); |
Yang Shi | 1064026 | 2022-06-16 10:48:39 -0700 | [diff] [blame] | 2602 | if (hugepage_flags_enabled() && khugepaged_thread) |
Vijay Balakrishna | 4aab2be | 2020-10-10 23:16:40 -0700 | [diff] [blame] | 2603 | set_recommended_min_free_kbytes(); |
| 2604 | mutex_unlock(&khugepaged_mutex); |
| 2605 | } |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2606 | |
Johannes Weiner | 57e9cc5 | 2022-10-26 14:01:33 -0400 | [diff] [blame] | 2607 | bool current_is_khugepaged(void) |
| 2608 | { |
| 2609 | return kthread_func(current) == khugepaged; |
| 2610 | } |
| 2611 | |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2612 | static int madvise_collapse_errno(enum scan_result r) |
| 2613 | { |
| 2614 | /* |
| 2615 | * MADV_COLLAPSE breaks from existing madvise(2) conventions to provide |
| 2616 | * actionable feedback to caller, so they may take an appropriate |
| 2617 | * fallback measure depending on the nature of the failure. |
| 2618 | */ |
| 2619 | switch (r) { |
| 2620 | case SCAN_ALLOC_HUGE_PAGE_FAIL: |
| 2621 | return -ENOMEM; |
| 2622 | case SCAN_CGROUP_CHARGE_FAIL: |
| 2623 | return -EBUSY; |
| 2624 | /* Resource temporary unavailable - trying again might succeed */ |
Zach O'Keefe | ae63c89 | 2023-01-24 17:57:37 -0800 | [diff] [blame] | 2625 | case SCAN_PAGE_COUNT: |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2626 | case SCAN_PAGE_LOCK: |
| 2627 | case SCAN_PAGE_LRU: |
Zach O'Keefe | 0f3e2a2 | 2022-09-22 11:46:50 -0700 | [diff] [blame] | 2628 | case SCAN_DEL_PAGE_LRU: |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2629 | return -EAGAIN; |
| 2630 | /* |
| 2631 | * Other: Trying again likely not to succeed / error intrinsic to |
| 2632 | * specified memory range. khugepaged likely won't be able to collapse |
| 2633 | * either. |
| 2634 | */ |
| 2635 | default: |
| 2636 | return -EINVAL; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev, |
| 2641 | unsigned long start, unsigned long end) |
| 2642 | { |
| 2643 | struct collapse_control *cc; |
| 2644 | struct mm_struct *mm = vma->vm_mm; |
| 2645 | unsigned long hstart, hend, addr; |
| 2646 | int thps = 0, last_fail = SCAN_FAIL; |
| 2647 | bool mmap_locked = true; |
| 2648 | |
| 2649 | BUG_ON(vma->vm_start > start); |
| 2650 | BUG_ON(vma->vm_end < end); |
| 2651 | |
| 2652 | *prev = vma; |
| 2653 | |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2654 | if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false)) |
| 2655 | return -EINVAL; |
| 2656 | |
| 2657 | cc = kmalloc(sizeof(*cc), GFP_KERNEL); |
| 2658 | if (!cc) |
| 2659 | return -ENOMEM; |
| 2660 | cc->is_khugepaged = false; |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2661 | |
| 2662 | mmgrab(mm); |
| 2663 | lru_add_drain_all(); |
| 2664 | |
| 2665 | hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2666 | hend = end & HPAGE_PMD_MASK; |
| 2667 | |
| 2668 | for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) { |
| 2669 | int result = SCAN_FAIL; |
| 2670 | |
| 2671 | if (!mmap_locked) { |
| 2672 | cond_resched(); |
| 2673 | mmap_read_lock(mm); |
| 2674 | mmap_locked = true; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2675 | result = hugepage_vma_revalidate(mm, addr, false, &vma, |
| 2676 | cc); |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2677 | if (result != SCAN_SUCCEED) { |
| 2678 | last_fail = result; |
| 2679 | goto out_nolock; |
| 2680 | } |
Yang Shi | 4d24de9 | 2022-09-14 09:22:20 -0700 | [diff] [blame] | 2681 | |
Zach O'Keefe | 52dc031 | 2022-12-24 00:20:34 -0800 | [diff] [blame] | 2682 | hend = min(hend, vma->vm_end & HPAGE_PMD_MASK); |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2683 | } |
| 2684 | mmap_assert_locked(mm); |
| 2685 | memset(cc->node_load, 0, sizeof(cc->node_load)); |
Yang Shi | e031ff9 | 2022-11-08 10:43:56 -0800 | [diff] [blame] | 2686 | nodes_clear(cc->alloc_nmask); |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2687 | if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) { |
| 2688 | struct file *file = get_file(vma->vm_file); |
| 2689 | pgoff_t pgoff = linear_page_index(vma, addr); |
| 2690 | |
| 2691 | mmap_read_unlock(mm); |
| 2692 | mmap_locked = false; |
| 2693 | result = hpage_collapse_scan_file(mm, addr, file, pgoff, |
| 2694 | cc); |
| 2695 | fput(file); |
| 2696 | } else { |
| 2697 | result = hpage_collapse_scan_pmd(mm, vma, addr, |
| 2698 | &mmap_locked, cc); |
| 2699 | } |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2700 | if (!mmap_locked) |
| 2701 | *prev = NULL; /* Tell caller we dropped mmap_lock */ |
| 2702 | |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2703 | handle_result: |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2704 | switch (result) { |
| 2705 | case SCAN_SUCCEED: |
| 2706 | case SCAN_PMD_MAPPED: |
| 2707 | ++thps; |
| 2708 | break; |
Zach O'Keefe | 3448839 | 2022-09-22 15:40:39 -0700 | [diff] [blame] | 2709 | case SCAN_PTE_MAPPED_HUGEPAGE: |
| 2710 | BUG_ON(mmap_locked); |
| 2711 | BUG_ON(*prev); |
| 2712 | mmap_write_lock(mm); |
| 2713 | result = collapse_pte_mapped_thp(mm, addr, true); |
| 2714 | mmap_write_unlock(mm); |
| 2715 | goto handle_result; |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2716 | /* Whitelisted set of results where continuing OK */ |
| 2717 | case SCAN_PMD_NULL: |
| 2718 | case SCAN_PTE_NON_PRESENT: |
| 2719 | case SCAN_PTE_UFFD_WP: |
| 2720 | case SCAN_PAGE_RO: |
| 2721 | case SCAN_LACK_REFERENCED_PAGE: |
| 2722 | case SCAN_PAGE_NULL: |
| 2723 | case SCAN_PAGE_COUNT: |
| 2724 | case SCAN_PAGE_LOCK: |
| 2725 | case SCAN_PAGE_COMPOUND: |
| 2726 | case SCAN_PAGE_LRU: |
Zach O'Keefe | 0f3e2a2 | 2022-09-22 11:46:50 -0700 | [diff] [blame] | 2727 | case SCAN_DEL_PAGE_LRU: |
Zach O'Keefe | 7d8faaf | 2022-07-06 16:59:27 -0700 | [diff] [blame] | 2728 | last_fail = result; |
| 2729 | break; |
| 2730 | default: |
| 2731 | last_fail = result; |
| 2732 | /* Other error, exit */ |
| 2733 | goto out_maybelock; |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | out_maybelock: |
| 2738 | /* Caller expects us to hold mmap_lock on return */ |
| 2739 | if (!mmap_locked) |
| 2740 | mmap_read_lock(mm); |
| 2741 | out_nolock: |
| 2742 | mmap_assert_locked(mm); |
| 2743 | mmdrop(mm); |
| 2744 | kfree(cc); |
| 2745 | |
| 2746 | return thps == ((hend - hstart) >> HPAGE_PMD_SHIFT) ? 0 |
| 2747 | : madvise_collapse_errno(last_fail); |
| 2748 | } |