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