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