| 1 | |
| 2 | |
| 3 | * Copyright (C) 2009 Red Hat, Inc.
|
| 4 |
|
| 5 |
|
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
| 7 |
|
| 8 | #include <linux/mm.h>
|
| 9 | #include <linux/sched.h>
|
| 10 | #include <linux/sched/mm.h>
|
| 11 | #include <linux/sched/numa_balancing.h>
|
| 12 | #include <linux/highmem.h>
|
| 13 | #include <linux/hugetlb.h>
|
| 14 | #include <linux/mmu_notifier.h>
|
| 15 | #include <linux/rmap.h>
|
| 16 | #include <linux/swap.h>
|
| 17 | #include <linux/list_lru.h>
|
| 18 | #include <linux/shrinker.h>
|
| 19 | #include <linux/mm_inline.h>
|
| 20 | #include <linux/swapops.h>
|
| 21 | #include <linux/backing-dev.h>
|
| 22 | #include <linux/dax.h>
|
| 23 | #include <linux/mm_types.h>
|
| 24 | #include <linux/khugepaged.h>
|
| 25 | #include <linux/freezer.h>
|
| 26 | #include <linux/mman.h>
|
| 27 | #include <linux/memremap.h>
|
| 28 | #include <linux/pagemap.h>
|
| 29 | #include <linux/debugfs.h>
|
| 30 | #include <linux/migrate.h>
|
| 31 | #include <linux/hashtable.h>
|
| 32 | #include <linux/userfaultfd_k.h>
|
| 33 | #include <linux/page_idle.h>
|
| 34 | #include <linux/shmem_fs.h>
|
| 35 | #include <linux/oom.h>
|
| 36 | #include <linux/numa.h>
|
| 37 | #include <linux/page_owner.h>
|
| 38 | #include <linux/sched/sysctl.h>
|
| 39 | #include <linux/memory-tiers.h>
|
| 40 | #include <linux/compat.h>
|
| 41 | #include <linux/pgalloc.h>
|
| 42 | #include <linux/pgalloc_tag.h>
|
| 43 | #include <linux/pagewalk.h>
|
| 44 | #include <linux/cleanup.h>
|
| 45 |
|
| 46 | #include <asm/tlb.h>
|
| 47 | #include "internal.h"
|
| 48 | #include "swap.h"
|
| 49 |
|
| 50 | #define CREATE_TRACE_POINTS
|
| 51 | #include <trace/events/thp.h>
|
| 52 |
|
| 53 | |
| 54 | * By default, transparent hugepage support is disabled in order to avoid
|
| 55 | * risking an increased memory footprint for applications that are not
|
| 56 | * guaranteed to benefit from it. When transparent hugepage support is
|
| 57 | * enabled, it is for all mappings, and khugepaged scans all mappings.
|
| 58 | * Defrag is invoked by khugepaged hugepage allocations and by page faults
|
| 59 | * for all hugepage allocations.
|
| 60 |
|
| 61 | unsigned long transparent_hugepage_flags __read_mostly =
|
| 62 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
|
| 63 | (1<<TRANSPARENT_HUGEPAGE_FLAG)|
|
| 64 | #endif
|
| 65 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
|
| 66 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
|
| 67 | #endif
|
| 68 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
|
| 69 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
|
| 70 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
|
| 71 |
|
| 72 | static struct lock_class_key deferred_split_key;
|
| 73 | static struct list_lru deferred_split_lru;
|
| 74 | static struct shrinker *deferred_split_shrinker;
|
| 75 | static unsigned long deferred_split_count(struct shrinker *shrink,
|
| 76 | struct shrink_control *sc);
|
| 77 | static unsigned long deferred_split_scan(struct shrinker *shrink,
|
| 78 | struct shrink_control *sc);
|
| 79 | static bool split_underused_thp = true;
|
| 80 |
|
| 81 | #define HUGE_ZERO_UNSET_PFN (~0UL)
|
| 82 | struct folio *huge_zero_folio __read_mostly;
|
| 83 | unsigned long huge_zero_pfn __read_mostly = HUGE_ZERO_UNSET_PFN;
|
| 84 | #ifndef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO
|
| 85 | static atomic_t huge_zero_refcount;
|
| 86 | static DEFINE_SPINLOCK(huge_zero_lock);
|
| 87 | static struct shrinker *huge_zero_folio_shrinker;
|
| 88 | #endif
|
| 89 |
|
| 90 | unsigned long huge_anon_orders_always __read_mostly;
|
| 91 | unsigned long huge_anon_orders_madvise __read_mostly;
|
| 92 | unsigned long huge_anon_orders_inherit __read_mostly;
|
| 93 | static bool anon_orders_configured __initdata;
|
| 94 |
|
| 95 | static inline bool file_thp_enabled(const struct vm_area_struct *vma)
|
| 96 | {
|
| 97 | struct inode *inode;
|
| 98 |
|
| 99 | if (!vma->vm_file)
|
| 100 | return false;
|
| 101 |
|
| 102 | inode = file_inode(vma->vm_file);
|
| 103 |
|
| 104 | if (IS_ANON_FILE(inode))
|
| 105 | return false;
|
| 106 |
|
| 107 | if (!mapping_pmd_folio_support(vma->vm_file->f_mapping))
|
| 108 | return false;
|
| 109 |
|
| 110 | return S_ISREG(inode->i_mode);
|
| 111 | }
|
| 112 |
|
| 113 |
|
| 114 | static bool vma_is_special_huge(const struct vm_area_struct *vma)
|
| 115 | {
|
| 116 | if (vma_is_dax(vma))
|
| 117 | return false;
|
| 118 | return vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT);
|
| 119 | }
|
| 120 |
|
| 121 | static bool vma_file_bypass_thp_tuneables(const struct vm_area_struct *vma,
|
| 122 | enum tva_type type)
|
| 123 | {
|
| 124 | const bool has_huge_fault = vma->vm_ops->huge_fault;
|
| 125 |
|
| 126 |
|
| 127 | if (type == TVA_FORCED_COLLAPSE)
|
| 128 | return true;
|
| 129 |
|
| 130 | if (vma_test(vma, VMA_PFNMAP_BIT) && has_huge_fault)
|
| 131 | return true;
|
| 132 | return false;
|
| 133 | }
|
| 134 |
|
| 135 | static bool vma_file_allow_thp_tuneables(vm_flags_t vm_flags)
|
| 136 | {
|
| 137 |
|
| 138 | if (hugepage_global_always())
|
| 139 | return true;
|
| 140 |
|
| 141 | if (hugepage_global_enabled() && (vm_flags & VM_HUGEPAGE))
|
| 142 | return true;
|
| 143 | return false;
|
| 144 | }
|
| 145 |
|
| 146 | static bool vma_file_check_thp_tuneables(const struct vm_area_struct *vma,
|
| 147 | vm_flags_t vm_flags, enum tva_type type)
|
| 148 | {
|
| 149 | return vma_file_bypass_thp_tuneables(vma, type) ||
|
| 150 | vma_file_allow_thp_tuneables(vm_flags);
|
| 151 | }
|
| 152 |
|
| 153 | static bool vma_can_map_huge_file(const struct vm_area_struct *vma,
|
| 154 | vm_flags_t vm_flags, enum tva_type type)
|
| 155 | {
|
| 156 | const bool has_huge_fault = vma->vm_ops->huge_fault;
|
| 157 |
|
| 158 | |
| 159 | * Enforce THP collapse requirements as necessary. Anonymous vmas
|
| 160 | * were already handled in thp_vma_allowable_orders().
|
| 161 |
|
| 162 | if (!vma_file_check_thp_tuneables(vma, vm_flags, type))
|
| 163 | return false;
|
| 164 |
|
| 165 | switch (type) {
|
| 166 | case TVA_PAGEFAULT:
|
| 167 | |
| 168 | * Trust that ->huge_fault() handlers know what they are doing
|
| 169 | * in fault path.
|
| 170 |
|
| 171 | return has_huge_fault;
|
| 172 | case TVA_SMAPS:
|
| 173 | if (has_huge_fault)
|
| 174 | return true;
|
| 175 | fallthrough;
|
| 176 | default:
|
| 177 |
|
| 178 | return file_thp_enabled(vma);
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
|
| 183 | vm_flags_t vm_flags,
|
| 184 | enum tva_type type,
|
| 185 | unsigned long orders)
|
| 186 | {
|
| 187 | const bool smaps = type == TVA_SMAPS;
|
| 188 | const bool in_pf = type == TVA_PAGEFAULT;
|
| 189 | const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
|
| 190 | unsigned long supported_orders;
|
| 191 |
|
| 192 |
|
| 193 | if (vma_is_anonymous(vma))
|
| 194 | supported_orders = THP_ORDERS_ALL_ANON;
|
| 195 | else if (vma_is_dax(vma) || vma_is_special_huge(vma))
|
| 196 | supported_orders = THP_ORDERS_ALL_SPECIAL_DAX;
|
| 197 | else
|
| 198 | supported_orders = THP_ORDERS_ALL_FILE_DEFAULT;
|
| 199 |
|
| 200 | orders &= supported_orders;
|
| 201 | if (!orders)
|
| 202 | return 0;
|
| 203 |
|
| 204 | if (!vma->vm_mm)
|
| 205 | return 0;
|
| 206 |
|
| 207 | if (thp_disabled_by_hw() || vma_thp_disabled(vma, vm_flags, forced_collapse))
|
| 208 | return 0;
|
| 209 |
|
| 210 |
|
| 211 | if (vma_is_dax(vma))
|
| 212 | return in_pf ? orders : 0;
|
| 213 |
|
| 214 | |
| 215 | * khugepaged special VMA and hugetlb VMA.
|
| 216 | * Must be checked after dax since some dax mappings may have
|
| 217 | * VM_MIXEDMAP set.
|
| 218 |
|
| 219 | if (!in_pf && !smaps && (vm_flags & VM_NO_KHUGEPAGED))
|
| 220 | return 0;
|
| 221 |
|
| 222 | |
| 223 | * Check alignment for file vma and size for both file and anon vma by
|
| 224 | * filtering out the unsuitable orders.
|
| 225 | *
|
| 226 | * Skip the check for page fault. Huge fault does the check in fault
|
| 227 | * handlers.
|
| 228 |
|
| 229 | if (!in_pf) {
|
| 230 | int order = highest_order(orders);
|
| 231 | unsigned long addr;
|
| 232 |
|
| 233 | while (orders) {
|
| 234 | addr = vma->vm_end - (PAGE_SIZE << order);
|
| 235 | if (thp_vma_suitable_order(vma, addr, order))
|
| 236 | break;
|
| 237 | order = next_order(&orders, order);
|
| 238 | }
|
| 239 |
|
| 240 | if (!orders)
|
| 241 | return 0;
|
| 242 | }
|
| 243 |
|
| 244 | |
| 245 | * Enabled via shmem mount options or sysfs settings.
|
| 246 | * Must be done before hugepage flags check since shmem has its
|
| 247 | * own flags.
|
| 248 |
|
| 249 | if (!in_pf && shmem_file(vma->vm_file))
|
| 250 | return orders & shmem_allowable_huge_orders(file_inode(vma->vm_file),
|
| 251 | vma, vma_start_pgoff(vma), 0,
|
| 252 | forced_collapse);
|
| 253 |
|
| 254 | if (!vma_is_anonymous(vma))
|
| 255 | return vma_can_map_huge_file(vma, vm_flags, type) ? orders : 0;
|
| 256 |
|
| 257 | if (vma_is_temporary_stack(vma))
|
| 258 | return 0;
|
| 259 |
|
| 260 | |
| 261 | * THPeligible bit of smaps should show 1 for proper VMAs even
|
| 262 | * though anon_vma is not initialized yet.
|
| 263 | *
|
| 264 | * Allow page fault since anon_vma may be not initialized until
|
| 265 | * the first page fault.
|
| 266 |
|
| 267 | if (!vma->anon_vma)
|
| 268 | return (smaps || in_pf) ? orders : 0;
|
| 269 |
|
| 270 | return orders;
|
| 271 | }
|
| 272 |
|
| 273 | static struct folio *alloc_huge_zero_folio(void)
|
| 274 | {
|
| 275 | struct folio *zero_folio;
|
| 276 |
|
| 277 | zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
|
| 278 | ~__GFP_MOVABLE,
|
| 279 | HPAGE_PMD_ORDER);
|
| 280 | if (!zero_folio) {
|
| 281 | count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
|
| 282 | return NULL;
|
| 283 | }
|
| 284 | folio_clear_large_rmappable(zero_folio);
|
| 285 | return zero_folio;
|
| 286 | }
|
| 287 |
|
| 288 | #ifdef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO
|
| 289 | static int __init huge_zero_init(void)
|
| 290 | {
|
| 291 | huge_zero_folio = alloc_huge_zero_folio();
|
| 292 | if (!huge_zero_folio) {
|
| 293 | pr_warn("Allocating persistent huge zero folio failed\n");
|
| 294 | } else {
|
| 295 | huge_zero_pfn = folio_pfn(huge_zero_folio);
|
| 296 | count_vm_event(THP_ZERO_PAGE_ALLOC);
|
| 297 | }
|
| 298 | return 0;
|
| 299 | }
|
| 300 |
|
| 301 | static void __init huge_zero_shrinker_exit(void)
|
| 302 | {
|
| 303 | }
|
| 304 |
|
| 305 | struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
|
| 306 | {
|
| 307 | return huge_zero_folio;
|
| 308 | }
|
| 309 |
|
| 310 | void mm_put_huge_zero_folio(struct mm_struct *mm)
|
| 311 | {
|
| 312 | }
|
| 313 | #else
|
| 314 | static bool get_huge_zero_folio(void)
|
| 315 | {
|
| 316 | struct folio *zero_folio;
|
| 317 |
|
| 318 |
|
| 319 | if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
|
| 320 | return true;
|
| 321 |
|
| 322 | zero_folio = alloc_huge_zero_folio();
|
| 323 | if (unlikely(!zero_folio))
|
| 324 | return false;
|
| 325 |
|
| 326 |
|
| 327 | spin_lock(&huge_zero_lock);
|
| 328 | if (huge_zero_folio) {
|
| 329 |
|
| 330 | atomic_inc(&huge_zero_refcount);
|
| 331 | spin_unlock(&huge_zero_lock);
|
| 332 | folio_put(zero_folio);
|
| 333 | return true;
|
| 334 | }
|
| 335 | WRITE_ONCE(huge_zero_folio, zero_folio);
|
| 336 | WRITE_ONCE(huge_zero_pfn, folio_pfn(zero_folio));
|
| 337 |
|
| 338 | atomic_set_release(&huge_zero_refcount, 2);
|
| 339 | spin_unlock(&huge_zero_lock);
|
| 340 |
|
| 341 | count_vm_event(THP_ZERO_PAGE_ALLOC);
|
| 342 | return true;
|
| 343 | }
|
| 344 |
|
| 345 | static void put_huge_zero_folio(void)
|
| 346 | {
|
| 347 | |
| 348 | * Counter should never go to zero here. Only shrinker can put
|
| 349 | * last reference.
|
| 350 |
|
| 351 | WARN_ON_ONCE(atomic_dec_and_test(&huge_zero_refcount));
|
| 352 | }
|
| 353 |
|
| 354 | static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,
|
| 355 | struct shrink_control *sc)
|
| 356 | {
|
| 357 |
|
| 358 | return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
|
| 359 | }
|
| 360 |
|
| 361 | static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
|
| 362 | struct shrink_control *sc)
|
| 363 | {
|
| 364 | struct folio *zero_folio;
|
| 365 |
|
| 366 |
|
| 367 | scoped_guard(spinlock, &huge_zero_lock) {
|
| 368 |
|
| 369 | if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) != 1)
|
| 370 | return 0;
|
| 371 |
|
| 372 | zero_folio = huge_zero_folio;
|
| 373 | VM_WARN_ON_ONCE(!zero_folio);
|
| 374 | WRITE_ONCE(huge_zero_folio, NULL);
|
| 375 | WRITE_ONCE(huge_zero_pfn, HUGE_ZERO_UNSET_PFN);
|
| 376 | }
|
| 377 |
|
| 378 | folio_put(zero_folio);
|
| 379 | return HPAGE_PMD_NR;
|
| 380 | }
|
| 381 |
|
| 382 | static int __init huge_zero_init(void)
|
| 383 | {
|
| 384 | huge_zero_folio_shrinker = shrinker_alloc(0, "thp-zero");
|
| 385 | if (!huge_zero_folio_shrinker) {
|
| 386 | shrinker_free(deferred_split_shrinker);
|
| 387 | list_lru_destroy(&deferred_split_lru);
|
| 388 | return -ENOMEM;
|
| 389 | }
|
| 390 |
|
| 391 | huge_zero_folio_shrinker->count_objects = shrink_huge_zero_folio_count;
|
| 392 | huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan;
|
| 393 | shrinker_register(huge_zero_folio_shrinker);
|
| 394 | return 0;
|
| 395 | }
|
| 396 |
|
| 397 | static void __init huge_zero_shrinker_exit(void)
|
| 398 | {
|
| 399 | shrinker_free(huge_zero_folio_shrinker);
|
| 400 | }
|
| 401 |
|
| 402 | struct folio *mm_get_huge_zero_folio(struct mm_struct *mm)
|
| 403 | {
|
| 404 | if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
|
| 405 | return READ_ONCE(huge_zero_folio);
|
| 406 |
|
| 407 | if (!get_huge_zero_folio())
|
| 408 | return NULL;
|
| 409 |
|
| 410 | if (mm_flags_test_and_set(MMF_HUGE_ZERO_FOLIO, mm))
|
| 411 | put_huge_zero_folio();
|
| 412 |
|
| 413 | return READ_ONCE(huge_zero_folio);
|
| 414 | }
|
| 415 |
|
| 416 | void mm_put_huge_zero_folio(struct mm_struct *mm)
|
| 417 | {
|
| 418 | if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm))
|
| 419 | put_huge_zero_folio();
|
| 420 | }
|
| 421 | #endif
|
| 422 |
|
| 423 | #ifdef CONFIG_SYSFS
|
| 424 | static ssize_t enabled_show(struct kobject *kobj,
|
| 425 | struct kobj_attribute *attr, char *buf)
|
| 426 | {
|
| 427 | const char *output;
|
| 428 |
|
| 429 | if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
|
| 430 | output = "[always] madvise never";
|
| 431 | else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
|
| 432 | &transparent_hugepage_flags))
|
| 433 | output = "always [madvise] never";
|
| 434 | else
|
| 435 | output = "always madvise [never]";
|
| 436 |
|
| 437 | return sysfs_emit(buf, "%s\n", output);
|
| 438 | }
|
| 439 |
|
| 440 | enum anon_enabled_mode {
|
| 441 | ANON_ENABLED_ALWAYS = 0,
|
| 442 | ANON_ENABLED_INHERIT = 1,
|
| 443 | ANON_ENABLED_MADVISE = 2,
|
| 444 | ANON_ENABLED_NEVER = 3,
|
| 445 | };
|
| 446 |
|
| 447 | static const char * const anon_enabled_mode_strings[] = {
|
| 448 | [ANON_ENABLED_ALWAYS] = "always",
|
| 449 | [ANON_ENABLED_INHERIT] = "inherit",
|
| 450 | [ANON_ENABLED_MADVISE] = "madvise",
|
| 451 | [ANON_ENABLED_NEVER] = "never",
|
| 452 | };
|
| 453 |
|
| 454 | enum global_enabled_mode {
|
| 455 | GLOBAL_ENABLED_ALWAYS = 0,
|
| 456 | GLOBAL_ENABLED_MADVISE = 1,
|
| 457 | GLOBAL_ENABLED_NEVER = 2,
|
| 458 | };
|
| 459 |
|
| 460 | static const char * const global_enabled_mode_strings[] = {
|
| 461 | [GLOBAL_ENABLED_ALWAYS] = "always",
|
| 462 | [GLOBAL_ENABLED_MADVISE] = "madvise",
|
| 463 | [GLOBAL_ENABLED_NEVER] = "never",
|
| 464 | };
|
| 465 |
|
| 466 | static bool set_global_enabled_mode(enum global_enabled_mode mode)
|
| 467 | {
|
| 468 | static const unsigned long thp_flags[] = {
|
| 469 | TRANSPARENT_HUGEPAGE_FLAG,
|
| 470 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
|
| 471 | };
|
| 472 | enum global_enabled_mode m;
|
| 473 | bool changed = false;
|
| 474 |
|
| 475 | for (m = 0; m < ARRAY_SIZE(thp_flags); m++) {
|
| 476 | if (m == mode)
|
| 477 | changed |= !test_and_set_bit(thp_flags[m],
|
| 478 | &transparent_hugepage_flags);
|
| 479 | else
|
| 480 | changed |= test_and_clear_bit(thp_flags[m],
|
| 481 | &transparent_hugepage_flags);
|
| 482 | }
|
| 483 |
|
| 484 | return changed;
|
| 485 | }
|
| 486 |
|
| 487 | static ssize_t enabled_store(struct kobject *kobj,
|
| 488 | struct kobj_attribute *attr,
|
| 489 | const char *buf, size_t count)
|
| 490 | {
|
| 491 | int mode;
|
| 492 |
|
| 493 | mode = sysfs_match_string(global_enabled_mode_strings, buf);
|
| 494 | if (mode < 0)
|
| 495 | return -EINVAL;
|
| 496 |
|
| 497 | if (set_global_enabled_mode(mode)) {
|
| 498 | int err = start_stop_khugepaged();
|
| 499 |
|
| 500 | if (err)
|
| 501 | return err;
|
| 502 | } else {
|
| 503 | |
| 504 | * Recalculate watermarks even when the mode didn't
|
| 505 | * change, as the previous code always called
|
| 506 | * start_stop_khugepaged() which does this internally.
|
| 507 |
|
| 508 | set_recommended_min_free_kbytes();
|
| 509 | }
|
| 510 | return count;
|
| 511 | }
|
| 512 |
|
| 513 | static struct kobj_attribute enabled_attr = __ATTR_RW(enabled);
|
| 514 |
|
| 515 | ssize_t single_hugepage_flag_show(struct kobject *kobj,
|
| 516 | struct kobj_attribute *attr, char *buf,
|
| 517 | enum transparent_hugepage_flag flag)
|
| 518 | {
|
| 519 | return sysfs_emit(buf, "%d\n",
|
| 520 | !!test_bit(flag, &transparent_hugepage_flags));
|
| 521 | }
|
| 522 |
|
| 523 | ssize_t single_hugepage_flag_store(struct kobject *kobj,
|
| 524 | struct kobj_attribute *attr,
|
| 525 | const char *buf, size_t count,
|
| 526 | enum transparent_hugepage_flag flag)
|
| 527 | {
|
| 528 | unsigned long value;
|
| 529 | int ret;
|
| 530 |
|
| 531 | ret = kstrtoul(buf, 10, &value);
|
| 532 | if (ret < 0)
|
| 533 | return ret;
|
| 534 | if (value > 1)
|
| 535 | return -EINVAL;
|
| 536 |
|
| 537 | if (value)
|
| 538 | set_bit(flag, &transparent_hugepage_flags);
|
| 539 | else
|
| 540 | clear_bit(flag, &transparent_hugepage_flags);
|
| 541 |
|
| 542 | return count;
|
| 543 | }
|
| 544 |
|
| 545 | enum defrag_mode {
|
| 546 | DEFRAG_ALWAYS = 0,
|
| 547 | DEFRAG_DEFER,
|
| 548 | DEFRAG_DEFER_MADVISE,
|
| 549 | DEFRAG_MADVISE,
|
| 550 | DEFRAG_NEVER,
|
| 551 | };
|
| 552 |
|
| 553 | static const char * const defrag_mode_strings[] = {
|
| 554 | [DEFRAG_ALWAYS] = "always",
|
| 555 | [DEFRAG_DEFER] = "defer",
|
| 556 | [DEFRAG_DEFER_MADVISE] = "defer+madvise",
|
| 557 | [DEFRAG_MADVISE] = "madvise",
|
| 558 | [DEFRAG_NEVER] = "never",
|
| 559 | };
|
| 560 |
|
| 561 | static const enum transparent_hugepage_flag defrag_flags[] = {
|
| 562 | [DEFRAG_ALWAYS] = TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
|
| 563 | [DEFRAG_DEFER] = TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
|
| 564 | [DEFRAG_DEFER_MADVISE] = TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
|
| 565 | [DEFRAG_MADVISE] = TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
|
| 566 | };
|
| 567 |
|
| 568 | static ssize_t defrag_show(struct kobject *kobj,
|
| 569 | struct kobj_attribute *attr, char *buf)
|
| 570 | {
|
| 571 | int active = DEFRAG_NEVER;
|
| 572 | int len = 0;
|
| 573 | int i;
|
| 574 |
|
| 575 | for (i = 0; i < ARRAY_SIZE(defrag_flags); i++) {
|
| 576 | if (test_bit(defrag_flags[i], &transparent_hugepage_flags)) {
|
| 577 | active = i;
|
| 578 | break;
|
| 579 | }
|
| 580 | }
|
| 581 |
|
| 582 | for (i = 0; i < ARRAY_SIZE(defrag_mode_strings); i++) {
|
| 583 | if (i == active)
|
| 584 | len += sysfs_emit_at(buf, len, "[%s] ",
|
| 585 | defrag_mode_strings[i]);
|
| 586 | else
|
| 587 | len += sysfs_emit_at(buf, len, "%s ",
|
| 588 | defrag_mode_strings[i]);
|
| 589 | }
|
| 590 |
|
| 591 |
|
| 592 | buf[len - 1] = '\n';
|
| 593 |
|
| 594 | return len;
|
| 595 | }
|
| 596 |
|
| 597 | static ssize_t defrag_store(struct kobject *kobj,
|
| 598 | struct kobj_attribute *attr,
|
| 599 | const char *buf, size_t count)
|
| 600 | {
|
| 601 | int mode, m;
|
| 602 |
|
| 603 | mode = sysfs_match_string(defrag_mode_strings, buf);
|
| 604 | if (mode < 0)
|
| 605 | return -EINVAL;
|
| 606 |
|
| 607 | for (m = 0; m < ARRAY_SIZE(defrag_flags); m++) {
|
| 608 | if (m == mode)
|
| 609 | set_bit(defrag_flags[m], &transparent_hugepage_flags);
|
| 610 | else
|
| 611 | clear_bit(defrag_flags[m], &transparent_hugepage_flags);
|
| 612 | }
|
| 613 |
|
| 614 | return count;
|
| 615 | }
|
| 616 | static struct kobj_attribute defrag_attr = __ATTR_RW(defrag);
|
| 617 |
|
| 618 | static ssize_t use_zero_page_show(struct kobject *kobj,
|
| 619 | struct kobj_attribute *attr, char *buf)
|
| 620 | {
|
| 621 | return single_hugepage_flag_show(kobj, attr, buf,
|
| 622 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
|
| 623 | }
|
| 624 | static ssize_t use_zero_page_store(struct kobject *kobj,
|
| 625 | struct kobj_attribute *attr, const char *buf, size_t count)
|
| 626 | {
|
| 627 | return single_hugepage_flag_store(kobj, attr, buf, count,
|
| 628 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
|
| 629 | }
|
| 630 | static struct kobj_attribute use_zero_page_attr = __ATTR_RW(use_zero_page);
|
| 631 |
|
| 632 | static ssize_t hpage_pmd_size_show(struct kobject *kobj,
|
| 633 | struct kobj_attribute *attr, char *buf)
|
| 634 | {
|
| 635 | return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE);
|
| 636 | }
|
| 637 | static struct kobj_attribute hpage_pmd_size_attr =
|
| 638 | __ATTR_RO(hpage_pmd_size);
|
| 639 |
|
| 640 | static ssize_t split_underused_thp_show(struct kobject *kobj,
|
| 641 | struct kobj_attribute *attr, char *buf)
|
| 642 | {
|
| 643 | return sysfs_emit(buf, "%d\n", split_underused_thp);
|
| 644 | }
|
| 645 |
|
| 646 | static ssize_t split_underused_thp_store(struct kobject *kobj,
|
| 647 | struct kobj_attribute *attr,
|
| 648 | const char *buf, size_t count)
|
| 649 | {
|
| 650 | int err = kstrtobool(buf, &split_underused_thp);
|
| 651 |
|
| 652 | if (err < 0)
|
| 653 | return err;
|
| 654 |
|
| 655 | return count;
|
| 656 | }
|
| 657 |
|
| 658 | static struct kobj_attribute split_underused_thp_attr = __ATTR(
|
| 659 | shrink_underused, 0644, split_underused_thp_show, split_underused_thp_store);
|
| 660 |
|
| 661 | static struct attribute *hugepage_attr[] = {
|
| 662 | &enabled_attr.attr,
|
| 663 | &defrag_attr.attr,
|
| 664 | &use_zero_page_attr.attr,
|
| 665 | &hpage_pmd_size_attr.attr,
|
| 666 | #ifdef CONFIG_SHMEM
|
| 667 | &shmem_enabled_attr.attr,
|
| 668 | #endif
|
| 669 | &split_underused_thp_attr.attr,
|
| 670 | NULL,
|
| 671 | };
|
| 672 |
|
| 673 | static const struct attribute_group hugepage_attr_group = {
|
| 674 | .attrs = hugepage_attr,
|
| 675 | };
|
| 676 |
|
| 677 | static void hugepage_exit_sysfs(struct kobject *hugepage_kobj);
|
| 678 | static void thpsize_release(struct kobject *kobj);
|
| 679 | static DEFINE_SPINLOCK(huge_anon_orders_lock);
|
| 680 | static LIST_HEAD(thpsize_list);
|
| 681 |
|
| 682 | static ssize_t anon_enabled_show(struct kobject *kobj,
|
| 683 | struct kobj_attribute *attr, char *buf)
|
| 684 | {
|
| 685 | int order = to_thpsize(kobj)->order;
|
| 686 | const char *output;
|
| 687 |
|
| 688 | if (test_bit(order, &huge_anon_orders_always))
|
| 689 | output = "[always] inherit madvise never";
|
| 690 | else if (test_bit(order, &huge_anon_orders_inherit))
|
| 691 | output = "always [inherit] madvise never";
|
| 692 | else if (test_bit(order, &huge_anon_orders_madvise))
|
| 693 | output = "always inherit [madvise] never";
|
| 694 | else
|
| 695 | output = "always inherit madvise [never]";
|
| 696 |
|
| 697 | return sysfs_emit(buf, "%s\n", output);
|
| 698 | }
|
| 699 |
|
| 700 | static bool set_anon_enabled_mode(int order, enum anon_enabled_mode mode)
|
| 701 | {
|
| 702 | static unsigned long *enabled_orders[] = {
|
| 703 | &huge_anon_orders_always,
|
| 704 | &huge_anon_orders_inherit,
|
| 705 | &huge_anon_orders_madvise,
|
| 706 | };
|
| 707 | enum anon_enabled_mode m;
|
| 708 | bool changed = false;
|
| 709 |
|
| 710 | spin_lock(&huge_anon_orders_lock);
|
| 711 | for (m = 0; m < ARRAY_SIZE(enabled_orders); m++) {
|
| 712 | if (m == mode)
|
| 713 | changed |= !__test_and_set_bit(order, enabled_orders[m]);
|
| 714 | else
|
| 715 | changed |= __test_and_clear_bit(order, enabled_orders[m]);
|
| 716 | }
|
| 717 | spin_unlock(&huge_anon_orders_lock);
|
| 718 |
|
| 719 | return changed;
|
| 720 | }
|
| 721 |
|
| 722 | static ssize_t anon_enabled_store(struct kobject *kobj,
|
| 723 | struct kobj_attribute *attr,
|
| 724 | const char *buf, size_t count)
|
| 725 | {
|
| 726 | int order = to_thpsize(kobj)->order;
|
| 727 | int mode;
|
| 728 |
|
| 729 | mode = sysfs_match_string(anon_enabled_mode_strings, buf);
|
| 730 | if (mode < 0)
|
| 731 | return -EINVAL;
|
| 732 |
|
| 733 | if (set_anon_enabled_mode(order, mode)) {
|
| 734 | int err = start_stop_khugepaged();
|
| 735 |
|
| 736 | if (err)
|
| 737 | return err;
|
| 738 | } else {
|
| 739 | |
| 740 | * Recalculate watermarks even when the mode didn't
|
| 741 | * change, as the previous code always called
|
| 742 | * start_stop_khugepaged() which does this internally.
|
| 743 |
|
| 744 | set_recommended_min_free_kbytes();
|
| 745 | }
|
| 746 |
|
| 747 | return count;
|
| 748 | }
|
| 749 |
|
| 750 | static struct kobj_attribute anon_enabled_attr =
|
| 751 | __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
|
| 752 |
|
| 753 | static struct attribute *anon_ctrl_attrs[] = {
|
| 754 | &anon_enabled_attr.attr,
|
| 755 | NULL,
|
| 756 | };
|
| 757 |
|
| 758 | static const struct attribute_group anon_ctrl_attr_grp = {
|
| 759 | .attrs = anon_ctrl_attrs,
|
| 760 | };
|
| 761 |
|
| 762 | static struct attribute *file_ctrl_attrs[] = {
|
| 763 | #ifdef CONFIG_SHMEM
|
| 764 | &thpsize_shmem_enabled_attr.attr,
|
| 765 | #endif
|
| 766 | NULL,
|
| 767 | };
|
| 768 |
|
| 769 | static const struct attribute_group file_ctrl_attr_grp = {
|
| 770 | .attrs = file_ctrl_attrs,
|
| 771 | };
|
| 772 |
|
| 773 | static struct attribute *any_ctrl_attrs[] = {
|
| 774 | NULL,
|
| 775 | };
|
| 776 |
|
| 777 | static const struct attribute_group any_ctrl_attr_grp = {
|
| 778 | .attrs = any_ctrl_attrs,
|
| 779 | };
|
| 780 |
|
| 781 | static const struct kobj_type thpsize_ktype = {
|
| 782 | .release = &thpsize_release,
|
| 783 | .sysfs_ops = &kobj_sysfs_ops,
|
| 784 | };
|
| 785 |
|
| 786 | DEFINE_PER_CPU(struct mthp_stat, mthp_stats) = {{{0}}};
|
| 787 |
|
| 788 | static unsigned long sum_mthp_stat(int order, enum mthp_stat_item item)
|
| 789 | {
|
| 790 | unsigned long sum = 0;
|
| 791 | int cpu;
|
| 792 |
|
| 793 | for_each_possible_cpu(cpu) {
|
| 794 | struct mthp_stat *this = &per_cpu(mthp_stats, cpu);
|
| 795 |
|
| 796 | sum += this->stats[order][item];
|
| 797 | }
|
| 798 |
|
| 799 | return sum;
|
| 800 | }
|
| 801 |
|
| 802 | #define DEFINE_MTHP_STAT_ATTR(_name, _index) \
|
| 803 | static ssize_t _name##_show(struct kobject *kobj, \
|
| 804 | struct kobj_attribute *attr, char *buf) \
|
| 805 | { \
|
| 806 | int order = to_thpsize(kobj)->order; \
|
| 807 | \
|
| 808 | return sysfs_emit(buf, "%lu\n", sum_mthp_stat(order, _index)); \
|
| 809 | } \
|
| 810 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
|
| 811 |
|
| 812 | DEFINE_MTHP_STAT_ATTR(anon_fault_alloc, MTHP_STAT_ANON_FAULT_ALLOC);
|
| 813 | DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK);
|
| 814 | DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
|
| 815 | DEFINE_MTHP_STAT_ATTR(collapse_alloc, MTHP_STAT_COLLAPSE_ALLOC);
|
| 816 | DEFINE_MTHP_STAT_ATTR(collapse_alloc_failed, MTHP_STAT_COLLAPSE_ALLOC_FAILED);
|
| 817 | DEFINE_MTHP_STAT_ATTR(zswpout, MTHP_STAT_ZSWPOUT);
|
| 818 | DEFINE_MTHP_STAT_ATTR(swpin, MTHP_STAT_SWPIN);
|
| 819 | DEFINE_MTHP_STAT_ATTR(swpin_fallback, MTHP_STAT_SWPIN_FALLBACK);
|
| 820 | DEFINE_MTHP_STAT_ATTR(swpin_fallback_charge, MTHP_STAT_SWPIN_FALLBACK_CHARGE);
|
| 821 | DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
|
| 822 | DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
|
| 823 | #ifdef CONFIG_SHMEM
|
| 824 | DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
|
| 825 | DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
|
| 826 | DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
|
| 827 | #endif
|
| 828 | DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
|
| 829 | DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
|
| 830 | DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
|
| 831 | DEFINE_MTHP_STAT_ATTR(nr_anon, MTHP_STAT_NR_ANON);
|
| 832 | DEFINE_MTHP_STAT_ATTR(nr_anon_partially_mapped, MTHP_STAT_NR_ANON_PARTIALLY_MAPPED);
|
| 833 | DEFINE_MTHP_STAT_ATTR(collapse_exceed_swap_pte, MTHP_STAT_COLLAPSE_EXCEED_SWAP);
|
| 834 | DEFINE_MTHP_STAT_ATTR(collapse_exceed_none_pte, MTHP_STAT_COLLAPSE_EXCEED_NONE);
|
| 835 | DEFINE_MTHP_STAT_ATTR(collapse_exceed_shared_pte, MTHP_STAT_COLLAPSE_EXCEED_SHARED);
|
| 836 |
|
| 837 |
|
| 838 | static struct attribute *anon_stats_attrs[] = {
|
| 839 | &anon_fault_alloc_attr.attr,
|
| 840 | &anon_fault_fallback_attr.attr,
|
| 841 | &anon_fault_fallback_charge_attr.attr,
|
| 842 | #ifndef CONFIG_SHMEM
|
| 843 | &zswpout_attr.attr,
|
| 844 | &swpin_attr.attr,
|
| 845 | &swpin_fallback_attr.attr,
|
| 846 | &swpin_fallback_charge_attr.attr,
|
| 847 | &swpout_attr.attr,
|
| 848 | &swpout_fallback_attr.attr,
|
| 849 | #endif
|
| 850 | &split_deferred_attr.attr,
|
| 851 | &nr_anon_attr.attr,
|
| 852 | &nr_anon_partially_mapped_attr.attr,
|
| 853 | &collapse_exceed_swap_pte_attr.attr,
|
| 854 | &collapse_exceed_none_pte_attr.attr,
|
| 855 | &collapse_exceed_shared_pte_attr.attr,
|
| 856 | NULL,
|
| 857 | };
|
| 858 |
|
| 859 | static struct attribute_group anon_stats_attr_grp = {
|
| 860 | .name = "stats",
|
| 861 | .attrs = anon_stats_attrs,
|
| 862 | };
|
| 863 |
|
| 864 | static struct attribute *file_stats_attrs[] = {
|
| 865 | #ifdef CONFIG_SHMEM
|
| 866 | &shmem_alloc_attr.attr,
|
| 867 | &shmem_fallback_attr.attr,
|
| 868 | &shmem_fallback_charge_attr.attr,
|
| 869 | #endif
|
| 870 | NULL,
|
| 871 | };
|
| 872 |
|
| 873 | static struct attribute_group file_stats_attr_grp = {
|
| 874 | .name = "stats",
|
| 875 | .attrs = file_stats_attrs,
|
| 876 | };
|
| 877 |
|
| 878 | static struct attribute *any_stats_attrs[] = {
|
| 879 | #ifdef CONFIG_SHMEM
|
| 880 | &zswpout_attr.attr,
|
| 881 | &swpin_attr.attr,
|
| 882 | &swpin_fallback_attr.attr,
|
| 883 | &swpin_fallback_charge_attr.attr,
|
| 884 | &swpout_attr.attr,
|
| 885 | &swpout_fallback_attr.attr,
|
| 886 | #endif
|
| 887 | &split_attr.attr,
|
| 888 | &split_failed_attr.attr,
|
| 889 | &collapse_alloc_attr.attr,
|
| 890 | &collapse_alloc_failed_attr.attr,
|
| 891 | NULL,
|
| 892 | };
|
| 893 |
|
| 894 | static struct attribute_group any_stats_attr_grp = {
|
| 895 | .name = "stats",
|
| 896 | .attrs = any_stats_attrs,
|
| 897 | };
|
| 898 |
|
| 899 | static int sysfs_add_group(struct kobject *kobj,
|
| 900 | const struct attribute_group *grp)
|
| 901 | {
|
| 902 | int ret = -ENOENT;
|
| 903 |
|
| 904 | |
| 905 | * If the group is named, try to merge first, assuming the subdirectory
|
| 906 | * was already created. This avoids the warning emitted by
|
| 907 | * sysfs_create_group() if the directory already exists.
|
| 908 |
|
| 909 | if (grp->name)
|
| 910 | ret = sysfs_merge_group(kobj, grp);
|
| 911 | if (ret)
|
| 912 | ret = sysfs_create_group(kobj, grp);
|
| 913 |
|
| 914 | return ret;
|
| 915 | }
|
| 916 |
|
| 917 | static struct thpsize *thpsize_create(int order, struct kobject *parent)
|
| 918 | {
|
| 919 | unsigned long size = (PAGE_SIZE << order) / SZ_1K;
|
| 920 | struct thpsize *thpsize;
|
| 921 | int ret = -ENOMEM;
|
| 922 |
|
| 923 | thpsize = kzalloc_obj(*thpsize);
|
| 924 | if (!thpsize)
|
| 925 | goto err;
|
| 926 |
|
| 927 | thpsize->order = order;
|
| 928 |
|
| 929 | ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
|
| 930 | "hugepages-%lukB", size);
|
| 931 | if (ret)
|
| 932 | goto err_put;
|
| 933 |
|
| 934 |
|
| 935 | ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
|
| 936 | if (ret)
|
| 937 | goto err_put;
|
| 938 |
|
| 939 | ret = sysfs_add_group(&thpsize->kobj, &any_stats_attr_grp);
|
| 940 | if (ret)
|
| 941 | goto err_put;
|
| 942 |
|
| 943 | if (BIT(order) & THP_ORDERS_ALL_ANON) {
|
| 944 | ret = sysfs_add_group(&thpsize->kobj, &anon_ctrl_attr_grp);
|
| 945 | if (ret)
|
| 946 | goto err_put;
|
| 947 |
|
| 948 | ret = sysfs_add_group(&thpsize->kobj, &anon_stats_attr_grp);
|
| 949 | if (ret)
|
| 950 | goto err_put;
|
| 951 | }
|
| 952 |
|
| 953 | if (BIT(order) & THP_ORDERS_ALL_FILE_DEFAULT) {
|
| 954 | ret = sysfs_add_group(&thpsize->kobj, &file_ctrl_attr_grp);
|
| 955 | if (ret)
|
| 956 | goto err_put;
|
| 957 |
|
| 958 | ret = sysfs_add_group(&thpsize->kobj, &file_stats_attr_grp);
|
| 959 | if (ret)
|
| 960 | goto err_put;
|
| 961 | }
|
| 962 |
|
| 963 | return thpsize;
|
| 964 | err_put:
|
| 965 | kobject_put(&thpsize->kobj);
|
| 966 | err:
|
| 967 | return ERR_PTR(ret);
|
| 968 | }
|
| 969 |
|
| 970 | static void thpsize_release(struct kobject *kobj)
|
| 971 | {
|
| 972 | kfree(to_thpsize(kobj));
|
| 973 | }
|
| 974 |
|
| 975 | static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
|
| 976 | {
|
| 977 | int err;
|
| 978 | struct thpsize *thpsize;
|
| 979 | unsigned long orders;
|
| 980 | int order;
|
| 981 |
|
| 982 | |
| 983 | * Default to setting PMD-sized THP to inherit the global setting and
|
| 984 | * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
|
| 985 | * constant so we have to do this here.
|
| 986 |
|
| 987 | if (!anon_orders_configured)
|
| 988 | huge_anon_orders_inherit = BIT(PMD_ORDER);
|
| 989 |
|
| 990 | *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
|
| 991 | if (unlikely(!*hugepage_kobj)) {
|
| 992 | pr_err("failed to create transparent hugepage kobject\n");
|
| 993 | return -ENOMEM;
|
| 994 | }
|
| 995 |
|
| 996 | err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
|
| 997 | if (err) {
|
| 998 | pr_err("failed to register transparent hugepage group\n");
|
| 999 | goto delete_obj;
|
| 1000 | }
|
| 1001 |
|
| 1002 | err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
|
| 1003 | if (err) {
|
| 1004 | pr_err("failed to register transparent hugepage group\n");
|
| 1005 | goto remove_hp_group;
|
| 1006 | }
|
| 1007 |
|
| 1008 | orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT;
|
| 1009 | order = highest_order(orders);
|
| 1010 | while (orders) {
|
| 1011 | thpsize = thpsize_create(order, *hugepage_kobj);
|
| 1012 | if (IS_ERR(thpsize)) {
|
| 1013 | pr_err("failed to create thpsize for order %d\n", order);
|
| 1014 | err = PTR_ERR(thpsize);
|
| 1015 | goto remove_all;
|
| 1016 | }
|
| 1017 | list_add(&thpsize->node, &thpsize_list);
|
| 1018 | order = next_order(&orders, order);
|
| 1019 | }
|
| 1020 |
|
| 1021 | return 0;
|
| 1022 |
|
| 1023 | remove_all:
|
| 1024 | hugepage_exit_sysfs(*hugepage_kobj);
|
| 1025 | return err;
|
| 1026 | remove_hp_group:
|
| 1027 | sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
|
| 1028 | delete_obj:
|
| 1029 | kobject_put(*hugepage_kobj);
|
| 1030 | return err;
|
| 1031 | }
|
| 1032 |
|
| 1033 | static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
|
| 1034 | {
|
| 1035 | struct thpsize *thpsize, *tmp;
|
| 1036 |
|
| 1037 | list_for_each_entry_safe(thpsize, tmp, &thpsize_list, node) {
|
| 1038 | list_del(&thpsize->node);
|
| 1039 | kobject_put(&thpsize->kobj);
|
| 1040 | }
|
| 1041 |
|
| 1042 | sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
|
| 1043 | sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
|
| 1044 | kobject_put(hugepage_kobj);
|
| 1045 | }
|
| 1046 | #else
|
| 1047 | static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
|
| 1048 | {
|
| 1049 | return 0;
|
| 1050 | }
|
| 1051 |
|
| 1052 | static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
|
| 1053 | {
|
| 1054 | }
|
| 1055 | #endif
|
| 1056 |
|
| 1057 | int folio_memcg_alloc_deferred(struct folio *folio)
|
| 1058 | {
|
| 1059 | if (mem_cgroup_disabled())
|
| 1060 | return 0;
|
| 1061 | return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
|
| 1062 | }
|
| 1063 |
|
| 1064 | static int __init thp_shrinker_init(void)
|
| 1065 | {
|
| 1066 | deferred_split_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE |
|
| 1067 | SHRINKER_MEMCG_AWARE,
|
| 1068 | "thp-deferred_split");
|
| 1069 | if (!deferred_split_shrinker)
|
| 1070 | return -ENOMEM;
|
| 1071 |
|
| 1072 | if (list_lru_init_memcg_key(&deferred_split_lru,
|
| 1073 | deferred_split_shrinker,
|
| 1074 | &deferred_split_key)) {
|
| 1075 | shrinker_free(deferred_split_shrinker);
|
| 1076 | return -ENOMEM;
|
| 1077 | }
|
| 1078 |
|
| 1079 | deferred_split_shrinker->count_objects = deferred_split_count;
|
| 1080 | deferred_split_shrinker->scan_objects = deferred_split_scan;
|
| 1081 | shrinker_register(deferred_split_shrinker);
|
| 1082 |
|
| 1083 | return huge_zero_init();
|
| 1084 | }
|
| 1085 |
|
| 1086 | static void __init thp_shrinker_exit(void)
|
| 1087 | {
|
| 1088 | shrinker_free(deferred_split_shrinker);
|
| 1089 | list_lru_destroy(&deferred_split_lru);
|
| 1090 | huge_zero_shrinker_exit();
|
| 1091 | }
|
| 1092 |
|
| 1093 | static int __init hugepage_init(void)
|
| 1094 | {
|
| 1095 | int err;
|
| 1096 | struct kobject *hugepage_kobj;
|
| 1097 |
|
| 1098 | if (!has_transparent_hugepage()) {
|
| 1099 | transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED;
|
| 1100 | return -EINVAL;
|
| 1101 | }
|
| 1102 |
|
| 1103 | |
| 1104 | * hugepages can't be allocated by the buddy allocator
|
| 1105 |
|
| 1106 | MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER > MAX_PAGE_ORDER);
|
| 1107 |
|
| 1108 | err = hugepage_init_sysfs(&hugepage_kobj);
|
| 1109 | if (err)
|
| 1110 | goto err_sysfs;
|
| 1111 |
|
| 1112 | err = khugepaged_init();
|
| 1113 | if (err)
|
| 1114 | goto err_slab;
|
| 1115 |
|
| 1116 | err = thp_shrinker_init();
|
| 1117 | if (err)
|
| 1118 | goto err_shrinker;
|
| 1119 |
|
| 1120 | |
| 1121 | * By default disable transparent hugepages on smaller systems,
|
| 1122 | * where the extra memory used could hurt more than TLB overhead
|
| 1123 | * is likely to save. The admin can still enable it through /sys.
|
| 1124 |
|
| 1125 | if (totalram_pages() < MB_TO_PAGES(512)) {
|
| 1126 | transparent_hugepage_flags = 0;
|
| 1127 | return 0;
|
| 1128 | }
|
| 1129 |
|
| 1130 | err = start_stop_khugepaged();
|
| 1131 | if (err)
|
| 1132 | goto err_khugepaged;
|
| 1133 |
|
| 1134 | return 0;
|
| 1135 | err_khugepaged:
|
| 1136 | thp_shrinker_exit();
|
| 1137 | err_shrinker:
|
| 1138 | khugepaged_destroy();
|
| 1139 | err_slab:
|
| 1140 | hugepage_exit_sysfs(hugepage_kobj);
|
| 1141 | err_sysfs:
|
| 1142 | return err;
|
| 1143 | }
|
| 1144 | subsys_initcall(hugepage_init);
|
| 1145 |
|
| 1146 | static int __init setup_transparent_hugepage(char *str)
|
| 1147 | {
|
| 1148 | int ret = 0;
|
| 1149 | if (!str)
|
| 1150 | goto out;
|
| 1151 | if (!strcmp(str, "always")) {
|
| 1152 | set_bit(TRANSPARENT_HUGEPAGE_FLAG,
|
| 1153 | &transparent_hugepage_flags);
|
| 1154 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
|
| 1155 | &transparent_hugepage_flags);
|
| 1156 | ret = 1;
|
| 1157 | } else if (!strcmp(str, "madvise")) {
|
| 1158 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
|
| 1159 | &transparent_hugepage_flags);
|
| 1160 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
|
| 1161 | &transparent_hugepage_flags);
|
| 1162 | ret = 1;
|
| 1163 | } else if (!strcmp(str, "never")) {
|
| 1164 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
|
| 1165 | &transparent_hugepage_flags);
|
| 1166 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
|
| 1167 | &transparent_hugepage_flags);
|
| 1168 | ret = 1;
|
| 1169 | }
|
| 1170 | out:
|
| 1171 | if (!ret)
|
| 1172 | pr_warn("transparent_hugepage= cannot parse, ignored\n");
|
| 1173 | return ret;
|
| 1174 | }
|
| 1175 | __setup("transparent_hugepage=", setup_transparent_hugepage);
|
| 1176 |
|
| 1177 | static char str_dup[PAGE_SIZE] __initdata;
|
| 1178 | static int __init setup_thp_anon(char *str)
|
| 1179 | {
|
| 1180 | char *token, *range, *policy, *subtoken;
|
| 1181 | unsigned long always, inherit, madvise;
|
| 1182 | char *start_size, *end_size;
|
| 1183 | int start, end, nr;
|
| 1184 | char *p;
|
| 1185 |
|
| 1186 | if (!str || strlen(str) + 1 > PAGE_SIZE)
|
| 1187 | goto err;
|
| 1188 | strscpy(str_dup, str);
|
| 1189 |
|
| 1190 | always = huge_anon_orders_always;
|
| 1191 | madvise = huge_anon_orders_madvise;
|
| 1192 | inherit = huge_anon_orders_inherit;
|
| 1193 | p = str_dup;
|
| 1194 | while ((token = strsep(&p, ";")) != NULL) {
|
| 1195 | range = strsep(&token, ":");
|
| 1196 | policy = token;
|
| 1197 |
|
| 1198 | if (!policy)
|
| 1199 | goto err;
|
| 1200 |
|
| 1201 | while ((subtoken = strsep(&range, ",")) != NULL) {
|
| 1202 | if (strchr(subtoken, '-')) {
|
| 1203 | start_size = strsep(&subtoken, "-");
|
| 1204 | end_size = subtoken;
|
| 1205 |
|
| 1206 | start = get_order_from_str(start_size, THP_ORDERS_ALL_ANON);
|
| 1207 | end = get_order_from_str(end_size, THP_ORDERS_ALL_ANON);
|
| 1208 | } else {
|
| 1209 | start_size = end_size = subtoken;
|
| 1210 | start = end = get_order_from_str(subtoken,
|
| 1211 | THP_ORDERS_ALL_ANON);
|
| 1212 | }
|
| 1213 |
|
| 1214 | if (start == -EINVAL) {
|
| 1215 | pr_err("invalid size %s in thp_anon boot parameter\n", start_size);
|
| 1216 | goto err;
|
| 1217 | }
|
| 1218 |
|
| 1219 | if (end == -EINVAL) {
|
| 1220 | pr_err("invalid size %s in thp_anon boot parameter\n", end_size);
|
| 1221 | goto err;
|
| 1222 | }
|
| 1223 |
|
| 1224 | if (start < 0 || end < 0 || start > end)
|
| 1225 | goto err;
|
| 1226 |
|
| 1227 | nr = end - start + 1;
|
| 1228 | if (!strcmp(policy, "always")) {
|
| 1229 | bitmap_set(&always, start, nr);
|
| 1230 | bitmap_clear(&inherit, start, nr);
|
| 1231 | bitmap_clear(&madvise, start, nr);
|
| 1232 | } else if (!strcmp(policy, "madvise")) {
|
| 1233 | bitmap_set(&madvise, start, nr);
|
| 1234 | bitmap_clear(&inherit, start, nr);
|
| 1235 | bitmap_clear(&always, start, nr);
|
| 1236 | } else if (!strcmp(policy, "inherit")) {
|
| 1237 | bitmap_set(&inherit, start, nr);
|
| 1238 | bitmap_clear(&madvise, start, nr);
|
| 1239 | bitmap_clear(&always, start, nr);
|
| 1240 | } else if (!strcmp(policy, "never")) {
|
| 1241 | bitmap_clear(&inherit, start, nr);
|
| 1242 | bitmap_clear(&madvise, start, nr);
|
| 1243 | bitmap_clear(&always, start, nr);
|
| 1244 | } else {
|
| 1245 | pr_err("invalid policy %s in thp_anon boot parameter\n", policy);
|
| 1246 | goto err;
|
| 1247 | }
|
| 1248 | }
|
| 1249 | }
|
| 1250 |
|
| 1251 | huge_anon_orders_always = always;
|
| 1252 | huge_anon_orders_madvise = madvise;
|
| 1253 | huge_anon_orders_inherit = inherit;
|
| 1254 | anon_orders_configured = true;
|
| 1255 | return 1;
|
| 1256 |
|
| 1257 | err:
|
| 1258 | pr_warn("thp_anon=%s: error parsing string, ignoring setting\n", str);
|
| 1259 | return 0;
|
| 1260 | }
|
| 1261 | __setup("thp_anon=", setup_thp_anon);
|
| 1262 |
|
| 1263 | pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
|
| 1264 | {
|
| 1265 | if (likely(vma->vm_flags & VM_WRITE))
|
| 1266 | pmd = pmd_mkwrite(pmd, vma);
|
| 1267 | return pmd;
|
| 1268 | }
|
| 1269 |
|
| 1270 | static inline bool is_transparent_hugepage(const struct folio *folio)
|
| 1271 | {
|
| 1272 | if (!folio_test_large(folio))
|
| 1273 | return false;
|
| 1274 |
|
| 1275 | return is_huge_zero_folio(folio) ||
|
| 1276 | folio_test_large_rmappable(folio);
|
| 1277 | }
|
| 1278 |
|
| 1279 | static unsigned long __thp_get_unmapped_area(struct file *filp,
|
| 1280 | unsigned long addr, unsigned long len,
|
| 1281 | loff_t off, unsigned long flags, unsigned long size,
|
| 1282 | vma_flags_t vma_flags)
|
| 1283 | {
|
| 1284 | loff_t off_end = off + len;
|
| 1285 | loff_t off_align = round_up(off, size);
|
| 1286 | unsigned long len_pad, ret, off_sub;
|
| 1287 |
|
| 1288 | if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall())
|
| 1289 | return 0;
|
| 1290 |
|
| 1291 | if (off_end <= off_align || (off_end - off_align) < size)
|
| 1292 | return 0;
|
| 1293 |
|
| 1294 | len_pad = len + size;
|
| 1295 | if (len_pad < len || (off + len_pad) < off)
|
| 1296 | return 0;
|
| 1297 |
|
| 1298 | ret = mm_get_unmapped_area_vmaflags(filp, addr, len_pad,
|
| 1299 | off >> PAGE_SHIFT, flags,
|
| 1300 | vma_flags);
|
| 1301 |
|
| 1302 | |
| 1303 | * The failure might be due to length padding. The caller will retry
|
| 1304 | * without the padding.
|
| 1305 |
|
| 1306 | if (IS_ERR_VALUE(ret))
|
| 1307 | return 0;
|
| 1308 |
|
| 1309 | |
| 1310 | * Do not try to align to THP boundary if allocation at the address
|
| 1311 | * hint succeeds.
|
| 1312 |
|
| 1313 | if (ret == addr)
|
| 1314 | return addr;
|
| 1315 |
|
| 1316 | off_sub = (off - ret) & (size - 1);
|
| 1317 |
|
| 1318 | if (mm_flags_test(MMF_TOPDOWN, current->mm) && !off_sub)
|
| 1319 | return ret + size;
|
| 1320 |
|
| 1321 | ret += off_sub;
|
| 1322 | return ret;
|
| 1323 | }
|
| 1324 |
|
| 1325 | unsigned long thp_get_unmapped_area_vmaflags(struct file *filp, unsigned long addr,
|
| 1326 | unsigned long len, unsigned long pgoff, unsigned long flags,
|
| 1327 | vma_flags_t vma_flags)
|
| 1328 | {
|
| 1329 | unsigned long ret;
|
| 1330 | loff_t off = (loff_t)pgoff << PAGE_SHIFT;
|
| 1331 |
|
| 1332 | ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE,
|
| 1333 | vma_flags);
|
| 1334 | if (ret)
|
| 1335 | return ret;
|
| 1336 |
|
| 1337 | return mm_get_unmapped_area_vmaflags(filp, addr, len, pgoff, flags,
|
| 1338 | vma_flags);
|
| 1339 | }
|
| 1340 |
|
| 1341 | unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
|
| 1342 | unsigned long len, unsigned long pgoff, unsigned long flags)
|
| 1343 | {
|
| 1344 | return thp_get_unmapped_area_vmaflags(filp, addr, len, pgoff, flags,
|
| 1345 | EMPTY_VMA_FLAGS);
|
| 1346 | }
|
| 1347 | EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
|
| 1348 |
|
| 1349 | static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma,
|
| 1350 | unsigned long addr)
|
| 1351 | {
|
| 1352 | gfp_t gfp = vma_thp_gfp_mask(vma);
|
| 1353 | const int order = HPAGE_PMD_ORDER;
|
| 1354 | struct folio *folio;
|
| 1355 |
|
| 1356 | folio = vma_alloc_folio(gfp, order, vma, addr & HPAGE_PMD_MASK);
|
| 1357 |
|
| 1358 | if (unlikely(!folio)) {
|
| 1359 | count_vm_event(THP_FAULT_FALLBACK);
|
| 1360 | count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
|
| 1361 | return NULL;
|
| 1362 | }
|
| 1363 |
|
| 1364 | VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
|
| 1365 | if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
|
| 1366 | folio_put(folio);
|
| 1367 | count_vm_event(THP_FAULT_FALLBACK);
|
| 1368 | count_vm_event(THP_FAULT_FALLBACK_CHARGE);
|
| 1369 | count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
|
| 1370 | count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
|
| 1371 | return NULL;
|
| 1372 | }
|
| 1373 |
|
| 1374 | if (folio_memcg_alloc_deferred(folio)) {
|
| 1375 | folio_put(folio);
|
| 1376 | count_vm_event(THP_FAULT_FALLBACK);
|
| 1377 | count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
|
| 1378 | return NULL;
|
| 1379 | }
|
| 1380 |
|
| 1381 | folio_throttle_swaprate(folio, gfp);
|
| 1382 |
|
| 1383 | |
| 1384 | * When a folio is not zeroed during allocation (__GFP_ZERO not used)
|
| 1385 | * or user folios require special handling, folio_zero_user() is used to
|
| 1386 | * make sure that the page corresponding to the faulting address will be
|
| 1387 | * hot in the cache after zeroing.
|
| 1388 |
|
| 1389 | if (user_alloc_needs_zeroing())
|
| 1390 | folio_zero_user(folio, addr);
|
| 1391 | |
| 1392 | * The memory barrier inside __folio_mark_uptodate makes sure that
|
| 1393 | * folio_zero_user writes become visible before the set_pmd_at()
|
| 1394 | * write.
|
| 1395 |
|
| 1396 | __folio_mark_uptodate(folio);
|
| 1397 | return folio;
|
| 1398 | }
|
| 1399 |
|
| 1400 | void map_anon_folio_pmd_nopf(struct folio *folio, pmd_t *pmd,
|
| 1401 | struct vm_area_struct *vma, unsigned long haddr)
|
| 1402 | {
|
| 1403 | pmd_t entry;
|
| 1404 |
|
| 1405 | entry = folio_mk_pmd(folio, vma->vm_page_prot);
|
| 1406 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
|
| 1407 | folio_add_new_anon_rmap(folio, vma, haddr, RMAP_EXCLUSIVE);
|
| 1408 | folio_add_lru_vma(folio, vma);
|
| 1409 | set_pmd_at(vma->vm_mm, haddr, pmd, entry);
|
| 1410 | update_mmu_cache_pmd(vma, haddr, pmd);
|
| 1411 | deferred_split_folio(folio, false);
|
| 1412 | }
|
| 1413 |
|
| 1414 | static void map_anon_folio_pmd_pf(struct folio *folio, pmd_t *pmd,
|
| 1415 | struct vm_area_struct *vma, unsigned long haddr)
|
| 1416 | {
|
| 1417 | map_anon_folio_pmd_nopf(folio, pmd, vma, haddr);
|
| 1418 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
|
| 1419 | count_vm_event(THP_FAULT_ALLOC);
|
| 1420 | count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_ALLOC);
|
| 1421 | count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
|
| 1422 | }
|
| 1423 |
|
| 1424 | static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf)
|
| 1425 | {
|
| 1426 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
| 1427 | struct vm_area_struct *vma = vmf->vma;
|
| 1428 | struct folio *folio;
|
| 1429 | pgtable_t pgtable;
|
| 1430 | vm_fault_t ret = 0;
|
| 1431 |
|
| 1432 | folio = vma_alloc_anon_folio_pmd(vma, vmf->address);
|
| 1433 | if (unlikely(!folio))
|
| 1434 | return VM_FAULT_FALLBACK;
|
| 1435 |
|
| 1436 | pgtable = pte_alloc_one(vma->vm_mm);
|
| 1437 | if (unlikely(!pgtable)) {
|
| 1438 | ret = VM_FAULT_OOM;
|
| 1439 | goto release;
|
| 1440 | }
|
| 1441 |
|
| 1442 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 1443 | if (unlikely(!pmd_none(*vmf->pmd))) {
|
| 1444 | goto unlock_release;
|
| 1445 | } else {
|
| 1446 | ret = check_stable_address_space(vma->vm_mm);
|
| 1447 | if (ret)
|
| 1448 | goto unlock_release;
|
| 1449 |
|
| 1450 |
|
| 1451 | if (userfaultfd_missing(vma)) {
|
| 1452 | spin_unlock(vmf->ptl);
|
| 1453 | folio_put(folio);
|
| 1454 | pte_free(vma->vm_mm, pgtable);
|
| 1455 | ret = handle_userfault(vmf, VM_UFFD_MISSING);
|
| 1456 | VM_BUG_ON(ret & VM_FAULT_FALLBACK);
|
| 1457 | return ret;
|
| 1458 | }
|
| 1459 | pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
|
| 1460 | map_anon_folio_pmd_pf(folio, vmf->pmd, vma, haddr);
|
| 1461 | mm_inc_nr_ptes(vma->vm_mm);
|
| 1462 | spin_unlock(vmf->ptl);
|
| 1463 | }
|
| 1464 |
|
| 1465 | return 0;
|
| 1466 | unlock_release:
|
| 1467 | spin_unlock(vmf->ptl);
|
| 1468 | release:
|
| 1469 | if (pgtable)
|
| 1470 | pte_free(vma->vm_mm, pgtable);
|
| 1471 | folio_put(folio);
|
| 1472 | return ret;
|
| 1473 |
|
| 1474 | }
|
| 1475 |
|
| 1476 | vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)
|
| 1477 | {
|
| 1478 | struct vm_area_struct *vma = vmf->vma;
|
| 1479 | vm_fault_t ret = 0;
|
| 1480 | spinlock_t *ptl;
|
| 1481 | softleaf_t entry;
|
| 1482 | struct page *page;
|
| 1483 | struct folio *folio;
|
| 1484 |
|
| 1485 | if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
|
| 1486 | vma_end_read(vma);
|
| 1487 | return VM_FAULT_RETRY;
|
| 1488 | }
|
| 1489 |
|
| 1490 | ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 1491 | if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd))) {
|
| 1492 | spin_unlock(ptl);
|
| 1493 | return 0;
|
| 1494 | }
|
| 1495 |
|
| 1496 | entry = softleaf_from_pmd(vmf->orig_pmd);
|
| 1497 | page = softleaf_to_page(entry);
|
| 1498 | folio = page_folio(page);
|
| 1499 | vmf->page = page;
|
| 1500 | vmf->pte = NULL;
|
| 1501 | if (folio_trylock(folio)) {
|
| 1502 | folio_get(folio);
|
| 1503 | spin_unlock(ptl);
|
| 1504 | ret = page_pgmap(page)->ops->migrate_to_ram(vmf);
|
| 1505 | folio_unlock(folio);
|
| 1506 | folio_put(folio);
|
| 1507 | } else {
|
| 1508 | spin_unlock(ptl);
|
| 1509 | }
|
| 1510 |
|
| 1511 | return ret;
|
| 1512 | }
|
| 1513 |
|
| 1514 | |
| 1515 | * always: directly stall for all thp allocations
|
| 1516 | * defer: wake kswapd and fail if not immediately available
|
| 1517 | * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
|
| 1518 | * fail if not immediately available
|
| 1519 | * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
|
| 1520 | * available
|
| 1521 | * never: never stall for any thp allocation
|
| 1522 |
|
| 1523 | gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
|
| 1524 | {
|
| 1525 | const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
|
| 1526 |
|
| 1527 |
|
| 1528 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
|
| 1529 | return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
|
| 1530 |
|
| 1531 |
|
| 1532 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
|
| 1533 | return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
|
| 1534 |
|
| 1535 |
|
| 1536 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
|
| 1537 | return GFP_TRANSHUGE_LIGHT |
|
| 1538 | (vma_madvised ? __GFP_DIRECT_RECLAIM :
|
| 1539 | __GFP_KSWAPD_RECLAIM);
|
| 1540 |
|
| 1541 |
|
| 1542 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
|
| 1543 | return GFP_TRANSHUGE_LIGHT |
|
| 1544 | (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
|
| 1545 |
|
| 1546 | return GFP_TRANSHUGE_LIGHT;
|
| 1547 | }
|
| 1548 |
|
| 1549 |
|
| 1550 | static void set_huge_zero_folio(pgtable_t pgtable, struct mm_struct *mm,
|
| 1551 | struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
|
| 1552 | struct folio *zero_folio)
|
| 1553 | {
|
| 1554 | pmd_t entry;
|
| 1555 | entry = folio_mk_pmd(zero_folio, vma->vm_page_prot);
|
| 1556 | entry = pmd_mkspecial(entry);
|
| 1557 | pgtable_trans_huge_deposit(mm, pmd, pgtable);
|
| 1558 | set_pmd_at(mm, haddr, pmd, entry);
|
| 1559 | mm_inc_nr_ptes(mm);
|
| 1560 | }
|
| 1561 |
|
| 1562 | vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
|
| 1563 | {
|
| 1564 | struct vm_area_struct *vma = vmf->vma;
|
| 1565 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
| 1566 | vm_fault_t ret;
|
| 1567 |
|
| 1568 | if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER))
|
| 1569 | return VM_FAULT_FALLBACK;
|
| 1570 | ret = vmf_anon_prepare(vmf);
|
| 1571 | if (ret)
|
| 1572 | return ret;
|
| 1573 | khugepaged_enter_vma(vma, vma->vm_flags);
|
| 1574 |
|
| 1575 | if (!(vmf->flags & FAULT_FLAG_WRITE) &&
|
| 1576 | !mm_forbids_zeropage(vma->vm_mm) &&
|
| 1577 | transparent_hugepage_use_zero_page()) {
|
| 1578 | pgtable_t pgtable;
|
| 1579 | struct folio *zero_folio;
|
| 1580 | vm_fault_t ret;
|
| 1581 |
|
| 1582 | pgtable = pte_alloc_one(vma->vm_mm);
|
| 1583 | if (unlikely(!pgtable))
|
| 1584 | return VM_FAULT_OOM;
|
| 1585 | zero_folio = mm_get_huge_zero_folio(vma->vm_mm);
|
| 1586 | if (unlikely(!zero_folio)) {
|
| 1587 | pte_free(vma->vm_mm, pgtable);
|
| 1588 | count_vm_event(THP_FAULT_FALLBACK);
|
| 1589 | return VM_FAULT_FALLBACK;
|
| 1590 | }
|
| 1591 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 1592 | ret = 0;
|
| 1593 | if (pmd_none(*vmf->pmd)) {
|
| 1594 | ret = check_stable_address_space(vma->vm_mm);
|
| 1595 | if (ret) {
|
| 1596 | spin_unlock(vmf->ptl);
|
| 1597 | pte_free(vma->vm_mm, pgtable);
|
| 1598 | } else if (userfaultfd_missing(vma)) {
|
| 1599 | spin_unlock(vmf->ptl);
|
| 1600 | pte_free(vma->vm_mm, pgtable);
|
| 1601 | ret = handle_userfault(vmf, VM_UFFD_MISSING);
|
| 1602 | VM_BUG_ON(ret & VM_FAULT_FALLBACK);
|
| 1603 | } else {
|
| 1604 | set_huge_zero_folio(pgtable, vma->vm_mm, vma,
|
| 1605 | haddr, vmf->pmd, zero_folio);
|
| 1606 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
|
| 1607 | spin_unlock(vmf->ptl);
|
| 1608 | }
|
| 1609 | } else {
|
| 1610 | spin_unlock(vmf->ptl);
|
| 1611 | pte_free(vma->vm_mm, pgtable);
|
| 1612 | }
|
| 1613 | return ret;
|
| 1614 | }
|
| 1615 |
|
| 1616 | return __do_huge_pmd_anonymous_page(vmf);
|
| 1617 | }
|
| 1618 |
|
| 1619 | struct folio_or_pfn {
|
| 1620 | union {
|
| 1621 | struct folio *folio;
|
| 1622 | unsigned long pfn;
|
| 1623 | };
|
| 1624 | bool is_folio;
|
| 1625 | };
|
| 1626 |
|
| 1627 | static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
|
| 1628 | pmd_t *pmd, struct folio_or_pfn fop, pgprot_t prot,
|
| 1629 | bool write)
|
| 1630 | {
|
| 1631 | struct mm_struct *mm = vma->vm_mm;
|
| 1632 | pgtable_t pgtable = NULL;
|
| 1633 | spinlock_t *ptl;
|
| 1634 | pmd_t entry;
|
| 1635 |
|
| 1636 | if (addr < vma->vm_start || addr >= vma->vm_end)
|
| 1637 | return VM_FAULT_SIGBUS;
|
| 1638 |
|
| 1639 | if (arch_needs_pgtable_deposit()) {
|
| 1640 | pgtable = pte_alloc_one(vma->vm_mm);
|
| 1641 | if (!pgtable)
|
| 1642 | return VM_FAULT_OOM;
|
| 1643 | }
|
| 1644 |
|
| 1645 | ptl = pmd_lock(mm, pmd);
|
| 1646 | if (!pmd_none(*pmd)) {
|
| 1647 | const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) :
|
| 1648 | fop.pfn;
|
| 1649 |
|
| 1650 | if (write) {
|
| 1651 | if (pmd_pfn(*pmd) != pfn) {
|
| 1652 | WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
|
| 1653 | goto out_unlock;
|
| 1654 | }
|
| 1655 | entry = pmd_mkyoung(*pmd);
|
| 1656 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
|
| 1657 | if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
|
| 1658 | update_mmu_cache_pmd(vma, addr, pmd);
|
| 1659 | }
|
| 1660 | goto out_unlock;
|
| 1661 | }
|
| 1662 |
|
| 1663 | if (fop.is_folio) {
|
| 1664 | entry = folio_mk_pmd(fop.folio, vma->vm_page_prot);
|
| 1665 |
|
| 1666 | if (is_huge_zero_folio(fop.folio)) {
|
| 1667 | entry = pmd_mkspecial(entry);
|
| 1668 | } else {
|
| 1669 | folio_get(fop.folio);
|
| 1670 | folio_add_file_rmap_pmd(fop.folio, &fop.folio->page, vma);
|
| 1671 | add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PMD_NR);
|
| 1672 | }
|
| 1673 | } else {
|
| 1674 | entry = pmd_mkhuge(pfn_pmd(fop.pfn, prot));
|
| 1675 | entry = pmd_mkspecial(entry);
|
| 1676 | }
|
| 1677 | if (write) {
|
| 1678 | entry = pmd_mkyoung(pmd_mkdirty(entry));
|
| 1679 | entry = maybe_pmd_mkwrite(entry, vma);
|
| 1680 | }
|
| 1681 |
|
| 1682 | if (pgtable) {
|
| 1683 | pgtable_trans_huge_deposit(mm, pmd, pgtable);
|
| 1684 | mm_inc_nr_ptes(mm);
|
| 1685 | pgtable = NULL;
|
| 1686 | }
|
| 1687 |
|
| 1688 | set_pmd_at(mm, addr, pmd, entry);
|
| 1689 | update_mmu_cache_pmd(vma, addr, pmd);
|
| 1690 |
|
| 1691 | out_unlock:
|
| 1692 | spin_unlock(ptl);
|
| 1693 | if (pgtable)
|
| 1694 | pte_free(mm, pgtable);
|
| 1695 | return VM_FAULT_NOPAGE;
|
| 1696 | }
|
| 1697 |
|
| 1698 | |
| 1699 | * vmf_insert_pfn_pmd - insert a pmd size pfn
|
| 1700 | * @vmf: Structure describing the fault
|
| 1701 | * @pfn: pfn to insert
|
| 1702 | * @write: whether it's a write fault
|
| 1703 | *
|
| 1704 | * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
|
| 1705 | *
|
| 1706 | * Return: vm_fault_t value.
|
| 1707 |
|
| 1708 | vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
|
| 1709 | bool write)
|
| 1710 | {
|
| 1711 | unsigned long addr = vmf->address & PMD_MASK;
|
| 1712 | struct vm_area_struct *vma = vmf->vma;
|
| 1713 | pgprot_t pgprot = vma->vm_page_prot;
|
| 1714 | struct folio_or_pfn fop = {
|
| 1715 | .pfn = pfn,
|
| 1716 | };
|
| 1717 |
|
| 1718 | |
| 1719 | * If we had pmd_special, we could avoid all these restrictions,
|
| 1720 | * but we need to be consistent with PTEs and architectures that
|
| 1721 | * can't support a 'special' bit.
|
| 1722 |
|
| 1723 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
|
| 1724 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
|
| 1725 | (VM_PFNMAP|VM_MIXEDMAP));
|
| 1726 | BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
|
| 1727 |
|
| 1728 | pfnmap_setup_cachemode_pfn(pfn, &pgprot);
|
| 1729 |
|
| 1730 | return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
|
| 1731 | }
|
| 1732 | EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
|
| 1733 |
|
| 1734 | vm_fault_t vmf_insert_folio_pmd(struct vm_fault *vmf, struct folio *folio,
|
| 1735 | bool write)
|
| 1736 | {
|
| 1737 | struct vm_area_struct *vma = vmf->vma;
|
| 1738 | unsigned long addr = vmf->address & PMD_MASK;
|
| 1739 | struct folio_or_pfn fop = {
|
| 1740 | .folio = folio,
|
| 1741 | .is_folio = true,
|
| 1742 | };
|
| 1743 |
|
| 1744 | if (WARN_ON_ONCE(folio_order(folio) != PMD_ORDER))
|
| 1745 | return VM_FAULT_SIGBUS;
|
| 1746 |
|
| 1747 | return insert_pmd(vma, addr, vmf->pmd, fop, vma->vm_page_prot, write);
|
| 1748 | }
|
| 1749 | EXPORT_SYMBOL_GPL(vmf_insert_folio_pmd);
|
| 1750 |
|
| 1751 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
|
| 1752 | static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
|
| 1753 | {
|
| 1754 | if (likely(vma->vm_flags & VM_WRITE))
|
| 1755 | pud = pud_mkwrite(pud);
|
| 1756 | return pud;
|
| 1757 | }
|
| 1758 |
|
| 1759 | static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
|
| 1760 | pud_t *pud, struct folio_or_pfn fop, pgprot_t prot, bool write)
|
| 1761 | {
|
| 1762 | struct mm_struct *mm = vma->vm_mm;
|
| 1763 | spinlock_t *ptl;
|
| 1764 | pud_t entry;
|
| 1765 |
|
| 1766 | if (addr < vma->vm_start || addr >= vma->vm_end)
|
| 1767 | return VM_FAULT_SIGBUS;
|
| 1768 |
|
| 1769 | ptl = pud_lock(mm, pud);
|
| 1770 | if (!pud_none(*pud)) {
|
| 1771 | const unsigned long pfn = fop.is_folio ? folio_pfn(fop.folio) :
|
| 1772 | fop.pfn;
|
| 1773 |
|
| 1774 | if (write) {
|
| 1775 | if (WARN_ON_ONCE(pud_pfn(*pud) != pfn))
|
| 1776 | goto out_unlock;
|
| 1777 | entry = pud_mkyoung(*pud);
|
| 1778 | entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
|
| 1779 | if (pudp_set_access_flags(vma, addr, pud, entry, 1))
|
| 1780 | update_mmu_cache_pud(vma, addr, pud);
|
| 1781 | }
|
| 1782 | goto out_unlock;
|
| 1783 | }
|
| 1784 |
|
| 1785 | if (fop.is_folio) {
|
| 1786 | entry = folio_mk_pud(fop.folio, vma->vm_page_prot);
|
| 1787 |
|
| 1788 | folio_get(fop.folio);
|
| 1789 | folio_add_file_rmap_pud(fop.folio, &fop.folio->page, vma);
|
| 1790 | add_mm_counter(mm, mm_counter_file(fop.folio), HPAGE_PUD_NR);
|
| 1791 | } else {
|
| 1792 | entry = pud_mkhuge(pfn_pud(fop.pfn, prot));
|
| 1793 | entry = pud_mkspecial(entry);
|
| 1794 | }
|
| 1795 | if (write) {
|
| 1796 | entry = pud_mkyoung(pud_mkdirty(entry));
|
| 1797 | entry = maybe_pud_mkwrite(entry, vma);
|
| 1798 | }
|
| 1799 | set_pud_at(mm, addr, pud, entry);
|
| 1800 | update_mmu_cache_pud(vma, addr, pud);
|
| 1801 | out_unlock:
|
| 1802 | spin_unlock(ptl);
|
| 1803 | return VM_FAULT_NOPAGE;
|
| 1804 | }
|
| 1805 |
|
| 1806 | |
| 1807 | * vmf_insert_pfn_pud - insert a pud size pfn
|
| 1808 | * @vmf: Structure describing the fault
|
| 1809 | * @pfn: pfn to insert
|
| 1810 | * @write: whether it's a write fault
|
| 1811 | *
|
| 1812 | * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
|
| 1813 | *
|
| 1814 | * Return: vm_fault_t value.
|
| 1815 |
|
| 1816 | vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn,
|
| 1817 | bool write)
|
| 1818 | {
|
| 1819 | unsigned long addr = vmf->address & PUD_MASK;
|
| 1820 | struct vm_area_struct *vma = vmf->vma;
|
| 1821 | pgprot_t pgprot = vma->vm_page_prot;
|
| 1822 | struct folio_or_pfn fop = {
|
| 1823 | .pfn = pfn,
|
| 1824 | };
|
| 1825 |
|
| 1826 | |
| 1827 | * If we had pud_special, we could avoid all these restrictions,
|
| 1828 | * but we need to be consistent with PTEs and architectures that
|
| 1829 | * can't support a 'special' bit.
|
| 1830 |
|
| 1831 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
|
| 1832 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
|
| 1833 | (VM_PFNMAP|VM_MIXEDMAP));
|
| 1834 | BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
|
| 1835 |
|
| 1836 | pfnmap_setup_cachemode_pfn(pfn, &pgprot);
|
| 1837 |
|
| 1838 | return insert_pud(vma, addr, vmf->pud, fop, pgprot, write);
|
| 1839 | }
|
| 1840 | EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud);
|
| 1841 |
|
| 1842 | |
| 1843 | * vmf_insert_folio_pud - insert a pud size folio mapped by a pud entry
|
| 1844 | * @vmf: Structure describing the fault
|
| 1845 | * @folio: folio to insert
|
| 1846 | * @write: whether it's a write fault
|
| 1847 | *
|
| 1848 | * Return: vm_fault_t value.
|
| 1849 |
|
| 1850 | vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf, struct folio *folio,
|
| 1851 | bool write)
|
| 1852 | {
|
| 1853 | struct vm_area_struct *vma = vmf->vma;
|
| 1854 | unsigned long addr = vmf->address & PUD_MASK;
|
| 1855 | struct folio_or_pfn fop = {
|
| 1856 | .folio = folio,
|
| 1857 | .is_folio = true,
|
| 1858 | };
|
| 1859 |
|
| 1860 | if (WARN_ON_ONCE(folio_order(folio) != PUD_ORDER))
|
| 1861 | return VM_FAULT_SIGBUS;
|
| 1862 |
|
| 1863 | return insert_pud(vma, addr, vmf->pud, fop, vma->vm_page_prot, write);
|
| 1864 | }
|
| 1865 | EXPORT_SYMBOL_GPL(vmf_insert_folio_pud);
|
| 1866 | #endif
|
| 1867 |
|
| 1868 | |
| 1869 | * touch_pmd - Mark page table pmd entry as accessed and dirty (for write)
|
| 1870 | * @vma: The VMA covering @addr
|
| 1871 | * @addr: The virtual address
|
| 1872 | * @pmd: pmd pointer into the page table mapping @addr
|
| 1873 | * @write: Whether it's a write access
|
| 1874 | *
|
| 1875 | * Return: whether the pmd entry is changed
|
| 1876 |
|
| 1877 | bool touch_pmd(struct vm_area_struct *vma, unsigned long addr,
|
| 1878 | pmd_t *pmd, bool write)
|
| 1879 | {
|
| 1880 | pmd_t entry;
|
| 1881 |
|
| 1882 | entry = pmd_mkyoung(*pmd);
|
| 1883 | if (write)
|
| 1884 | entry = pmd_mkdirty(entry);
|
| 1885 | if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
|
| 1886 | pmd, entry, write)) {
|
| 1887 | update_mmu_cache_pmd(vma, addr, pmd);
|
| 1888 | return true;
|
| 1889 | }
|
| 1890 |
|
| 1891 | return false;
|
| 1892 | }
|
| 1893 |
|
| 1894 | static void copy_huge_non_present_pmd(
|
| 1895 | struct mm_struct *dst_mm, struct mm_struct *src_mm,
|
| 1896 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
|
| 1897 | struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
| 1898 | pmd_t pmd, pgtable_t pgtable)
|
| 1899 | {
|
| 1900 | softleaf_t entry = softleaf_from_pmd(pmd);
|
| 1901 | struct folio *src_folio;
|
| 1902 |
|
| 1903 | VM_WARN_ON_ONCE(!pmd_is_valid_softleaf(pmd));
|
| 1904 |
|
| 1905 | if (softleaf_is_migration_write(entry) ||
|
| 1906 | softleaf_is_migration_read_exclusive(entry)) {
|
| 1907 | entry = make_readable_migration_entry(swp_offset(entry));
|
| 1908 | pmd = softleaf_to_pmd(entry);
|
| 1909 | if (pmd_swp_soft_dirty(*src_pmd))
|
| 1910 | pmd = pmd_swp_mksoft_dirty(pmd);
|
| 1911 | if (pmd_swp_uffd(*src_pmd))
|
| 1912 | pmd = pmd_swp_mkuffd(pmd);
|
| 1913 | set_pmd_at(src_mm, addr, src_pmd, pmd);
|
| 1914 | } else if (softleaf_is_device_private(entry)) {
|
| 1915 | |
| 1916 | * For device private entries, since there are no
|
| 1917 | * read exclusive entries, writable = !readable
|
| 1918 |
|
| 1919 | if (softleaf_is_device_private_write(entry)) {
|
| 1920 | entry = make_readable_device_private_entry(swp_offset(entry));
|
| 1921 | pmd = softleaf_to_pmd(entry);
|
| 1922 |
|
| 1923 | if (pmd_swp_soft_dirty(*src_pmd))
|
| 1924 | pmd = pmd_swp_mksoft_dirty(pmd);
|
| 1925 | if (pmd_swp_uffd(*src_pmd))
|
| 1926 | pmd = pmd_swp_mkuffd(pmd);
|
| 1927 | set_pmd_at(src_mm, addr, src_pmd, pmd);
|
| 1928 | }
|
| 1929 |
|
| 1930 | src_folio = softleaf_to_folio(entry);
|
| 1931 | VM_WARN_ON(!folio_test_large(src_folio));
|
| 1932 |
|
| 1933 | folio_get(src_folio);
|
| 1934 | |
| 1935 | * folio_try_dup_anon_rmap_pmd does not fail for
|
| 1936 | * device private entries.
|
| 1937 |
|
| 1938 | folio_try_dup_anon_rmap_pmd(src_folio, &src_folio->page,
|
| 1939 | dst_vma, src_vma);
|
| 1940 | }
|
| 1941 |
|
| 1942 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
|
| 1943 | mm_inc_nr_ptes(dst_mm);
|
| 1944 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
|
| 1945 | if (!userfaultfd_protected(dst_vma))
|
| 1946 | pmd = pmd_swp_clear_uffd(pmd);
|
| 1947 | set_pmd_at(dst_mm, addr, dst_pmd, pmd);
|
| 1948 | }
|
| 1949 |
|
| 1950 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
|
| 1951 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
|
| 1952 | struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
|
| 1953 | {
|
| 1954 | spinlock_t *dst_ptl, *src_ptl;
|
| 1955 | struct page *src_page;
|
| 1956 | struct folio *src_folio;
|
| 1957 | pmd_t pmd;
|
| 1958 | pgtable_t pgtable = NULL;
|
| 1959 | int ret = -ENOMEM;
|
| 1960 |
|
| 1961 | pmd = pmdp_get_lockless(src_pmd);
|
| 1962 | if (unlikely(pmd_present(pmd) && pmd_special(pmd) &&
|
| 1963 | !is_huge_zero_pmd(pmd))) {
|
| 1964 | dst_ptl = pmd_lock(dst_mm, dst_pmd);
|
| 1965 | src_ptl = pmd_lockptr(src_mm, src_pmd);
|
| 1966 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
|
| 1967 | |
| 1968 | * No need to recheck the pmd, it can't change with write
|
| 1969 | * mmap lock held here.
|
| 1970 | *
|
| 1971 | * Meanwhile, making sure it's not a CoW VMA with writable
|
| 1972 | * mapping, otherwise it means either the anon page wrongly
|
| 1973 | * applied special bit, or we made the PRIVATE mapping be
|
| 1974 | * able to wrongly write to the backend MMIO.
|
| 1975 |
|
| 1976 | VM_WARN_ON_ONCE(vma_is_cow_mapping(src_vma) && pmd_write(pmd));
|
| 1977 | goto set_pmd;
|
| 1978 | }
|
| 1979 |
|
| 1980 |
|
| 1981 | if (!vma_is_anonymous(dst_vma))
|
| 1982 | return 0;
|
| 1983 |
|
| 1984 | pgtable = pte_alloc_one(dst_mm);
|
| 1985 | if (unlikely(!pgtable))
|
| 1986 | goto out;
|
| 1987 |
|
| 1988 | dst_ptl = pmd_lock(dst_mm, dst_pmd);
|
| 1989 | src_ptl = pmd_lockptr(src_mm, src_pmd);
|
| 1990 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
|
| 1991 |
|
| 1992 | ret = -EAGAIN;
|
| 1993 | pmd = *src_pmd;
|
| 1994 |
|
| 1995 | if (unlikely(thp_migration_supported() &&
|
| 1996 | pmd_is_valid_softleaf(pmd))) {
|
| 1997 | copy_huge_non_present_pmd(dst_mm, src_mm, dst_pmd, src_pmd, addr,
|
| 1998 | dst_vma, src_vma, pmd, pgtable);
|
| 1999 | ret = 0;
|
| 2000 | goto out_unlock;
|
| 2001 | }
|
| 2002 |
|
| 2003 | if (unlikely(!pmd_trans_huge(pmd))) {
|
| 2004 | pte_free(dst_mm, pgtable);
|
| 2005 | goto out_unlock;
|
| 2006 | }
|
| 2007 | |
| 2008 | * When page table lock is held, the huge zero pmd should not be
|
| 2009 | * under splitting since we don't split the page itself, only pmd to
|
| 2010 | * a page table.
|
| 2011 |
|
| 2012 | if (is_huge_zero_pmd(pmd)) {
|
| 2013 | |
| 2014 | * mm_get_huge_zero_folio() will never allocate a new
|
| 2015 | * folio here, since we already have a zero page to
|
| 2016 | * copy. It just takes a reference.
|
| 2017 |
|
| 2018 | mm_get_huge_zero_folio(dst_mm);
|
| 2019 | goto out_zero_page;
|
| 2020 | }
|
| 2021 |
|
| 2022 | src_page = pmd_page(pmd);
|
| 2023 | VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
|
| 2024 | src_folio = page_folio(src_page);
|
| 2025 |
|
| 2026 | folio_get(src_folio);
|
| 2027 | if (unlikely(folio_try_dup_anon_rmap_pmd(src_folio, src_page, dst_vma, src_vma))) {
|
| 2028 |
|
| 2029 | folio_put(src_folio);
|
| 2030 | pte_free(dst_mm, pgtable);
|
| 2031 | spin_unlock(src_ptl);
|
| 2032 | spin_unlock(dst_ptl);
|
| 2033 | __split_huge_pmd(src_vma, src_pmd, addr, false);
|
| 2034 | return -EAGAIN;
|
| 2035 | }
|
| 2036 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
|
| 2037 | out_zero_page:
|
| 2038 | mm_inc_nr_ptes(dst_mm);
|
| 2039 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
|
| 2040 |
|
| 2041 |
|
| 2042 | if (!userfaultfd_protected(dst_vma)) {
|
| 2043 | if (userfaultfd_rwp(src_vma) && pmd_uffd(pmd))
|
| 2044 | pmd = pmd_modify(pmd, dst_vma->vm_page_prot);
|
| 2045 | pmd = pmd_clear_uffd(pmd);
|
| 2046 | }
|
| 2047 |
|
| 2048 | pmdp_set_wrprotect(src_mm, addr, src_pmd);
|
| 2049 | pmd = pmd_wrprotect(pmd);
|
| 2050 | set_pmd:
|
| 2051 | pmd = pmd_mkold(pmd);
|
| 2052 | set_pmd_at(dst_mm, addr, dst_pmd, pmd);
|
| 2053 |
|
| 2054 | ret = 0;
|
| 2055 | out_unlock:
|
| 2056 | spin_unlock(src_ptl);
|
| 2057 | spin_unlock(dst_ptl);
|
| 2058 | out:
|
| 2059 | return ret;
|
| 2060 | }
|
| 2061 |
|
| 2062 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
|
| 2063 | void touch_pud(struct vm_area_struct *vma, unsigned long addr,
|
| 2064 | pud_t *pud, bool write)
|
| 2065 | {
|
| 2066 | pud_t _pud;
|
| 2067 |
|
| 2068 | _pud = pud_mkyoung(*pud);
|
| 2069 | if (write)
|
| 2070 | _pud = pud_mkdirty(_pud);
|
| 2071 | if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
|
| 2072 | pud, _pud, write))
|
| 2073 | update_mmu_cache_pud(vma, addr, pud);
|
| 2074 | }
|
| 2075 |
|
| 2076 | int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
|
| 2077 | pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
|
| 2078 | struct vm_area_struct *vma)
|
| 2079 | {
|
| 2080 | spinlock_t *dst_ptl, *src_ptl;
|
| 2081 | pud_t pud;
|
| 2082 | int ret;
|
| 2083 |
|
| 2084 | dst_ptl = pud_lock(dst_mm, dst_pud);
|
| 2085 | src_ptl = pud_lockptr(src_mm, src_pud);
|
| 2086 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
|
| 2087 |
|
| 2088 | ret = -EAGAIN;
|
| 2089 | pud = *src_pud;
|
| 2090 | if (unlikely(!pud_trans_huge(pud)))
|
| 2091 | goto out_unlock;
|
| 2092 |
|
| 2093 | |
| 2094 | * TODO: once we support anonymous pages, use
|
| 2095 | * folio_try_dup_anon_rmap_*() and split if duplicating fails.
|
| 2096 |
|
| 2097 | if (vma_is_cow_mapping(vma) && pud_write(pud)) {
|
| 2098 | pudp_set_wrprotect(src_mm, addr, src_pud);
|
| 2099 | pud = pud_wrprotect(pud);
|
| 2100 | }
|
| 2101 | pud = pud_mkold(pud);
|
| 2102 | set_pud_at(dst_mm, addr, dst_pud, pud);
|
| 2103 |
|
| 2104 | ret = 0;
|
| 2105 | out_unlock:
|
| 2106 | spin_unlock(src_ptl);
|
| 2107 | spin_unlock(dst_ptl);
|
| 2108 | return ret;
|
| 2109 | }
|
| 2110 |
|
| 2111 | void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
|
| 2112 | {
|
| 2113 | bool write = vmf->flags & FAULT_FLAG_WRITE;
|
| 2114 |
|
| 2115 | vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
|
| 2116 | if (unlikely(!pud_same(*vmf->pud, orig_pud)))
|
| 2117 | goto unlock;
|
| 2118 |
|
| 2119 | touch_pud(vmf->vma, vmf->address, vmf->pud, write);
|
| 2120 | unlock:
|
| 2121 | spin_unlock(vmf->ptl);
|
| 2122 | }
|
| 2123 | #endif
|
| 2124 |
|
| 2125 | bool huge_pmd_set_accessed(struct vm_fault *vmf)
|
| 2126 | {
|
| 2127 | bool write = vmf->flags & FAULT_FLAG_WRITE;
|
| 2128 |
|
| 2129 | if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd)))
|
| 2130 | return false;
|
| 2131 |
|
| 2132 | return touch_pmd(vmf->vma, vmf->address, vmf->pmd, write);
|
| 2133 | }
|
| 2134 |
|
| 2135 | static vm_fault_t do_huge_zero_wp_pmd(struct vm_fault *vmf)
|
| 2136 | {
|
| 2137 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
| 2138 | struct vm_area_struct *vma = vmf->vma;
|
| 2139 | struct mmu_notifier_range range;
|
| 2140 | struct folio *folio;
|
| 2141 | vm_fault_t ret = 0;
|
| 2142 |
|
| 2143 | folio = vma_alloc_anon_folio_pmd(vma, vmf->address);
|
| 2144 | if (unlikely(!folio))
|
| 2145 | return VM_FAULT_FALLBACK;
|
| 2146 |
|
| 2147 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, haddr,
|
| 2148 | haddr + HPAGE_PMD_SIZE);
|
| 2149 | mmu_notifier_invalidate_range_start(&range);
|
| 2150 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 2151 | if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd)))
|
| 2152 | goto release;
|
| 2153 | ret = check_stable_address_space(vma->vm_mm);
|
| 2154 | if (ret)
|
| 2155 | goto release;
|
| 2156 | (void)pmdp_huge_clear_flush(vma, haddr, vmf->pmd);
|
| 2157 | map_anon_folio_pmd_pf(folio, vmf->pmd, vma, haddr);
|
| 2158 | goto unlock;
|
| 2159 | release:
|
| 2160 | folio_put(folio);
|
| 2161 | unlock:
|
| 2162 | spin_unlock(vmf->ptl);
|
| 2163 | mmu_notifier_invalidate_range_end(&range);
|
| 2164 | return ret;
|
| 2165 | }
|
| 2166 |
|
| 2167 | vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
|
| 2168 | {
|
| 2169 | const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
|
| 2170 | struct vm_area_struct *vma = vmf->vma;
|
| 2171 | struct folio *folio;
|
| 2172 | struct page *page;
|
| 2173 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
| 2174 | pmd_t orig_pmd = vmf->orig_pmd;
|
| 2175 |
|
| 2176 | vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
|
| 2177 | VM_BUG_ON_VMA(!vma->anon_vma, vma);
|
| 2178 |
|
| 2179 | if (is_huge_zero_pmd(orig_pmd)) {
|
| 2180 | vm_fault_t ret = do_huge_zero_wp_pmd(vmf);
|
| 2181 |
|
| 2182 | if (!(ret & VM_FAULT_FALLBACK))
|
| 2183 | return ret;
|
| 2184 |
|
| 2185 |
|
| 2186 | goto fallback;
|
| 2187 | }
|
| 2188 |
|
| 2189 | spin_lock(vmf->ptl);
|
| 2190 |
|
| 2191 | if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
|
| 2192 | spin_unlock(vmf->ptl);
|
| 2193 | return 0;
|
| 2194 | }
|
| 2195 |
|
| 2196 | page = pmd_page(orig_pmd);
|
| 2197 | folio = page_folio(page);
|
| 2198 | VM_BUG_ON_PAGE(!PageHead(page), page);
|
| 2199 |
|
| 2200 |
|
| 2201 | if (PageAnonExclusive(page))
|
| 2202 | goto reuse;
|
| 2203 |
|
| 2204 | if (!folio_trylock(folio)) {
|
| 2205 | folio_get(folio);
|
| 2206 | spin_unlock(vmf->ptl);
|
| 2207 | folio_lock(folio);
|
| 2208 | spin_lock(vmf->ptl);
|
| 2209 | if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
|
| 2210 | spin_unlock(vmf->ptl);
|
| 2211 | folio_unlock(folio);
|
| 2212 | folio_put(folio);
|
| 2213 | return 0;
|
| 2214 | }
|
| 2215 | folio_put(folio);
|
| 2216 | }
|
| 2217 |
|
| 2218 |
|
| 2219 | if (PageAnonExclusive(page)) {
|
| 2220 | folio_unlock(folio);
|
| 2221 | goto reuse;
|
| 2222 | }
|
| 2223 |
|
| 2224 | |
| 2225 | * See do_wp_page(): we can only reuse the folio exclusively if
|
| 2226 | * there are no additional references. Note that we always drain
|
| 2227 | * the LRU cache immediately after adding a THP.
|
| 2228 |
|
| 2229 | if (folio_ref_count(folio) >
|
| 2230 | 1 + folio_test_swapcache(folio) * folio_nr_pages(folio))
|
| 2231 | goto unlock_fallback;
|
| 2232 | if (folio_test_swapcache(folio))
|
| 2233 | folio_free_swap(folio);
|
| 2234 | if (folio_ref_count(folio) == 1) {
|
| 2235 | pmd_t entry;
|
| 2236 |
|
| 2237 | folio_move_anon_rmap(folio, vma);
|
| 2238 | SetPageAnonExclusive(page);
|
| 2239 | folio_unlock(folio);
|
| 2240 | reuse:
|
| 2241 | if (unlikely(unshare)) {
|
| 2242 | spin_unlock(vmf->ptl);
|
| 2243 | return 0;
|
| 2244 | }
|
| 2245 | entry = pmd_mkyoung(orig_pmd);
|
| 2246 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
|
| 2247 | if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
|
| 2248 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
|
| 2249 | spin_unlock(vmf->ptl);
|
| 2250 | return 0;
|
| 2251 | }
|
| 2252 |
|
| 2253 | unlock_fallback:
|
| 2254 | folio_unlock(folio);
|
| 2255 | spin_unlock(vmf->ptl);
|
| 2256 | fallback:
|
| 2257 | __split_huge_pmd(vma, vmf->pmd, vmf->address, false);
|
| 2258 | return VM_FAULT_FALLBACK;
|
| 2259 | }
|
| 2260 |
|
| 2261 | static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
|
| 2262 | unsigned long addr, pmd_t pmd)
|
| 2263 | {
|
| 2264 | struct page *page;
|
| 2265 |
|
| 2266 | if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
|
| 2267 | return false;
|
| 2268 |
|
| 2269 |
|
| 2270 | if (pmd_protnone(pmd))
|
| 2271 | return false;
|
| 2272 |
|
| 2273 |
|
| 2274 | if (pmd_needs_soft_dirty_wp(vma, pmd))
|
| 2275 | return false;
|
| 2276 |
|
| 2277 |
|
| 2278 | if (userfaultfd_huge_pmd_wp(vma, pmd))
|
| 2279 | return false;
|
| 2280 |
|
| 2281 | if (!(vma->vm_flags & VM_SHARED)) {
|
| 2282 |
|
| 2283 | page = vm_normal_page_pmd(vma, addr, pmd);
|
| 2284 | return page && PageAnon(page) && PageAnonExclusive(page);
|
| 2285 | }
|
| 2286 |
|
| 2287 |
|
| 2288 | return pmd_dirty(pmd);
|
| 2289 | }
|
| 2290 |
|
| 2291 | vm_fault_t do_huge_pmd_uffd_rwp(struct vm_fault *vmf)
|
| 2292 | {
|
| 2293 | struct vm_area_struct *vma = vmf->vma;
|
| 2294 | pmd_t pmd;
|
| 2295 |
|
| 2296 | if (!userfaultfd_rwp_async(vma))
|
| 2297 | return handle_userfault(vmf, VM_UFFD_RWP);
|
| 2298 |
|
| 2299 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 2300 | if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd))) {
|
| 2301 | spin_unlock(vmf->ptl);
|
| 2302 | return 0;
|
| 2303 | }
|
| 2304 | pmd = pmd_modify(vmf->orig_pmd, vma->vm_page_prot);
|
| 2305 |
|
| 2306 | pmd = pmd_clear_uffd(pmd);
|
| 2307 | pmd = pmd_mkyoung(pmd);
|
| 2308 | if (!pmd_write(pmd) &&
|
| 2309 | vma_wants_manual_pte_write_upgrade(vma) &&
|
| 2310 | can_change_pmd_writable(vma, vmf->address, pmd))
|
| 2311 | pmd = pmd_mkwrite(pmd, vma);
|
| 2312 | set_pmd_at(vma->vm_mm, vmf->address & HPAGE_PMD_MASK,
|
| 2313 | vmf->pmd, pmd);
|
| 2314 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
|
| 2315 | spin_unlock(vmf->ptl);
|
| 2316 | return 0;
|
| 2317 | }
|
| 2318 |
|
| 2319 |
|
| 2320 | vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
|
| 2321 | {
|
| 2322 | struct vm_area_struct *vma = vmf->vma;
|
| 2323 | struct folio *folio;
|
| 2324 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
|
| 2325 | int nid = NUMA_NO_NODE;
|
| 2326 | int target_nid, last_cpupid;
|
| 2327 | pmd_t pmd, old_pmd;
|
| 2328 | bool writable = false;
|
| 2329 | int flags = 0;
|
| 2330 |
|
| 2331 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 2332 | old_pmd = pmdp_get(vmf->pmd);
|
| 2333 |
|
| 2334 | if (unlikely(!pmd_same(old_pmd, vmf->orig_pmd))) {
|
| 2335 | spin_unlock(vmf->ptl);
|
| 2336 | return 0;
|
| 2337 | }
|
| 2338 |
|
| 2339 | pmd = pmd_modify(old_pmd, vma->vm_page_prot);
|
| 2340 |
|
| 2341 | |
| 2342 | * Detect now whether the PMD could be writable; this information
|
| 2343 | * is only valid while holding the PT lock.
|
| 2344 |
|
| 2345 | writable = pmd_write(pmd);
|
| 2346 | if (!writable && vma_wants_manual_pte_write_upgrade(vma) &&
|
| 2347 | can_change_pmd_writable(vma, vmf->address, pmd))
|
| 2348 | writable = true;
|
| 2349 |
|
| 2350 | folio = vm_normal_folio_pmd(vma, haddr, pmd);
|
| 2351 | if (!folio)
|
| 2352 | goto out_map;
|
| 2353 |
|
| 2354 | nid = folio_nid(folio);
|
| 2355 |
|
| 2356 | target_nid = numa_migrate_check(folio, vmf, haddr, &flags, writable,
|
| 2357 | &last_cpupid);
|
| 2358 | if (target_nid == NUMA_NO_NODE)
|
| 2359 | goto out_map;
|
| 2360 | if (migrate_misplaced_folio_prepare(folio, vma, target_nid)) {
|
| 2361 | flags |= TNF_MIGRATE_FAIL;
|
| 2362 | goto out_map;
|
| 2363 | }
|
| 2364 |
|
| 2365 | spin_unlock(vmf->ptl);
|
| 2366 | writable = false;
|
| 2367 |
|
| 2368 | if (!migrate_misplaced_folio(folio, target_nid)) {
|
| 2369 | flags |= TNF_MIGRATED;
|
| 2370 | nid = target_nid;
|
| 2371 | task_numa_fault(last_cpupid, nid, HPAGE_PMD_NR, flags);
|
| 2372 | return 0;
|
| 2373 | }
|
| 2374 |
|
| 2375 | flags |= TNF_MIGRATE_FAIL;
|
| 2376 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
|
| 2377 | if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd))) {
|
| 2378 | spin_unlock(vmf->ptl);
|
| 2379 | return 0;
|
| 2380 | }
|
| 2381 | out_map:
|
| 2382 |
|
| 2383 | pmd = pmd_modify(pmdp_get(vmf->pmd), vma->vm_page_prot);
|
| 2384 | pmd = pmd_mkyoung(pmd);
|
| 2385 | if (writable)
|
| 2386 | pmd = pmd_mkwrite(pmd, vma);
|
| 2387 | set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
|
| 2388 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
|
| 2389 | spin_unlock(vmf->ptl);
|
| 2390 |
|
| 2391 | if (nid != NUMA_NO_NODE)
|
| 2392 | task_numa_fault(last_cpupid, nid, HPAGE_PMD_NR, flags);
|
| 2393 | return 0;
|
| 2394 | }
|
| 2395 |
|
| 2396 | |
| 2397 | * Return true if we do MADV_FREE successfully on entire pmd page.
|
| 2398 | * Otherwise, return false.
|
| 2399 |
|
| 2400 | bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
| 2401 | pmd_t *pmd, unsigned long addr, unsigned long next)
|
| 2402 | {
|
| 2403 | spinlock_t *ptl;
|
| 2404 | pmd_t orig_pmd;
|
| 2405 | struct folio *folio;
|
| 2406 | struct mm_struct *mm = tlb->mm;
|
| 2407 | bool ret = false;
|
| 2408 |
|
| 2409 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
|
| 2410 |
|
| 2411 | ptl = pmd_trans_huge_lock(pmd, vma);
|
| 2412 | if (!ptl)
|
| 2413 | goto out_unlocked;
|
| 2414 |
|
| 2415 | orig_pmd = *pmd;
|
| 2416 | if (is_huge_zero_pmd(orig_pmd))
|
| 2417 | goto out;
|
| 2418 |
|
| 2419 | if (unlikely(!pmd_present(orig_pmd))) {
|
| 2420 | VM_WARN_ON_ONCE(!pmd_is_migration_entry(orig_pmd) &&
|
| 2421 | !pmd_is_device_private_entry(orig_pmd));
|
| 2422 | goto out;
|
| 2423 | }
|
| 2424 |
|
| 2425 | folio = pmd_folio(orig_pmd);
|
| 2426 | |
| 2427 | * If other processes are mapping this folio, we couldn't discard
|
| 2428 | * the folio unless they all do MADV_FREE so let's skip the folio.
|
| 2429 |
|
| 2430 | if (folio_maybe_mapped_shared(folio))
|
| 2431 | goto out;
|
| 2432 |
|
| 2433 | if (!folio_trylock(folio))
|
| 2434 | goto out;
|
| 2435 |
|
| 2436 | |
| 2437 | * If user want to discard part-pages of THP, split it so MADV_FREE
|
| 2438 | * will deactivate only them.
|
| 2439 |
|
| 2440 | if (next - addr != HPAGE_PMD_SIZE) {
|
| 2441 | folio_get(folio);
|
| 2442 | spin_unlock(ptl);
|
| 2443 | split_folio(folio);
|
| 2444 | folio_unlock(folio);
|
| 2445 | folio_put(folio);
|
| 2446 | goto out_unlocked;
|
| 2447 | }
|
| 2448 |
|
| 2449 | if (folio_test_dirty(folio))
|
| 2450 | folio_clear_dirty(folio);
|
| 2451 | folio_unlock(folio);
|
| 2452 |
|
| 2453 | if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
|
| 2454 | pmdp_invalidate(vma, addr, pmd);
|
| 2455 | orig_pmd = pmd_mkold(orig_pmd);
|
| 2456 | orig_pmd = pmd_mkclean(orig_pmd);
|
| 2457 |
|
| 2458 | set_pmd_at(mm, addr, pmd, orig_pmd);
|
| 2459 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
|
| 2460 | }
|
| 2461 |
|
| 2462 | folio_mark_lazyfree(folio);
|
| 2463 | ret = true;
|
| 2464 | out:
|
| 2465 | spin_unlock(ptl);
|
| 2466 | out_unlocked:
|
| 2467 | return ret;
|
| 2468 | }
|
| 2469 |
|
| 2470 | static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
|
| 2471 | {
|
| 2472 | pgtable_t pgtable;
|
| 2473 |
|
| 2474 | pgtable = pgtable_trans_huge_withdraw(mm, pmd);
|
| 2475 | pte_free(mm, pgtable);
|
| 2476 | mm_dec_nr_ptes(mm);
|
| 2477 | }
|
| 2478 |
|
| 2479 | static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct *vma,
|
| 2480 | pmd_t pmdval, struct folio *folio, bool is_present)
|
| 2481 | {
|
| 2482 | const bool is_device_private = folio_is_device_private(folio);
|
| 2483 |
|
| 2484 |
|
| 2485 | if (is_present || is_device_private)
|
| 2486 | folio_remove_rmap_pmd(folio, &folio->page, vma);
|
| 2487 |
|
| 2488 | if (folio_test_anon(folio)) {
|
| 2489 | add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
|
| 2490 | } else {
|
| 2491 | add_mm_counter(mm, mm_counter_file(folio),
|
| 2492 | -HPAGE_PMD_NR);
|
| 2493 |
|
| 2494 | if (is_present && pmd_dirty(pmdval))
|
| 2495 | folio_mark_dirty(folio);
|
| 2496 | if (is_present && pmd_young(pmdval) &&
|
| 2497 | likely(vma_has_recency(vma)))
|
| 2498 | folio_mark_accessed(folio);
|
| 2499 | }
|
| 2500 |
|
| 2501 |
|
| 2502 | if (is_device_private)
|
| 2503 | folio_put(folio);
|
| 2504 | }
|
| 2505 |
|
| 2506 | static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *vma,
|
| 2507 | unsigned long addr, pmd_t pmdval, bool is_present)
|
| 2508 | {
|
| 2509 | if (is_present)
|
| 2510 | return vm_normal_folio_pmd(vma, addr, pmdval);
|
| 2511 |
|
| 2512 | if (!thp_migration_supported())
|
| 2513 | WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
|
| 2514 | return pmd_to_softleaf_folio(pmdval);
|
| 2515 | }
|
| 2516 |
|
| 2517 | static bool has_deposited_pgtable(struct vm_area_struct *vma, pmd_t pmdval,
|
| 2518 | struct folio *folio)
|
| 2519 | {
|
| 2520 |
|
| 2521 | if (arch_needs_pgtable_deposit())
|
| 2522 | return true;
|
| 2523 |
|
| 2524 | |
| 2525 | * Huge zero always deposited except for DAX which handles itself, see
|
| 2526 | * set_huge_zero_folio().
|
| 2527 |
|
| 2528 | if (is_huge_zero_pmd(pmdval))
|
| 2529 | return !vma_is_dax(vma);
|
| 2530 |
|
| 2531 | |
| 2532 | * Otherwise, only anonymous folios are deposited, see
|
| 2533 | * __do_huge_pmd_anonymous_page().
|
| 2534 |
|
| 2535 | return folio && folio_test_anon(folio);
|
| 2536 | }
|
| 2537 |
|
| 2538 | |
| 2539 | * zap_huge_pmd - Zap a huge THP which is of PMD size.
|
| 2540 | * @tlb: The MMU gather TLB state associated with the operation.
|
| 2541 | * @vma: The VMA containing the range to zap.
|
| 2542 | * @pmd: A pointer to the leaf PMD entry.
|
| 2543 | * @addr: The virtual address for the range to zap.
|
| 2544 | *
|
| 2545 | * Returns: %true on success, %false otherwise.
|
| 2546 |
|
| 2547 | bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
| 2548 | pmd_t *pmd, unsigned long addr)
|
| 2549 | {
|
| 2550 | struct mm_struct *mm = tlb->mm;
|
| 2551 | struct folio *folio = NULL;
|
| 2552 | bool is_present = false;
|
| 2553 | bool has_deposit;
|
| 2554 | spinlock_t *ptl;
|
| 2555 | pmd_t orig_pmd;
|
| 2556 |
|
| 2557 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
|
| 2558 |
|
| 2559 | ptl = __pmd_trans_huge_lock(pmd, vma);
|
| 2560 | if (!ptl)
|
| 2561 | return false;
|
| 2562 | |
| 2563 | * For architectures like ppc64 we look at deposited pgtable
|
| 2564 | * when calling pmdp_huge_get_and_clear. So do the
|
| 2565 | * pgtable_trans_huge_withdraw after finishing pmdp related
|
| 2566 | * operations.
|
| 2567 |
|
| 2568 | orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
|
| 2569 | tlb->fullmm);
|
| 2570 | arch_check_zapped_pmd(vma, orig_pmd);
|
| 2571 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
|
| 2572 |
|
| 2573 | is_present = pmd_present(orig_pmd);
|
| 2574 | folio = normal_or_softleaf_folio_pmd(vma, addr, orig_pmd, is_present);
|
| 2575 | has_deposit = has_deposited_pgtable(vma, orig_pmd, folio);
|
| 2576 | if (folio)
|
| 2577 | zap_huge_pmd_folio(mm, vma, orig_pmd, folio, is_present);
|
| 2578 | if (has_deposit)
|
| 2579 | zap_deposited_table(mm, pmd);
|
| 2580 |
|
| 2581 | spin_unlock(ptl);
|
| 2582 | if (is_present && folio)
|
| 2583 | tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE);
|
| 2584 | return true;
|
| 2585 | }
|
| 2586 |
|
| 2587 | #ifndef pmd_move_must_withdraw
|
| 2588 | static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
|
| 2589 | spinlock_t *old_pmd_ptl,
|
| 2590 | struct vm_area_struct *vma)
|
| 2591 | {
|
| 2592 | |
| 2593 | * With split pmd lock we also need to move preallocated
|
| 2594 | * PTE page table if new_pmd is on different PMD page table.
|
| 2595 | *
|
| 2596 | * We also don't deposit and withdraw tables for file pages.
|
| 2597 |
|
| 2598 | return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
|
| 2599 | }
|
| 2600 | #endif
|
| 2601 |
|
| 2602 | static pmd_t move_soft_dirty_pmd(pmd_t pmd)
|
| 2603 | {
|
| 2604 | if (pgtable_supports_soft_dirty()) {
|
| 2605 | if (unlikely(pmd_is_migration_entry(pmd)))
|
| 2606 | pmd = pmd_swp_mksoft_dirty(pmd);
|
| 2607 | else if (pmd_present(pmd))
|
| 2608 | pmd = pmd_mksoft_dirty(pmd);
|
| 2609 | }
|
| 2610 |
|
| 2611 | return pmd;
|
| 2612 | }
|
| 2613 |
|
| 2614 | static pmd_t clear_uffd_wp_pmd(pmd_t pmd)
|
| 2615 | {
|
| 2616 | if (pmd_none(pmd))
|
| 2617 | return pmd;
|
| 2618 | if (pmd_present(pmd))
|
| 2619 | pmd = pmd_clear_uffd(pmd);
|
| 2620 | else
|
| 2621 | pmd = pmd_swp_clear_uffd(pmd);
|
| 2622 |
|
| 2623 | return pmd;
|
| 2624 | }
|
| 2625 |
|
| 2626 | bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
|
| 2627 | unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
|
| 2628 | {
|
| 2629 | spinlock_t *old_ptl, *new_ptl;
|
| 2630 | pmd_t pmd;
|
| 2631 | struct mm_struct *mm = vma->vm_mm;
|
| 2632 | bool force_flush = false;
|
| 2633 |
|
| 2634 | |
| 2635 | * The destination pmd shouldn't be established, free_pgtables()
|
| 2636 | * should have released it; but move_page_tables() might have already
|
| 2637 | * inserted a page table, if racing against shmem/file collapse.
|
| 2638 |
|
| 2639 | if (!pmd_none(*new_pmd)) {
|
| 2640 | VM_BUG_ON(pmd_trans_huge(*new_pmd));
|
| 2641 | return false;
|
| 2642 | }
|
| 2643 |
|
| 2644 | |
| 2645 | * We don't have to worry about the ordering of src and dst
|
| 2646 | * ptlocks because exclusive mmap_lock prevents deadlock.
|
| 2647 |
|
| 2648 | old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
|
| 2649 | if (old_ptl) {
|
| 2650 | new_ptl = pmd_lockptr(mm, new_pmd);
|
| 2651 | if (new_ptl != old_ptl)
|
| 2652 | spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
|
| 2653 | pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
|
| 2654 | if (pmd_present(pmd))
|
| 2655 | force_flush = true;
|
| 2656 | VM_BUG_ON(!pmd_none(*new_pmd));
|
| 2657 |
|
| 2658 | if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
|
| 2659 | pgtable_t pgtable;
|
| 2660 | pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
|
| 2661 | pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
|
| 2662 | }
|
| 2663 | pmd = move_soft_dirty_pmd(pmd);
|
| 2664 | if (vma_has_uffd_without_event_remap(vma)) {
|
| 2665 | |
| 2666 | * See __copy_present_ptes(): normalise the RWP marker
|
| 2667 | * so the destination starts accessible instead of
|
| 2668 | * taking a numa-hinting fault on first access. Only the
|
| 2669 | * marker (protnone + uffd) needs it; leave other present
|
| 2670 | * PMDs in the VMA untouched.
|
| 2671 |
|
| 2672 | if (pmd_present(pmd) && userfaultfd_rwp(vma) &&
|
| 2673 | pmd_uffd(pmd))
|
| 2674 | pmd = pmd_modify(pmd, vma->vm_page_prot);
|
| 2675 | pmd = clear_uffd_wp_pmd(pmd);
|
| 2676 | }
|
| 2677 | set_pmd_at(mm, new_addr, new_pmd, pmd);
|
| 2678 | if (force_flush)
|
| 2679 | flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
|
| 2680 | if (new_ptl != old_ptl)
|
| 2681 | spin_unlock(new_ptl);
|
| 2682 | spin_unlock(old_ptl);
|
| 2683 | return true;
|
| 2684 | }
|
| 2685 | return false;
|
| 2686 | }
|
| 2687 |
|
| 2688 | static void change_non_present_huge_pmd(struct mm_struct *mm,
|
| 2689 | unsigned long addr, pmd_t *pmd, bool uffd_prot,
|
| 2690 | bool uffd_prot_resolve)
|
| 2691 | {
|
| 2692 | softleaf_t entry = softleaf_from_pmd(*pmd);
|
| 2693 | pmd_t newpmd;
|
| 2694 |
|
| 2695 | VM_WARN_ON(!pmd_is_valid_softleaf(*pmd));
|
| 2696 | if (softleaf_is_migration_write(entry)) {
|
| 2697 | const struct folio *folio = softleaf_to_folio(entry);
|
| 2698 |
|
| 2699 | |
| 2700 | * A protection check is difficult so
|
| 2701 | * just be safe and disable write
|
| 2702 |
|
| 2703 | if (folio_test_anon(folio))
|
| 2704 | entry = make_readable_exclusive_migration_entry(swp_offset(entry));
|
| 2705 | else
|
| 2706 | entry = make_readable_migration_entry(swp_offset(entry));
|
| 2707 | newpmd = softleaf_to_pmd(entry);
|
| 2708 | if (pmd_swp_soft_dirty(*pmd))
|
| 2709 | newpmd = pmd_swp_mksoft_dirty(newpmd);
|
| 2710 | } else if (softleaf_is_device_private_write(entry)) {
|
| 2711 | entry = make_readable_device_private_entry(swp_offset(entry));
|
| 2712 | newpmd = softleaf_to_pmd(entry);
|
| 2713 | if (pmd_swp_uffd(*pmd))
|
| 2714 | newpmd = pmd_swp_mkuffd(newpmd);
|
| 2715 | } else {
|
| 2716 | newpmd = *pmd;
|
| 2717 | }
|
| 2718 |
|
| 2719 | if (uffd_prot)
|
| 2720 | newpmd = pmd_swp_mkuffd(newpmd);
|
| 2721 | else if (uffd_prot_resolve)
|
| 2722 | newpmd = pmd_swp_clear_uffd(newpmd);
|
| 2723 | if (!pmd_same(*pmd, newpmd))
|
| 2724 | set_pmd_at(mm, addr, pmd, newpmd);
|
| 2725 | }
|
| 2726 |
|
| 2727 | |
| 2728 | * Returns
|
| 2729 | * - 0 if PMD could not be locked
|
| 2730 | * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary
|
| 2731 | * or if prot_numa but THP migration is not supported
|
| 2732 | * - HPAGE_PMD_NR if protections changed and TLB flush necessary
|
| 2733 |
|
| 2734 | int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
| 2735 | pmd_t *pmd, unsigned long addr, pgprot_t newprot,
|
| 2736 | unsigned long cp_flags)
|
| 2737 | {
|
| 2738 | struct mm_struct *mm = vma->vm_mm;
|
| 2739 | spinlock_t *ptl;
|
| 2740 | pmd_t oldpmd, entry;
|
| 2741 | bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
|
| 2742 | bool uffd_prot = cp_flags & (MM_CP_UFFD_WP | MM_CP_UFFD_RWP);
|
| 2743 | bool uffd_prot_resolve = cp_flags &
|
| 2744 | (MM_CP_UFFD_WP_RESOLVE | MM_CP_UFFD_RWP_RESOLVE);
|
| 2745 | int ret = 1;
|
| 2746 |
|
| 2747 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
|
| 2748 |
|
| 2749 | if (prot_numa && !thp_migration_supported())
|
| 2750 | return 1;
|
| 2751 |
|
| 2752 | ptl = __pmd_trans_huge_lock(pmd, vma);
|
| 2753 | if (!ptl)
|
| 2754 | return 0;
|
| 2755 |
|
| 2756 | if (thp_migration_supported() && pmd_is_valid_softleaf(*pmd)) {
|
| 2757 | change_non_present_huge_pmd(mm, addr, pmd, uffd_prot,
|
| 2758 | uffd_prot_resolve);
|
| 2759 | goto unlock;
|
| 2760 | }
|
| 2761 |
|
| 2762 |
|
| 2763 | if (prot_numa && pmd_protnone(*pmd))
|
| 2764 | goto unlock;
|
| 2765 | if ((cp_flags & MM_CP_UFFD_RWP) && pmd_protnone(*pmd) && pmd_uffd(*pmd))
|
| 2766 | goto unlock;
|
| 2767 |
|
| 2768 | if (prot_numa) {
|
| 2769 |
|
| 2770 | |
| 2771 | * Avoid trapping faults against the zero page. The read-only
|
| 2772 | * data is likely to be read-cached on the local CPU and
|
| 2773 | * local/remote hits to the zero page are not interesting.
|
| 2774 |
|
| 2775 | if (is_huge_zero_pmd(*pmd))
|
| 2776 | goto unlock;
|
| 2777 |
|
| 2778 | if (!folio_can_map_prot_numa(pmd_folio(*pmd), vma,
|
| 2779 | vma_is_single_threaded_private(vma)))
|
| 2780 | goto unlock;
|
| 2781 | }
|
| 2782 | |
| 2783 | * In case prot_numa, we are under mmap_read_lock(mm). It's critical
|
| 2784 | * to not clear pmd intermittently to avoid race with MADV_DONTNEED
|
| 2785 | * which is also under mmap_read_lock(mm):
|
| 2786 | *
|
| 2787 | * CPU0: CPU1:
|
| 2788 | * change_huge_pmd(prot_numa=1)
|
| 2789 | * pmdp_huge_get_and_clear_notify()
|
| 2790 | * madvise_dontneed()
|
| 2791 | * zap_pmd_range()
|
| 2792 | * pmd_trans_huge(*pmd) == 0 (without ptl)
|
| 2793 | * // skip the pmd
|
| 2794 | * set_pmd_at();
|
| 2795 | * // pmd is re-established
|
| 2796 | *
|
| 2797 | * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
|
| 2798 | * which may break userspace.
|
| 2799 | *
|
| 2800 | * pmdp_invalidate_ad() is required to make sure we don't miss
|
| 2801 | * dirty/young flags set by hardware.
|
| 2802 |
|
| 2803 | oldpmd = pmdp_invalidate_ad(vma, addr, pmd);
|
| 2804 |
|
| 2805 | entry = pmd_modify(oldpmd, newprot);
|
| 2806 | if (uffd_prot)
|
| 2807 | entry = pmd_mkuffd(entry);
|
| 2808 | else if (uffd_prot_resolve)
|
| 2809 | |
| 2810 | * Leave the write bit to be handled by PF interrupt
|
| 2811 | * handler, then things like COW could be properly
|
| 2812 | * handled.
|
| 2813 |
|
| 2814 | entry = pmd_clear_uffd(entry);
|
| 2815 |
|
| 2816 |
|
| 2817 | if (userfaultfd_rwp(vma) && pmd_uffd(entry))
|
| 2818 | entry = pmd_modify(entry, PAGE_NONE);
|
| 2819 |
|
| 2820 |
|
| 2821 | if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) &&
|
| 2822 | can_change_pmd_writable(vma, addr, entry))
|
| 2823 | entry = pmd_mkwrite(entry, vma);
|
| 2824 |
|
| 2825 | ret = HPAGE_PMD_NR;
|
| 2826 | set_pmd_at(mm, addr, pmd, entry);
|
| 2827 |
|
| 2828 | if (huge_pmd_needs_flush(oldpmd, entry))
|
| 2829 | tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE);
|
| 2830 | unlock:
|
| 2831 | spin_unlock(ptl);
|
| 2832 | return ret;
|
| 2833 | }
|
| 2834 |
|
| 2835 | |
| 2836 | * Returns:
|
| 2837 | *
|
| 2838 | * - 0: if pud leaf changed from under us
|
| 2839 | * - 1: if pud can be skipped
|
| 2840 | * - HPAGE_PUD_NR: if pud was successfully processed
|
| 2841 |
|
| 2842 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
|
| 2843 | int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
| 2844 | pud_t *pudp, unsigned long addr, pgprot_t newprot,
|
| 2845 | unsigned long cp_flags)
|
| 2846 | {
|
| 2847 | struct mm_struct *mm = vma->vm_mm;
|
| 2848 | pud_t oldpud, entry;
|
| 2849 | spinlock_t *ptl;
|
| 2850 |
|
| 2851 | tlb_change_page_size(tlb, HPAGE_PUD_SIZE);
|
| 2852 |
|
| 2853 |
|
| 2854 | if (cp_flags & MM_CP_PROT_NUMA)
|
| 2855 | return 1;
|
| 2856 |
|
| 2857 | |
| 2858 | * Huge entries on userfault-wp or userfault-rwp only work with
|
| 2859 | * anonymous, while we don't have anonymous PUDs yet.
|
| 2860 |
|
| 2861 | if (WARN_ON_ONCE(cp_flags & (MM_CP_UFFD_WP_ALL | MM_CP_UFFD_RWP_ALL)))
|
| 2862 | return 1;
|
| 2863 |
|
| 2864 | ptl = __pud_trans_huge_lock(pudp, vma);
|
| 2865 | if (!ptl)
|
| 2866 | return 0;
|
| 2867 |
|
| 2868 | |
| 2869 | * Can't clear PUD or it can race with concurrent zapping. See
|
| 2870 | * change_huge_pmd().
|
| 2871 |
|
| 2872 | oldpud = pudp_invalidate(vma, addr, pudp);
|
| 2873 | entry = pud_modify(oldpud, newprot);
|
| 2874 | set_pud_at(mm, addr, pudp, entry);
|
| 2875 | tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE);
|
| 2876 |
|
| 2877 | spin_unlock(ptl);
|
| 2878 | return HPAGE_PUD_NR;
|
| 2879 | }
|
| 2880 | #endif
|
| 2881 |
|
| 2882 | #ifdef CONFIG_USERFAULTFD
|
| 2883 | |
| 2884 | * The PT lock for src_pmd and dst_vma/src_vma (for reading) are locked by
|
| 2885 | * the caller, but it must return after releasing the page_table_lock.
|
| 2886 | * Just move the page from src_pmd to dst_pmd if possible.
|
| 2887 | * Return zero if succeeded in moving the page, -EAGAIN if it needs to be
|
| 2888 | * repeated by the caller, or other errors in case of failure.
|
| 2889 |
|
| 2890 | int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,
|
| 2891 | struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
|
| 2892 | unsigned long dst_addr, unsigned long src_addr)
|
| 2893 | {
|
| 2894 | pmd_t _dst_pmd, src_pmdval;
|
| 2895 | struct page *src_page;
|
| 2896 | struct folio *src_folio;
|
| 2897 | spinlock_t *src_ptl, *dst_ptl;
|
| 2898 | pgtable_t src_pgtable;
|
| 2899 | struct mmu_notifier_range range;
|
| 2900 | int err = 0;
|
| 2901 |
|
| 2902 | src_pmdval = *src_pmd;
|
| 2903 | src_ptl = pmd_lockptr(mm, src_pmd);
|
| 2904 |
|
| 2905 | lockdep_assert_held(src_ptl);
|
| 2906 | vma_assert_locked(src_vma);
|
| 2907 | vma_assert_locked(dst_vma);
|
| 2908 |
|
| 2909 |
|
| 2910 | if (WARN_ON_ONCE(!pmd_none(dst_pmdval)) || WARN_ON_ONCE(src_addr & ~HPAGE_PMD_MASK) ||
|
| 2911 | WARN_ON_ONCE(dst_addr & ~HPAGE_PMD_MASK)) {
|
| 2912 | spin_unlock(src_ptl);
|
| 2913 | return -EINVAL;
|
| 2914 | }
|
| 2915 |
|
| 2916 | if (!pmd_trans_huge(src_pmdval)) {
|
| 2917 | spin_unlock(src_ptl);
|
| 2918 | if (pmd_is_migration_entry(src_pmdval)) {
|
| 2919 | pmd_migration_entry_wait(mm, src_pmd);
|
| 2920 | return -EAGAIN;
|
| 2921 | }
|
| 2922 | return -ENOENT;
|
| 2923 | }
|
| 2924 |
|
| 2925 | src_page = pmd_page(src_pmdval);
|
| 2926 |
|
| 2927 | if (!is_huge_zero_pmd(src_pmdval)) {
|
| 2928 | if (unlikely(!PageAnonExclusive(src_page))) {
|
| 2929 | spin_unlock(src_ptl);
|
| 2930 | return -EBUSY;
|
| 2931 | }
|
| 2932 |
|
| 2933 | src_folio = page_folio(src_page);
|
| 2934 | folio_get(src_folio);
|
| 2935 | } else
|
| 2936 | src_folio = NULL;
|
| 2937 |
|
| 2938 | spin_unlock(src_ptl);
|
| 2939 |
|
| 2940 | flush_cache_range(src_vma, src_addr, src_addr + HPAGE_PMD_SIZE);
|
| 2941 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, src_addr,
|
| 2942 | src_addr + HPAGE_PMD_SIZE);
|
| 2943 | mmu_notifier_invalidate_range_start(&range);
|
| 2944 |
|
| 2945 | if (src_folio)
|
| 2946 | folio_lock(src_folio);
|
| 2947 |
|
| 2948 | dst_ptl = pmd_lockptr(mm, dst_pmd);
|
| 2949 | double_pt_lock(src_ptl, dst_ptl);
|
| 2950 | if (unlikely(!pmd_same(*src_pmd, src_pmdval) ||
|
| 2951 | !pmd_same(*dst_pmd, dst_pmdval))) {
|
| 2952 | err = -EAGAIN;
|
| 2953 | goto unlock_ptls;
|
| 2954 | }
|
| 2955 | if (src_folio) {
|
| 2956 | if (folio_maybe_dma_pinned(src_folio) ||
|
| 2957 | !PageAnonExclusive(&src_folio->page)) {
|
| 2958 | err = -EBUSY;
|
| 2959 | goto unlock_ptls;
|
| 2960 | }
|
| 2961 |
|
| 2962 | if (WARN_ON_ONCE(!folio_test_head(src_folio)) ||
|
| 2963 | WARN_ON_ONCE(!folio_test_anon(src_folio))) {
|
| 2964 | err = -EBUSY;
|
| 2965 | goto unlock_ptls;
|
| 2966 | }
|
| 2967 |
|
| 2968 | src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
|
| 2969 |
|
| 2970 | if (folio_maybe_dma_pinned(src_folio)) {
|
| 2971 | set_pmd_at(mm, src_addr, src_pmd, src_pmdval);
|
| 2972 | err = -EBUSY;
|
| 2973 | goto unlock_ptls;
|
| 2974 | }
|
| 2975 |
|
| 2976 | folio_move_anon_rmap(src_folio, dst_vma);
|
| 2977 | src_folio->index = linear_anon_page_index(dst_vma, dst_addr);
|
| 2978 |
|
| 2979 | _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot);
|
| 2980 |
|
| 2981 | _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
|
| 2982 | } else {
|
| 2983 | src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
|
| 2984 | _dst_pmd = move_soft_dirty_pmd(src_pmdval);
|
| 2985 | _dst_pmd = clear_uffd_wp_pmd(_dst_pmd);
|
| 2986 | }
|
| 2987 |
|
| 2988 |
|
| 2989 | if (userfaultfd_rwp(dst_vma)) {
|
| 2990 | _dst_pmd = pmd_modify(_dst_pmd, PAGE_NONE);
|
| 2991 | _dst_pmd = pmd_mkuffd(_dst_pmd);
|
| 2992 | }
|
| 2993 |
|
| 2994 | set_pmd_at(mm, dst_addr, dst_pmd, _dst_pmd);
|
| 2995 |
|
| 2996 | src_pgtable = pgtable_trans_huge_withdraw(mm, src_pmd);
|
| 2997 | pgtable_trans_huge_deposit(mm, dst_pmd, src_pgtable);
|
| 2998 | unlock_ptls:
|
| 2999 | double_pt_unlock(src_ptl, dst_ptl);
|
| 3000 |
|
| 3001 | if (src_folio)
|
| 3002 | folio_unlock(src_folio);
|
| 3003 | mmu_notifier_invalidate_range_end(&range);
|
| 3004 | if (src_folio)
|
| 3005 | folio_put(src_folio);
|
| 3006 | return err;
|
| 3007 | }
|
| 3008 | #endif
|
| 3009 |
|
| 3010 | |
| 3011 | * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
|
| 3012 | *
|
| 3013 | * Note that if it returns page table lock pointer, this routine returns without
|
| 3014 | * unlocking page table lock. So callers must unlock it.
|
| 3015 |
|
| 3016 | spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
|
| 3017 | {
|
| 3018 | spinlock_t *ptl;
|
| 3019 |
|
| 3020 | ptl = pmd_lock(vma->vm_mm, pmd);
|
| 3021 | if (likely(pmd_is_huge(*pmd)))
|
| 3022 | return ptl;
|
| 3023 | spin_unlock(ptl);
|
| 3024 | return NULL;
|
| 3025 | }
|
| 3026 |
|
| 3027 | |
| 3028 | * Returns page table lock pointer if a given pud maps a thp, NULL otherwise.
|
| 3029 | *
|
| 3030 | * Note that if it returns page table lock pointer, this routine returns without
|
| 3031 | * unlocking page table lock. So callers must unlock it.
|
| 3032 |
|
| 3033 | spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
|
| 3034 | {
|
| 3035 | spinlock_t *ptl;
|
| 3036 |
|
| 3037 | ptl = pud_lock(vma->vm_mm, pud);
|
| 3038 | if (likely(pud_trans_huge(*pud)))
|
| 3039 | return ptl;
|
| 3040 | spin_unlock(ptl);
|
| 3041 | return NULL;
|
| 3042 | }
|
| 3043 |
|
| 3044 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
|
| 3045 | int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
|
| 3046 | pud_t *pud, unsigned long addr)
|
| 3047 | {
|
| 3048 | spinlock_t *ptl;
|
| 3049 | pud_t orig_pud;
|
| 3050 |
|
| 3051 | ptl = __pud_trans_huge_lock(pud, vma);
|
| 3052 | if (!ptl)
|
| 3053 | return 0;
|
| 3054 |
|
| 3055 | orig_pud = pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm);
|
| 3056 | arch_check_zapped_pud(vma, orig_pud);
|
| 3057 | tlb_remove_pud_tlb_entry(tlb, pud, addr);
|
| 3058 | if (vma_is_special_huge(vma)) {
|
| 3059 | spin_unlock(ptl);
|
| 3060 |
|
| 3061 | } else {
|
| 3062 | struct page *page = NULL;
|
| 3063 | struct folio *folio;
|
| 3064 |
|
| 3065 |
|
| 3066 | VM_WARN_ON_ONCE(vma_is_anonymous(vma) ||
|
| 3067 | !pud_present(orig_pud));
|
| 3068 |
|
| 3069 | page = pud_page(orig_pud);
|
| 3070 | folio = page_folio(page);
|
| 3071 | folio_remove_rmap_pud(folio, page, vma);
|
| 3072 | add_mm_counter(tlb->mm, mm_counter_file(folio), -HPAGE_PUD_NR);
|
| 3073 |
|
| 3074 | spin_unlock(ptl);
|
| 3075 | tlb_remove_page_size(tlb, page, HPAGE_PUD_SIZE);
|
| 3076 | }
|
| 3077 | return 1;
|
| 3078 | }
|
| 3079 |
|
| 3080 | static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
|
| 3081 | unsigned long haddr)
|
| 3082 | {
|
| 3083 | struct folio *folio;
|
| 3084 | struct page *page;
|
| 3085 | pud_t old_pud;
|
| 3086 |
|
| 3087 | VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
|
| 3088 | VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
|
| 3089 | VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
|
| 3090 | VM_BUG_ON(!pud_trans_huge(*pud));
|
| 3091 |
|
| 3092 | count_vm_event(THP_SPLIT_PUD);
|
| 3093 |
|
| 3094 | old_pud = pudp_huge_clear_flush(vma, haddr, pud);
|
| 3095 |
|
| 3096 | if (!vma_is_dax(vma))
|
| 3097 | return;
|
| 3098 |
|
| 3099 | page = pud_page(old_pud);
|
| 3100 | folio = page_folio(page);
|
| 3101 |
|
| 3102 | if (!folio_test_dirty(folio) && pud_dirty(old_pud))
|
| 3103 | folio_mark_dirty(folio);
|
| 3104 | if (!folio_test_referenced(folio) && pud_young(old_pud))
|
| 3105 | folio_set_referenced(folio);
|
| 3106 | folio_remove_rmap_pud(folio, page, vma);
|
| 3107 | add_mm_counter(vma->vm_mm, mm_counter_file(folio),
|
| 3108 | -HPAGE_PUD_NR);
|
| 3109 | folio_put(folio);
|
| 3110 | }
|
| 3111 |
|
| 3112 | void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
|
| 3113 | unsigned long address)
|
| 3114 | {
|
| 3115 | spinlock_t *ptl;
|
| 3116 | struct mmu_notifier_range range;
|
| 3117 |
|
| 3118 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
|
| 3119 | address & HPAGE_PUD_MASK,
|
| 3120 | (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
|
| 3121 | mmu_notifier_invalidate_range_start(&range);
|
| 3122 | ptl = pud_lock(vma->vm_mm, pud);
|
| 3123 | if (unlikely(!pud_trans_huge(*pud)))
|
| 3124 | goto out;
|
| 3125 | __split_huge_pud_locked(vma, pud, range.start);
|
| 3126 |
|
| 3127 | out:
|
| 3128 | spin_unlock(ptl);
|
| 3129 | mmu_notifier_invalidate_range_end(&range);
|
| 3130 | }
|
| 3131 | #else
|
| 3132 | void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
|
| 3133 | unsigned long address)
|
| 3134 | {
|
| 3135 | }
|
| 3136 | #endif
|
| 3137 |
|
| 3138 | static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
|
| 3139 | unsigned long haddr, pmd_t *pmd)
|
| 3140 | {
|
| 3141 | struct mm_struct *mm = vma->vm_mm;
|
| 3142 | pgtable_t pgtable;
|
| 3143 | pmd_t _pmd, old_pmd;
|
| 3144 | unsigned long addr;
|
| 3145 | pte_t *pte;
|
| 3146 | int i;
|
| 3147 |
|
| 3148 | |
| 3149 | * Leave pmd empty until pte is filled note that it is fine to delay
|
| 3150 | * notification until mmu_notifier_invalidate_range_end() as we are
|
| 3151 | * replacing a zero pmd write protected page with a zero pte write
|
| 3152 | * protected page.
|
| 3153 | *
|
| 3154 | * See Documentation/mm/mmu_notifier.rst
|
| 3155 |
|
| 3156 | old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd);
|
| 3157 |
|
| 3158 | pgtable = pgtable_trans_huge_withdraw(mm, pmd);
|
| 3159 | pmd_populate(mm, &_pmd, pgtable);
|
| 3160 |
|
| 3161 | pte = pte_offset_map(&_pmd, haddr);
|
| 3162 | VM_BUG_ON(!pte);
|
| 3163 | for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
|
| 3164 | pte_t entry;
|
| 3165 |
|
| 3166 | entry = pfn_pte(zero_pfn(addr), vma->vm_page_prot);
|
| 3167 | entry = pte_mkspecial(entry);
|
| 3168 | if (pmd_uffd(old_pmd))
|
| 3169 | entry = pte_mkuffd(entry);
|
| 3170 |
|
| 3171 |
|
| 3172 | if (userfaultfd_rwp(vma) && pmd_uffd(old_pmd))
|
| 3173 | entry = pte_modify(entry, PAGE_NONE);
|
| 3174 |
|
| 3175 | VM_BUG_ON(!pte_none(ptep_get(pte)));
|
| 3176 | set_pte_at(mm, addr, pte, entry);
|
| 3177 | pte++;
|
| 3178 | }
|
| 3179 | pte_unmap(pte - 1);
|
| 3180 | smp_wmb();
|
| 3181 | pmd_populate(mm, pmd, pgtable);
|
| 3182 | }
|
| 3183 |
|
| 3184 | static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
|
| 3185 | unsigned long haddr, bool freeze)
|
| 3186 | {
|
| 3187 | struct mm_struct *mm = vma->vm_mm;
|
| 3188 | struct folio *folio;
|
| 3189 | struct page *page;
|
| 3190 | pgtable_t pgtable;
|
| 3191 | pmd_t old_pmd, _pmd;
|
| 3192 | bool soft_dirty, uffd_wp = false, young = false, write = false;
|
| 3193 | bool anon_exclusive = false, dirty = false;
|
| 3194 | unsigned long addr;
|
| 3195 | pte_t *pte;
|
| 3196 | int i;
|
| 3197 |
|
| 3198 | VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
|
| 3199 | VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
|
| 3200 | VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
|
| 3201 |
|
| 3202 | VM_WARN_ON_ONCE(!pmd_is_valid_softleaf(*pmd) && !pmd_trans_huge(*pmd));
|
| 3203 |
|
| 3204 | count_vm_event(THP_SPLIT_PMD);
|
| 3205 |
|
| 3206 | if (!vma_is_anonymous(vma)) {
|
| 3207 | old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd);
|
| 3208 | |
| 3209 | * We are going to unmap this huge page. So
|
| 3210 | * just go ahead and zap it
|
| 3211 |
|
| 3212 | if (arch_needs_pgtable_deposit())
|
| 3213 | zap_deposited_table(mm, pmd);
|
| 3214 | if (vma_is_special_huge(vma))
|
| 3215 | return;
|
| 3216 | if (unlikely(pmd_is_migration_entry(old_pmd))) {
|
| 3217 | const softleaf_t old_entry = softleaf_from_pmd(old_pmd);
|
| 3218 |
|
| 3219 | folio = softleaf_to_folio(old_entry);
|
| 3220 | } else if (is_huge_zero_pmd(old_pmd)) {
|
| 3221 | return;
|
| 3222 | } else {
|
| 3223 | page = pmd_page(old_pmd);
|
| 3224 | folio = page_folio(page);
|
| 3225 | if (!folio_test_dirty(folio) && pmd_dirty(old_pmd))
|
| 3226 | folio_mark_dirty(folio);
|
| 3227 | if (!folio_test_referenced(folio) && pmd_young(old_pmd))
|
| 3228 | folio_set_referenced(folio);
|
| 3229 | folio_remove_rmap_pmd(folio, page, vma);
|
| 3230 | add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
|
| 3231 | folio_put(folio);
|
| 3232 | return;
|
| 3233 | }
|
| 3234 | add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
|
| 3235 | return;
|
| 3236 | }
|
| 3237 |
|
| 3238 | if (is_huge_zero_pmd(*pmd)) {
|
| 3239 | |
| 3240 | * FIXME: Do we want to invalidate secondary mmu by calling
|
| 3241 | * mmu_notifier_arch_invalidate_secondary_tlbs() see comments below
|
| 3242 | * inside __split_huge_pmd() ?
|
| 3243 | *
|
| 3244 | * We are going from a zero huge page write protected to zero
|
| 3245 | * small page also write protected so it does not seems useful
|
| 3246 | * to invalidate secondary mmu at this time.
|
| 3247 |
|
| 3248 | return __split_huge_zero_page_pmd(vma, haddr, pmd);
|
| 3249 | }
|
| 3250 |
|
| 3251 | if (pmd_is_migration_entry(*pmd)) {
|
| 3252 | softleaf_t entry;
|
| 3253 |
|
| 3254 | old_pmd = *pmd;
|
| 3255 | entry = softleaf_from_pmd(old_pmd);
|
| 3256 | page = softleaf_to_page(entry);
|
| 3257 | folio = page_folio(page);
|
| 3258 |
|
| 3259 | soft_dirty = pmd_swp_soft_dirty(old_pmd);
|
| 3260 | uffd_wp = pmd_swp_uffd(old_pmd);
|
| 3261 |
|
| 3262 | write = softleaf_is_migration_write(entry);
|
| 3263 | if (PageAnon(page))
|
| 3264 | anon_exclusive = softleaf_is_migration_read_exclusive(entry);
|
| 3265 | young = softleaf_is_migration_young(entry);
|
| 3266 | dirty = softleaf_is_migration_dirty(entry);
|
| 3267 | } else if (pmd_is_device_private_entry(*pmd)) {
|
| 3268 | softleaf_t entry;
|
| 3269 |
|
| 3270 | old_pmd = *pmd;
|
| 3271 | entry = softleaf_from_pmd(old_pmd);
|
| 3272 | page = softleaf_to_page(entry);
|
| 3273 | folio = page_folio(page);
|
| 3274 |
|
| 3275 | soft_dirty = pmd_swp_soft_dirty(old_pmd);
|
| 3276 | uffd_wp = pmd_swp_uffd(old_pmd);
|
| 3277 |
|
| 3278 | write = softleaf_is_device_private_write(entry);
|
| 3279 | anon_exclusive = PageAnonExclusive(page);
|
| 3280 |
|
| 3281 | |
| 3282 | * Device private THP should be treated the same as regular
|
| 3283 | * folios w.r.t anon exclusive handling. See the comments for
|
| 3284 | * folio handling and anon_exclusive below.
|
| 3285 |
|
| 3286 | if (freeze && anon_exclusive &&
|
| 3287 | folio_try_share_anon_rmap_pmd(folio, page))
|
| 3288 | freeze = false;
|
| 3289 | if (!freeze) {
|
| 3290 | rmap_t rmap_flags = RMAP_NONE;
|
| 3291 |
|
| 3292 | folio_ref_add(folio, HPAGE_PMD_NR - 1);
|
| 3293 | if (anon_exclusive)
|
| 3294 | rmap_flags |= RMAP_EXCLUSIVE;
|
| 3295 |
|
| 3296 | folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR,
|
| 3297 | vma, haddr, rmap_flags);
|
| 3298 | }
|
| 3299 | } else {
|
| 3300 | |
| 3301 | * Up to this point the pmd is present and huge and userland has
|
| 3302 | * the whole access to the hugepage during the split (which
|
| 3303 | * happens in place). If we overwrite the pmd with the not-huge
|
| 3304 | * version pointing to the pte here (which of course we could if
|
| 3305 | * all CPUs were bug free), userland could trigger a small page
|
| 3306 | * size TLB miss on the small sized TLB while the hugepage TLB
|
| 3307 | * entry is still established in the huge TLB. Some CPU doesn't
|
| 3308 | * like that. See
|
| 3309 | * http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
|
| 3310 | * 383 on page 105. Intel should be safe but is also warns that
|
| 3311 | * it's only safe if the permission and cache attributes of the
|
| 3312 | * two entries loaded in the two TLB is identical (which should
|
| 3313 | * be the case here). But it is generally safer to never allow
|
| 3314 | * small and huge TLB entries for the same virtual address to be
|
| 3315 | * loaded simultaneously. So instead of doing "pmd_populate();
|
| 3316 | * flush_pmd_tlb_range();" we first mark the current pmd
|
| 3317 | * notpresent (atomically because here the pmd_trans_huge must
|
| 3318 | * remain set at all times on the pmd until the split is
|
| 3319 | * complete for this pmd), then we flush the SMP TLB and finally
|
| 3320 | * we write the non-huge version of the pmd entry with
|
| 3321 | * pmd_populate.
|
| 3322 |
|
| 3323 | old_pmd = pmdp_invalidate(vma, haddr, pmd);
|
| 3324 | page = pmd_page(old_pmd);
|
| 3325 | folio = page_folio(page);
|
| 3326 | if (pmd_dirty(old_pmd)) {
|
| 3327 | dirty = true;
|
| 3328 | folio_set_dirty(folio);
|
| 3329 | }
|
| 3330 | write = pmd_write(old_pmd);
|
| 3331 | young = pmd_young(old_pmd);
|
| 3332 | soft_dirty = pmd_soft_dirty(old_pmd);
|
| 3333 | uffd_wp = pmd_uffd(old_pmd);
|
| 3334 |
|
| 3335 | VM_WARN_ON_FOLIO(!folio_ref_count(folio), folio);
|
| 3336 | VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
|
| 3337 |
|
| 3338 | |
| 3339 | * Without "freeze", we'll simply split the PMD, propagating the
|
| 3340 | * PageAnonExclusive() flag for each PTE by setting it for
|
| 3341 | * each subpage -- no need to (temporarily) clear.
|
| 3342 | *
|
| 3343 | * With "freeze" we want to replace mapped pages by
|
| 3344 | * migration entries right away. This is only possible if we
|
| 3345 | * managed to clear PageAnonExclusive() -- see
|
| 3346 | * set_pmd_migration_entry().
|
| 3347 | *
|
| 3348 | * In case we cannot clear PageAnonExclusive(), split the PMD
|
| 3349 | * only and let try_to_migrate_one() fail later.
|
| 3350 | *
|
| 3351 | * See folio_try_share_anon_rmap_pmd(): invalidate PMD first.
|
| 3352 |
|
| 3353 | anon_exclusive = PageAnonExclusive(page);
|
| 3354 | if (freeze && anon_exclusive &&
|
| 3355 | folio_try_share_anon_rmap_pmd(folio, page))
|
| 3356 | freeze = false;
|
| 3357 | if (!freeze) {
|
| 3358 | rmap_t rmap_flags = RMAP_NONE;
|
| 3359 |
|
| 3360 | folio_ref_add(folio, HPAGE_PMD_NR - 1);
|
| 3361 | if (anon_exclusive)
|
| 3362 | rmap_flags |= RMAP_EXCLUSIVE;
|
| 3363 | folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR,
|
| 3364 | vma, haddr, rmap_flags);
|
| 3365 | }
|
| 3366 | }
|
| 3367 |
|
| 3368 | |
| 3369 | * Withdraw the table only after we mark the pmd entry invalid.
|
| 3370 | * This's critical for some architectures (Power).
|
| 3371 |
|
| 3372 | pgtable = pgtable_trans_huge_withdraw(mm, pmd);
|
| 3373 | pmd_populate(mm, &_pmd, pgtable);
|
| 3374 |
|
| 3375 | pte = pte_offset_map(&_pmd, haddr);
|
| 3376 | VM_BUG_ON(!pte);
|
| 3377 |
|
| 3378 | |
| 3379 | * Note that NUMA hinting access restrictions are not transferred to
|
| 3380 | * avoid any possibility of altering permissions across VMAs.
|
| 3381 |
|
| 3382 | if (freeze || pmd_is_migration_entry(old_pmd)) {
|
| 3383 | pte_t entry;
|
| 3384 | swp_entry_t swp_entry;
|
| 3385 |
|
| 3386 | for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
|
| 3387 | if (write)
|
| 3388 | swp_entry = make_writable_migration_entry(
|
| 3389 | page_to_pfn(page + i));
|
| 3390 | else if (anon_exclusive)
|
| 3391 | swp_entry = make_readable_exclusive_migration_entry(
|
| 3392 | page_to_pfn(page + i));
|
| 3393 | else
|
| 3394 | swp_entry = make_readable_migration_entry(
|
| 3395 | page_to_pfn(page + i));
|
| 3396 | if (young)
|
| 3397 | swp_entry = make_migration_entry_young(swp_entry);
|
| 3398 | if (dirty)
|
| 3399 | swp_entry = make_migration_entry_dirty(swp_entry);
|
| 3400 | entry = swp_entry_to_pte(swp_entry);
|
| 3401 | if (soft_dirty)
|
| 3402 | entry = pte_swp_mksoft_dirty(entry);
|
| 3403 | if (uffd_wp)
|
| 3404 | entry = pte_swp_mkuffd(entry);
|
| 3405 | VM_WARN_ON(!pte_none(ptep_get(pte + i)));
|
| 3406 | set_pte_at(mm, addr, pte + i, entry);
|
| 3407 | }
|
| 3408 | } else if (pmd_is_device_private_entry(old_pmd)) {
|
| 3409 | pte_t entry;
|
| 3410 | swp_entry_t swp_entry;
|
| 3411 |
|
| 3412 | for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
|
| 3413 | |
| 3414 | * anon_exclusive was already propagated to the relevant
|
| 3415 | * pages corresponding to the pte entries when freeze
|
| 3416 | * is false.
|
| 3417 |
|
| 3418 | if (write)
|
| 3419 | swp_entry = make_writable_device_private_entry(
|
| 3420 | page_to_pfn(page + i));
|
| 3421 | else
|
| 3422 | swp_entry = make_readable_device_private_entry(
|
| 3423 | page_to_pfn(page + i));
|
| 3424 | |
| 3425 | * Young and dirty bits are not progated via swp_entry
|
| 3426 |
|
| 3427 | entry = swp_entry_to_pte(swp_entry);
|
| 3428 | if (soft_dirty)
|
| 3429 | entry = pte_swp_mksoft_dirty(entry);
|
| 3430 | if (uffd_wp)
|
| 3431 | entry = pte_swp_mkuffd(entry);
|
| 3432 | VM_WARN_ON(!pte_none(ptep_get(pte + i)));
|
| 3433 | set_pte_at(mm, addr, pte + i, entry);
|
| 3434 | }
|
| 3435 | } else {
|
| 3436 | pte_t entry;
|
| 3437 |
|
| 3438 | entry = mk_pte(page, READ_ONCE(vma->vm_page_prot));
|
| 3439 | if (write)
|
| 3440 | entry = pte_mkwrite(entry, vma);
|
| 3441 | if (!young)
|
| 3442 | entry = pte_mkold(entry);
|
| 3443 |
|
| 3444 | if (dirty)
|
| 3445 | entry = pte_mkdirty(entry);
|
| 3446 | if (soft_dirty)
|
| 3447 | entry = pte_mksoft_dirty(entry);
|
| 3448 | if (uffd_wp)
|
| 3449 | entry = pte_mkuffd(entry);
|
| 3450 |
|
| 3451 |
|
| 3452 | if (userfaultfd_rwp(vma) && uffd_wp)
|
| 3453 | entry = pte_modify(entry, PAGE_NONE);
|
| 3454 |
|
| 3455 | for (i = 0; i < HPAGE_PMD_NR; i++)
|
| 3456 | VM_WARN_ON(!pte_none(ptep_get(pte + i)));
|
| 3457 |
|
| 3458 | set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR);
|
| 3459 | }
|
| 3460 | pte_unmap(pte);
|
| 3461 |
|
| 3462 | if (!pmd_is_migration_entry(*pmd))
|
| 3463 | folio_remove_rmap_pmd(folio, page, vma);
|
| 3464 | if (freeze)
|
| 3465 | put_page(page);
|
| 3466 |
|
| 3467 | smp_wmb();
|
| 3468 | pmd_populate(mm, pmd, pgtable);
|
| 3469 | }
|
| 3470 |
|
| 3471 | void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
|
| 3472 | pmd_t *pmd, bool freeze)
|
| 3473 | {
|
| 3474 | VM_WARN_ON_ONCE(!IS_ALIGNED(address, HPAGE_PMD_SIZE));
|
| 3475 | if (pmd_trans_huge(*pmd) || pmd_is_valid_softleaf(*pmd))
|
| 3476 | __split_huge_pmd_locked(vma, pmd, address, freeze);
|
| 3477 | }
|
| 3478 |
|
| 3479 | void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
|
| 3480 | unsigned long address, bool freeze)
|
| 3481 | {
|
| 3482 | spinlock_t *ptl;
|
| 3483 | struct mmu_notifier_range range;
|
| 3484 |
|
| 3485 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
|
| 3486 | address & HPAGE_PMD_MASK,
|
| 3487 | (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
|
| 3488 | mmu_notifier_invalidate_range_start(&range);
|
| 3489 | ptl = pmd_lock(vma->vm_mm, pmd);
|
| 3490 | split_huge_pmd_locked(vma, range.start, pmd, freeze);
|
| 3491 | spin_unlock(ptl);
|
| 3492 | mmu_notifier_invalidate_range_end(&range);
|
| 3493 | }
|
| 3494 |
|
| 3495 | void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
|
| 3496 | bool freeze)
|
| 3497 | {
|
| 3498 | pmd_t *pmd = mm_find_pmd(vma->vm_mm, address);
|
| 3499 |
|
| 3500 | if (!pmd)
|
| 3501 | return;
|
| 3502 |
|
| 3503 | __split_huge_pmd(vma, pmd, address, freeze);
|
| 3504 | }
|
| 3505 |
|
| 3506 | static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
|
| 3507 | {
|
| 3508 | |
| 3509 | * If the new address isn't hpage aligned and it could previously
|
| 3510 | * contain an hugepage: check if we need to split an huge pmd.
|
| 3511 |
|
| 3512 | if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
|
| 3513 | range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
|
| 3514 | ALIGN(address, HPAGE_PMD_SIZE)))
|
| 3515 | split_huge_pmd_address(vma, address, false);
|
| 3516 | }
|
| 3517 |
|
| 3518 | void vma_adjust_trans_huge(struct vm_area_struct *vma,
|
| 3519 | unsigned long start,
|
| 3520 | unsigned long end,
|
| 3521 | struct vm_area_struct *next)
|
| 3522 | {
|
| 3523 |
|
| 3524 | split_huge_pmd_if_needed(vma, start);
|
| 3525 |
|
| 3526 |
|
| 3527 | split_huge_pmd_if_needed(vma, end);
|
| 3528 |
|
| 3529 |
|
| 3530 | if (next)
|
| 3531 | split_huge_pmd_if_needed(next, end);
|
| 3532 | }
|
| 3533 |
|
| 3534 | static void unmap_folio(struct folio *folio)
|
| 3535 | {
|
| 3536 | enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SYNC |
|
| 3537 | TTU_BATCH_FLUSH;
|
| 3538 |
|
| 3539 | VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
|
| 3540 |
|
| 3541 | if (folio_test_pmd_mappable(folio))
|
| 3542 | ttu_flags |= TTU_SPLIT_HUGE_PMD;
|
| 3543 |
|
| 3544 | |
| 3545 | * Anon pages need migration entries to preserve them, but file
|
| 3546 | * pages can simply be left unmapped, then faulted back on demand.
|
| 3547 | * If that is ever changed (perhaps for mlock), update remap_page().
|
| 3548 |
|
| 3549 | if (folio_test_anon(folio))
|
| 3550 | try_to_migrate(folio, ttu_flags);
|
| 3551 | else
|
| 3552 | try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK);
|
| 3553 |
|
| 3554 | try_to_unmap_flush();
|
| 3555 | }
|
| 3556 |
|
| 3557 | static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
|
| 3558 | unsigned long addr, pmd_t *pmdp,
|
| 3559 | struct folio *folio)
|
| 3560 | {
|
| 3561 | struct mm_struct *mm = vma->vm_mm;
|
| 3562 | int ref_count, map_count;
|
| 3563 | pmd_t orig_pmd = *pmdp;
|
| 3564 |
|
| 3565 | if (pmd_dirty(orig_pmd))
|
| 3566 | folio_set_dirty(folio);
|
| 3567 | if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
|
| 3568 | folio_set_swapbacked(folio);
|
| 3569 | return false;
|
| 3570 | }
|
| 3571 |
|
| 3572 | orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
|
| 3573 |
|
| 3574 | |
| 3575 | * Syncing against concurrent GUP-fast:
|
| 3576 | * - clear PMD; barrier; read refcount
|
| 3577 | * - inc refcount; barrier; read PMD
|
| 3578 |
|
| 3579 | smp_mb();
|
| 3580 |
|
| 3581 | ref_count = folio_ref_count(folio);
|
| 3582 | map_count = folio_mapcount(folio);
|
| 3583 |
|
| 3584 | |
| 3585 | * Order reads for folio refcount and dirty flag
|
| 3586 | * (see comments in __remove_mapping()).
|
| 3587 |
|
| 3588 | smp_rmb();
|
| 3589 |
|
| 3590 | |
| 3591 | * If the folio or its PMD is redirtied at this point, or if there
|
| 3592 | * are unexpected references, we will give up to discard this folio
|
| 3593 | * and remap it.
|
| 3594 | *
|
| 3595 | * The only folio refs must be one from isolation plus the rmap(s).
|
| 3596 |
|
| 3597 | if (pmd_dirty(orig_pmd))
|
| 3598 | folio_set_dirty(folio);
|
| 3599 | if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
|
| 3600 | folio_set_swapbacked(folio);
|
| 3601 | set_pmd_at(mm, addr, pmdp, orig_pmd);
|
| 3602 | return false;
|
| 3603 | }
|
| 3604 |
|
| 3605 | if (ref_count != map_count + 1) {
|
| 3606 | set_pmd_at(mm, addr, pmdp, orig_pmd);
|
| 3607 | return false;
|
| 3608 | }
|
| 3609 |
|
| 3610 | folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
|
| 3611 | zap_deposited_table(mm, pmdp);
|
| 3612 | add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
|
| 3613 | if (vma->vm_flags & VM_LOCKED)
|
| 3614 | mlock_drain_local();
|
| 3615 | folio_put(folio);
|
| 3616 |
|
| 3617 | return true;
|
| 3618 | }
|
| 3619 |
|
| 3620 | bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
|
| 3621 | pmd_t *pmdp, struct folio *folio)
|
| 3622 | {
|
| 3623 | VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio);
|
| 3624 | VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
|
| 3625 | VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
|
| 3626 | VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio);
|
| 3627 | VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE));
|
| 3628 |
|
| 3629 | return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio);
|
| 3630 | }
|
| 3631 |
|
| 3632 | static void remap_page(struct folio *folio, unsigned long nr, int flags)
|
| 3633 | {
|
| 3634 | int i = 0;
|
| 3635 |
|
| 3636 |
|
| 3637 | if (!folio_test_anon(folio))
|
| 3638 | return;
|
| 3639 | for (;;) {
|
| 3640 | remove_migration_ptes(folio, folio, TTU_RMAP_LOCKED | flags);
|
| 3641 | i += folio_nr_pages(folio);
|
| 3642 | if (i >= nr)
|
| 3643 | break;
|
| 3644 | folio = folio_next(folio);
|
| 3645 | }
|
| 3646 | }
|
| 3647 |
|
| 3648 | static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,
|
| 3649 | struct lruvec *lruvec, struct list_head *list)
|
| 3650 | {
|
| 3651 | VM_BUG_ON_FOLIO(folio_test_lru(new_folio), folio);
|
| 3652 | lockdep_assert_held(&lruvec->lru_lock);
|
| 3653 |
|
| 3654 | if (folio_is_device_private(folio))
|
| 3655 | return;
|
| 3656 |
|
| 3657 | if (list) {
|
| 3658 |
|
| 3659 | VM_WARN_ON(folio_test_lru(folio));
|
| 3660 | folio_get(new_folio);
|
| 3661 | list_add_tail(&new_folio->lru, list);
|
| 3662 | } else {
|
| 3663 |
|
| 3664 | VM_WARN_ON(!folio_test_lru(folio));
|
| 3665 | if (folio_test_unevictable(folio))
|
| 3666 | new_folio->mlock_count = 0;
|
| 3667 | else
|
| 3668 | list_add_tail(&new_folio->lru, &folio->lru);
|
| 3669 | folio_set_lru(new_folio);
|
| 3670 | }
|
| 3671 | }
|
| 3672 |
|
| 3673 | static bool page_range_has_hwpoisoned(struct page *page, long nr_pages)
|
| 3674 | {
|
| 3675 | for (; nr_pages; page++, nr_pages--)
|
| 3676 | if (PageHWPoison(page))
|
| 3677 | return true;
|
| 3678 | return false;
|
| 3679 | }
|
| 3680 |
|
| 3681 | |
| 3682 | * It splits @folio into @new_order folios and copies the @folio metadata to
|
| 3683 | * all the resulting folios.
|
| 3684 |
|
| 3685 | static void __split_folio_to_order(struct folio *folio, int old_order,
|
| 3686 | int new_order)
|
| 3687 | {
|
| 3688 |
|
| 3689 | const bool handle_hwpoison = folio_test_has_hwpoisoned(folio) && new_order;
|
| 3690 | long new_nr_pages = 1 << new_order;
|
| 3691 | long nr_pages = 1 << old_order;
|
| 3692 | long i;
|
| 3693 |
|
| 3694 | folio_clear_has_hwpoisoned(folio);
|
| 3695 |
|
| 3696 |
|
| 3697 | if (handle_hwpoison &&
|
| 3698 | page_range_has_hwpoisoned(folio_page(folio, 0), new_nr_pages))
|
| 3699 | folio_set_has_hwpoisoned(folio);
|
| 3700 | |
| 3701 | * Skip the first new_nr_pages, since the new folio from them have all
|
| 3702 | * the flags from the original folio.
|
| 3703 |
|
| 3704 | for (i = new_nr_pages; i < nr_pages; i += new_nr_pages) {
|
| 3705 | struct page *new_head = &folio->page + i;
|
| 3706 | |
| 3707 | * Careful: new_folio is not a "real" folio before we cleared PageTail.
|
| 3708 | * Don't pass it around before clear_compound_head().
|
| 3709 |
|
| 3710 | struct folio *new_folio = (struct folio *)new_head;
|
| 3711 |
|
| 3712 | VM_BUG_ON_PAGE(atomic_read(&new_folio->_mapcount) != -1, new_head);
|
| 3713 |
|
| 3714 | |
| 3715 | * Clone page flags before unfreezing refcount.
|
| 3716 | *
|
| 3717 | * After successful get_page_unless_zero() might follow flags change,
|
| 3718 | * for example lock_page() which set PG_waiters.
|
| 3719 | *
|
| 3720 | * Note that for mapped sub-pages of an anonymous THP,
|
| 3721 | * PG_anon_exclusive has been cleared in unmap_folio() and is stored in
|
| 3722 | * the migration entry instead from where remap_page() will restore it.
|
| 3723 | * We can still have PG_anon_exclusive set on effectively unmapped and
|
| 3724 | * unreferenced sub-pages of an anonymous THP: we can simply drop
|
| 3725 | * PG_anon_exclusive (-> PG_mappedtodisk) for these here.
|
| 3726 |
|
| 3727 | new_folio->flags.f &= ~PAGE_FLAGS_CHECK_AT_PREP;
|
| 3728 | new_folio->flags.f |= (folio->flags.f &
|
| 3729 | ((1L << PG_referenced) |
|
| 3730 | (1L << PG_swapbacked) |
|
| 3731 | (1L << PG_swapcache) |
|
| 3732 | (1L << PG_mlocked) |
|
| 3733 | (1L << PG_uptodate) |
|
| 3734 | (1L << PG_active) |
|
| 3735 | (1L << PG_workingset) |
|
| 3736 | (1L << PG_locked) |
|
| 3737 | (1L << PG_unevictable) |
|
| 3738 | #ifdef CONFIG_ARCH_USES_PG_ARCH_2
|
| 3739 | (1L << PG_arch_2) |
|
| 3740 | #endif
|
| 3741 | #ifdef CONFIG_ARCH_USES_PG_ARCH_3
|
| 3742 | (1L << PG_arch_3) |
|
| 3743 | #endif
|
| 3744 | (1L << PG_dirty) |
|
| 3745 | (1L << PG_dropbehind) |
|
| 3746 | LRU_GEN_MASK | LRU_REFS_MASK));
|
| 3747 |
|
| 3748 | new_folio->mapping = folio->mapping;
|
| 3749 | new_folio->index = folio->index + i;
|
| 3750 |
|
| 3751 | |
| 3752 | * page->private should not be set in tail pages. Warn once
|
| 3753 | * if private is unexpectedly set. Do it before swap.val assignment
|
| 3754 | * since private overlaps with swap.val.
|
| 3755 |
|
| 3756 | VM_WARN_ON_ONCE_PAGE(new_folio->private, new_head);
|
| 3757 |
|
| 3758 | if (folio_test_swapcache(folio))
|
| 3759 | new_folio->swap.val = folio->swap.val + i;
|
| 3760 |
|
| 3761 |
|
| 3762 | smp_wmb();
|
| 3763 |
|
| 3764 | |
| 3765 | * Clear PageTail before unfreezing page refcount.
|
| 3766 | *
|
| 3767 | * After successful get_page_unless_zero() might follow put_page()
|
| 3768 | * which needs correct compound_head().
|
| 3769 |
|
| 3770 | clear_compound_head(new_head);
|
| 3771 | if (new_order) {
|
| 3772 | prep_compound_page(new_head, new_order);
|
| 3773 | folio_set_large_rmappable(new_folio);
|
| 3774 | }
|
| 3775 |
|
| 3776 | |
| 3777 | * PG_has_hwpoisoned is on the 2nd page, so set it after
|
| 3778 | * the compound head is prepped.
|
| 3779 |
|
| 3780 | if (handle_hwpoison &&
|
| 3781 | page_range_has_hwpoisoned(new_head, new_nr_pages))
|
| 3782 | folio_set_has_hwpoisoned(new_folio);
|
| 3783 |
|
| 3784 | if (folio_test_young(folio))
|
| 3785 | folio_set_young(new_folio);
|
| 3786 | if (folio_test_idle(folio))
|
| 3787 | folio_set_idle(new_folio);
|
| 3788 | #ifdef CONFIG_MEMCG
|
| 3789 | new_folio->memcg_data = folio->memcg_data;
|
| 3790 | #endif
|
| 3791 |
|
| 3792 | folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio));
|
| 3793 | }
|
| 3794 |
|
| 3795 | if (new_order)
|
| 3796 | folio_set_order(folio, new_order);
|
| 3797 | else
|
| 3798 | ClearPageCompound(&folio->page);
|
| 3799 | }
|
| 3800 |
|
| 3801 | |
| 3802 | * __split_unmapped_folio() - splits an unmapped @folio to lower order folios in
|
| 3803 | * two ways: uniform split or non-uniform split.
|
| 3804 | * @folio: the to-be-split folio
|
| 3805 | * @new_order: the smallest order of the after split folios (since buddy
|
| 3806 | * allocator like split generates folios with orders from @folio's
|
| 3807 | * order - 1 to new_order).
|
| 3808 | * @split_at: in buddy allocator like split, the folio containing @split_at
|
| 3809 | * will be split until its order becomes @new_order.
|
| 3810 | * @xas: xa_state pointing to folio->mapping->i_pages and locked by caller
|
| 3811 | * @mapping: @folio->mapping
|
| 3812 | * @split_type: if the split is uniform or not (buddy allocator like split)
|
| 3813 | *
|
| 3814 | *
|
| 3815 | * 1. uniform split: the given @folio into multiple @new_order small folios,
|
| 3816 | * where all small folios have the same order. This is done when
|
| 3817 | * split_type is SPLIT_TYPE_UNIFORM.
|
| 3818 | * 2. buddy allocator like (non-uniform) split: the given @folio is split into
|
| 3819 | * half and one of the half (containing the given page) is split into half
|
| 3820 | * until the given @folio's order becomes @new_order. This is done when
|
| 3821 | * split_type is SPLIT_TYPE_NON_UNIFORM.
|
| 3822 | *
|
| 3823 | * The high level flow for these two methods are:
|
| 3824 | *
|
| 3825 | * 1. uniform split: @xas is split with no expectation of failure and a single
|
| 3826 | * __split_folio_to_order() is called to split the @folio into @new_order
|
| 3827 | * along with stats update.
|
| 3828 | * 2. non-uniform split: folio_order - @new_order calls to
|
| 3829 | * __split_folio_to_order() are expected to be made in a for loop to split
|
| 3830 | * the @folio to one lower order at a time. The folio containing @split_at
|
| 3831 | * is split in each iteration. @xas is split into half in each iteration and
|
| 3832 | * can fail. A failed @xas split leaves split folios as is without merging
|
| 3833 | * them back.
|
| 3834 | *
|
| 3835 | * After splitting, the caller's folio reference will be transferred to the
|
| 3836 | * folio containing @split_at. The caller needs to unlock and/or free
|
| 3837 | * after-split folios if necessary.
|
| 3838 | *
|
| 3839 | * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be
|
| 3840 | * split but not to @new_order, the caller needs to check)
|
| 3841 |
|
| 3842 | static int __split_unmapped_folio(struct folio *folio, int new_order,
|
| 3843 | struct page *split_at, struct xa_state *xas,
|
| 3844 | struct address_space *mapping, enum split_type split_type)
|
| 3845 | {
|
| 3846 | const bool is_anon = folio_test_anon(folio);
|
| 3847 | int old_order = folio_order(folio);
|
| 3848 | int start_order = split_type == SPLIT_TYPE_UNIFORM ? new_order : old_order - 1;
|
| 3849 | struct folio *old_folio = folio;
|
| 3850 | int split_order;
|
| 3851 |
|
| 3852 | |
| 3853 | * split to new_order one order at a time. For uniform split,
|
| 3854 | * folio is split to new_order directly.
|
| 3855 |
|
| 3856 | for (split_order = start_order;
|
| 3857 | split_order >= new_order;
|
| 3858 | split_order--) {
|
| 3859 | int nr_new_folios = 1UL << (old_order - split_order);
|
| 3860 |
|
| 3861 |
|
| 3862 | if (is_anon && split_order == 1)
|
| 3863 | continue;
|
| 3864 |
|
| 3865 | if (mapping) {
|
| 3866 | |
| 3867 | * uniform split has xas_split_alloc() called before
|
| 3868 | * irq is disabled to allocate enough memory, whereas
|
| 3869 | * non-uniform split can handle ENOMEM.
|
| 3870 | * Use the to-be-split folio, so that a parallel
|
| 3871 | * folio_try_get() waits on it until xarray is updated
|
| 3872 | * with after-split folios and the original one is
|
| 3873 | * unfrozen.
|
| 3874 |
|
| 3875 | if (split_type == SPLIT_TYPE_UNIFORM) {
|
| 3876 | xas_split(xas, old_folio, old_order);
|
| 3877 | } else {
|
| 3878 | xas_set_order(xas, folio->index, split_order);
|
| 3879 | xas_try_split(xas, old_folio, old_order);
|
| 3880 | if (xas_error(xas))
|
| 3881 | return xas_error(xas);
|
| 3882 | }
|
| 3883 | }
|
| 3884 |
|
| 3885 | folio_split_memcg_refs(folio, old_order, split_order);
|
| 3886 | split_page_owner(&folio->page, old_order, split_order);
|
| 3887 | pgalloc_tag_split(folio, old_order, split_order);
|
| 3888 | __split_folio_to_order(folio, old_order, split_order);
|
| 3889 |
|
| 3890 | if (is_anon) {
|
| 3891 | mod_mthp_stat(old_order, MTHP_STAT_NR_ANON, -1);
|
| 3892 | mod_mthp_stat(split_order, MTHP_STAT_NR_ANON, nr_new_folios);
|
| 3893 | }
|
| 3894 | |
| 3895 | * If uniform split, the process is complete.
|
| 3896 | * If non-uniform, continue splitting the folio at @split_at
|
| 3897 | * as long as the next @split_order is >= @new_order.
|
| 3898 |
|
| 3899 | folio = page_folio(split_at);
|
| 3900 | old_order = split_order;
|
| 3901 | }
|
| 3902 |
|
| 3903 | return 0;
|
| 3904 | }
|
| 3905 |
|
| 3906 | |
| 3907 | * folio_check_splittable() - check if a folio can be split to a given order
|
| 3908 | * @folio: folio to be split
|
| 3909 | * @new_order: the smallest order of the after split folios (since buddy
|
| 3910 | * allocator like split generates folios with orders from @folio's
|
| 3911 | * order - 1 to new_order).
|
| 3912 | * @split_type: uniform or non-uniform split
|
| 3913 | *
|
| 3914 | * folio_check_splittable() checks if @folio can be split to @new_order using
|
| 3915 | * @split_type method. The truncated folio check must come first.
|
| 3916 | *
|
| 3917 | * Context: folio must be locked.
|
| 3918 | *
|
| 3919 | * Return: 0 - @folio can be split to @new_order, otherwise an error number is
|
| 3920 | * returned.
|
| 3921 |
|
| 3922 | int folio_check_splittable(struct folio *folio, unsigned int new_order,
|
| 3923 | enum split_type split_type)
|
| 3924 | {
|
| 3925 | VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
|
| 3926 | |
| 3927 | * Folios that just got truncated cannot get split. Signal to the
|
| 3928 | * caller that there was a race.
|
| 3929 | *
|
| 3930 | * TODO: this will also currently refuse folios without a mapping in the
|
| 3931 | * swapcache (shmem or to-be-anon folios).
|
| 3932 |
|
| 3933 | if (!folio->mapping && !folio_test_anon(folio))
|
| 3934 | return -EBUSY;
|
| 3935 |
|
| 3936 |
|
| 3937 | if (folio_test_anon(folio) && new_order == 1)
|
| 3938 | return -EINVAL;
|
| 3939 |
|
| 3940 | |
| 3941 | * swapcache folio could only be split to order 0
|
| 3942 | *
|
| 3943 | * non-uniform split creates after-split folios with orders from
|
| 3944 | * folio_order(folio) - 1 to new_order, making it not suitable for any
|
| 3945 | * swapcache folio split. Only uniform split to order-0 can be used
|
| 3946 | * here.
|
| 3947 |
|
| 3948 | if ((split_type == SPLIT_TYPE_NON_UNIFORM || new_order) && folio_test_swapcache(folio)) {
|
| 3949 | return -EINVAL;
|
| 3950 | }
|
| 3951 |
|
| 3952 | if (is_huge_zero_folio(folio))
|
| 3953 | return -EINVAL;
|
| 3954 |
|
| 3955 | if (folio_test_writeback(folio))
|
| 3956 | return -EBUSY;
|
| 3957 |
|
| 3958 | return 0;
|
| 3959 | }
|
| 3960 |
|
| 3961 |
|
| 3962 | static unsigned int folio_cache_ref_count(const struct folio *folio)
|
| 3963 | {
|
| 3964 | if (folio_test_anon(folio) && !folio_test_swapcache(folio))
|
| 3965 | return 0;
|
| 3966 | return folio_nr_pages(folio);
|
| 3967 | }
|
| 3968 |
|
| 3969 | static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int new_order,
|
| 3970 | struct page *split_at, struct xa_state *xas,
|
| 3971 | struct address_space *mapping, bool do_lru,
|
| 3972 | struct list_head *list, enum split_type split_type,
|
| 3973 | pgoff_t end, int *nr_shmem_dropped)
|
| 3974 | {
|
| 3975 | struct folio *end_folio = folio_next(folio);
|
| 3976 | struct folio *new_folio, *next;
|
| 3977 | int old_order = folio_order(folio);
|
| 3978 | struct list_lru_one *lru;
|
| 3979 | bool dequeue_deferred;
|
| 3980 | int ret = 0;
|
| 3981 |
|
| 3982 | VM_WARN_ON_ONCE(!mapping && end);
|
| 3983 | |
| 3984 | * If this folio can be on the deferred split queue, lock out
|
| 3985 | * the shrinker before freezing the ref. If the shrinker sees
|
| 3986 | * a 0-ref folio, it assumes it beat folio_put() to the list
|
| 3987 | * lock and must clean up the LRU state - the same dequeue we
|
| 3988 | * will do below as part of the split.
|
| 3989 |
|
| 3990 | dequeue_deferred = folio_test_anon(folio) && old_order > 1;
|
| 3991 | if (dequeue_deferred) {
|
| 3992 | struct mem_cgroup *memcg;
|
| 3993 |
|
| 3994 | rcu_read_lock();
|
| 3995 | memcg = folio_memcg(folio);
|
| 3996 | lru = list_lru_lock(&deferred_split_lru,
|
| 3997 | folio_nid(folio), &memcg);
|
| 3998 | }
|
| 3999 | if (folio_ref_freeze(folio, folio_cache_ref_count(folio) + 1)) {
|
| 4000 | struct swap_cluster_info *ci = NULL;
|
| 4001 | struct lruvec *lruvec;
|
| 4002 |
|
| 4003 | if (dequeue_deferred) {
|
| 4004 | __list_lru_del(&deferred_split_lru, lru,
|
| 4005 | &folio->_deferred_list, folio_nid(folio));
|
| 4006 | if (folio_test_partially_mapped(folio)) {
|
| 4007 | folio_clear_partially_mapped(folio);
|
| 4008 | mod_mthp_stat(old_order,
|
| 4009 | MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
|
| 4010 | }
|
| 4011 | list_lru_unlock(lru);
|
| 4012 | rcu_read_unlock();
|
| 4013 | }
|
| 4014 |
|
| 4015 | if (mapping) {
|
| 4016 | int nr = folio_nr_pages(folio);
|
| 4017 |
|
| 4018 | if (folio_test_pmd_mappable(folio) &&
|
| 4019 | new_order < HPAGE_PMD_ORDER) {
|
| 4020 | if (folio_test_swapbacked(folio)) {
|
| 4021 | lruvec_stat_mod_folio(folio,
|
| 4022 | NR_SHMEM_THPS, -nr);
|
| 4023 | } else {
|
| 4024 | lruvec_stat_mod_folio(folio,
|
| 4025 | NR_FILE_THPS, -nr);
|
| 4026 | }
|
| 4027 | }
|
| 4028 | }
|
| 4029 |
|
| 4030 | if (folio_test_swapcache(folio)) {
|
| 4031 | if (mapping) {
|
| 4032 | VM_WARN_ON_ONCE_FOLIO(mapping, folio);
|
| 4033 | return -EINVAL;
|
| 4034 | }
|
| 4035 |
|
| 4036 | ci = swap_cluster_get_and_lock(folio);
|
| 4037 | }
|
| 4038 |
|
| 4039 |
|
| 4040 | if (do_lru)
|
| 4041 | lruvec = folio_lruvec_lock(folio);
|
| 4042 |
|
| 4043 | ret = __split_unmapped_folio(folio, new_order, split_at, xas,
|
| 4044 | mapping, split_type);
|
| 4045 |
|
| 4046 | |
| 4047 | * Unfreeze after-split folios and put them back to the right
|
| 4048 | * list. @folio should be kept frozon until page cache
|
| 4049 | * entries are updated with all the other after-split folios
|
| 4050 | * to prevent others seeing stale page cache entries.
|
| 4051 | * As a result, new_folio starts from the next folio of
|
| 4052 | * @folio.
|
| 4053 |
|
| 4054 | for (new_folio = folio_next(folio); new_folio != end_folio;
|
| 4055 | new_folio = next) {
|
| 4056 | unsigned long nr_pages = folio_nr_pages(new_folio);
|
| 4057 |
|
| 4058 | next = folio_next(new_folio);
|
| 4059 |
|
| 4060 | zone_device_private_split_cb(folio, new_folio);
|
| 4061 |
|
| 4062 | folio_ref_unfreeze(new_folio,
|
| 4063 | folio_cache_ref_count(new_folio) + 1);
|
| 4064 |
|
| 4065 | if (do_lru)
|
| 4066 | lru_add_split_folio(folio, new_folio, lruvec, list);
|
| 4067 |
|
| 4068 | |
| 4069 | * Anonymous folio with swap cache.
|
| 4070 | * NOTE: shmem in swap cache is not supported yet.
|
| 4071 |
|
| 4072 | if (ci) {
|
| 4073 | __swap_cache_replace_folio(ci, folio, new_folio);
|
| 4074 | continue;
|
| 4075 | }
|
| 4076 |
|
| 4077 |
|
| 4078 | if (!mapping)
|
| 4079 | continue;
|
| 4080 |
|
| 4081 |
|
| 4082 | if (new_folio->index < end) {
|
| 4083 | __xa_store(&mapping->i_pages, new_folio->index,
|
| 4084 | new_folio, 0);
|
| 4085 | continue;
|
| 4086 | }
|
| 4087 |
|
| 4088 | VM_WARN_ON_ONCE(!nr_shmem_dropped);
|
| 4089 |
|
| 4090 | if (shmem_mapping(mapping) && nr_shmem_dropped)
|
| 4091 | *nr_shmem_dropped += nr_pages;
|
| 4092 | else if (folio_test_clear_dirty(new_folio))
|
| 4093 | folio_account_cleaned(
|
| 4094 | new_folio, inode_to_wb(mapping->host));
|
| 4095 | __filemap_remove_folio(new_folio, NULL);
|
| 4096 | folio_put_refs(new_folio, nr_pages);
|
| 4097 | }
|
| 4098 |
|
| 4099 | zone_device_private_split_cb(folio, NULL);
|
| 4100 | |
| 4101 | * Unfreeze @folio only after all page cache entries, which
|
| 4102 | * used to point to it, have been updated with new folios.
|
| 4103 | * Otherwise, a parallel folio_try_get() can grab @folio
|
| 4104 | * and its caller can see stale page cache entries.
|
| 4105 |
|
| 4106 | folio_ref_unfreeze(folio, folio_cache_ref_count(folio) + 1);
|
| 4107 |
|
| 4108 | if (do_lru)
|
| 4109 | lruvec_unlock(lruvec);
|
| 4110 |
|
| 4111 | if (ci)
|
| 4112 | swap_cluster_unlock(ci);
|
| 4113 | } else {
|
| 4114 | if (dequeue_deferred) {
|
| 4115 | list_lru_unlock(lru);
|
| 4116 | rcu_read_unlock();
|
| 4117 | }
|
| 4118 | return -EAGAIN;
|
| 4119 | }
|
| 4120 |
|
| 4121 | return ret;
|
| 4122 | }
|
| 4123 |
|
| 4124 | |
| 4125 | * __folio_split() - split a folio at @split_at to a @new_order folio
|
| 4126 | * @folio: folio to split
|
| 4127 | * @new_order: the order of the new folio
|
| 4128 | * @split_at: a page within the new folio
|
| 4129 | * @lock_at: a page within @folio to be left locked to caller
|
| 4130 | * @list: after-split folios will be put on it if non NULL
|
| 4131 | * @split_type: perform uniform split or not (non-uniform split)
|
| 4132 | *
|
| 4133 | * It calls __split_unmapped_folio() to perform uniform and non-uniform split.
|
| 4134 | * It is in charge of checking whether the split is supported or not and
|
| 4135 | * preparing @folio for __split_unmapped_folio().
|
| 4136 | *
|
| 4137 | * After splitting, the after-split folio containing @lock_at remains locked
|
| 4138 | * and others are unlocked:
|
| 4139 | * 1. for uniform split, @lock_at points to one of @folio's subpages;
|
| 4140 | * 2. for buddy allocator like (non-uniform) split, @lock_at points to @folio.
|
| 4141 | *
|
| 4142 | * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be
|
| 4143 | * split but not to @new_order, the caller needs to check)
|
| 4144 |
|
| 4145 | static int __folio_split(struct folio *folio, unsigned int new_order,
|
| 4146 | struct page *split_at, struct page *lock_at,
|
| 4147 | struct list_head *list, enum split_type split_type)
|
| 4148 | {
|
| 4149 | XA_STATE(xas, &folio->mapping->i_pages, folio->index);
|
| 4150 | struct folio *end_folio = folio_next(folio);
|
| 4151 | bool is_anon = folio_test_anon(folio);
|
| 4152 | struct mem_cgroup *memcg, *old_memcg;
|
| 4153 | struct address_space *mapping = NULL;
|
| 4154 | struct anon_vma *anon_vma = NULL;
|
| 4155 | int old_order = folio_order(folio);
|
| 4156 | struct folio *new_folio, *next;
|
| 4157 | int nr_shmem_dropped = 0;
|
| 4158 | enum ttu_flags ttu_flags = 0;
|
| 4159 | pgoff_t end = 0;
|
| 4160 | int ret;
|
| 4161 |
|
| 4162 | VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
|
| 4163 | VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
|
| 4164 |
|
| 4165 | if (folio != page_folio(split_at) || folio != page_folio(lock_at)) {
|
| 4166 | ret = -EINVAL;
|
| 4167 | goto out_no_memcg;
|
| 4168 | }
|
| 4169 |
|
| 4170 | if (new_order >= old_order) {
|
| 4171 | ret = -EINVAL;
|
| 4172 | goto out_no_memcg;
|
| 4173 | }
|
| 4174 |
|
| 4175 | ret = folio_check_splittable(folio, new_order, split_type);
|
| 4176 | if (ret) {
|
| 4177 | VM_WARN_ONCE(ret == -EINVAL, "Tried to split an unsplittable folio");
|
| 4178 | goto out_no_memcg;
|
| 4179 | }
|
| 4180 |
|
| 4181 | |
| 4182 | * switch to folio's memcg as xarray node allocation can happen and
|
| 4183 | * needs to charge to it.
|
| 4184 |
|
| 4185 | memcg = get_mem_cgroup_from_folio(folio);
|
| 4186 | old_memcg = set_active_memcg(memcg);
|
| 4187 |
|
| 4188 | if (is_anon) {
|
| 4189 | |
| 4190 | * The caller does not necessarily hold an mmap_lock that would
|
| 4191 | * prevent the anon_vma disappearing so we first we take a
|
| 4192 | * reference to it and then lock the anon_vma for write. This
|
| 4193 | * is similar to folio_lock_anon_vma_read except the write lock
|
| 4194 | * is taken to serialise against parallel split or collapse
|
| 4195 | * operations.
|
| 4196 |
|
| 4197 | anon_vma = folio_get_anon_vma(folio);
|
| 4198 | if (!anon_vma) {
|
| 4199 | ret = -EBUSY;
|
| 4200 | goto out;
|
| 4201 | }
|
| 4202 | anon_vma_lock_write(anon_vma);
|
| 4203 | mapping = NULL;
|
| 4204 | } else {
|
| 4205 | unsigned int min_order;
|
| 4206 | gfp_t gfp;
|
| 4207 |
|
| 4208 | mapping = folio->mapping;
|
| 4209 | min_order = mapping_min_folio_order(mapping);
|
| 4210 | if (new_order < min_order) {
|
| 4211 | ret = -EINVAL;
|
| 4212 | goto out;
|
| 4213 | }
|
| 4214 |
|
| 4215 | gfp = current_gfp_context(mapping_gfp_mask(mapping) &
|
| 4216 | GFP_RECLAIM_MASK);
|
| 4217 |
|
| 4218 | if (!filemap_release_folio(folio, gfp)) {
|
| 4219 | ret = -EBUSY;
|
| 4220 | goto out;
|
| 4221 | }
|
| 4222 |
|
| 4223 | mapping_set_update(&xas, mapping);
|
| 4224 |
|
| 4225 | if (split_type == SPLIT_TYPE_UNIFORM) {
|
| 4226 | xas_set_order(&xas, folio->index, new_order);
|
| 4227 | xas_split_alloc(&xas, folio, old_order, gfp);
|
| 4228 | if (xas_error(&xas)) {
|
| 4229 | ret = xas_error(&xas);
|
| 4230 | goto out;
|
| 4231 | }
|
| 4232 | }
|
| 4233 |
|
| 4234 | anon_vma = NULL;
|
| 4235 | i_mmap_lock_read(mapping);
|
| 4236 |
|
| 4237 | |
| 4238 | *__split_unmapped_folio() may need to trim off pages beyond
|
| 4239 | * EOF: but on 32-bit, i_size_read() takes an irq-unsafe
|
| 4240 | * seqlock, which cannot be nested inside the page tree lock.
|
| 4241 | * So note end now: i_size itself may be changed at any moment,
|
| 4242 | * but folio lock is good enough to serialize the trimming.
|
| 4243 |
|
| 4244 | end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
|
| 4245 | if (shmem_mapping(mapping))
|
| 4246 | end = shmem_fallocend(mapping->host, end);
|
| 4247 | }
|
| 4248 |
|
| 4249 | |
| 4250 | * Racy check if we can split the page, before unmap_folio() will
|
| 4251 | * split PMDs
|
| 4252 |
|
| 4253 | if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1) {
|
| 4254 | ret = -EAGAIN;
|
| 4255 | goto out_unlock;
|
| 4256 | }
|
| 4257 |
|
| 4258 | unmap_folio(folio);
|
| 4259 |
|
| 4260 |
|
| 4261 | local_irq_disable();
|
| 4262 | if (mapping) {
|
| 4263 | |
| 4264 | * Check if the folio is present in page cache.
|
| 4265 | * We assume all tail are present too, if folio is there.
|
| 4266 |
|
| 4267 | xas_lock(&xas);
|
| 4268 | xas_reset(&xas);
|
| 4269 | if (xas_load(&xas) != folio) {
|
| 4270 | ret = -EAGAIN;
|
| 4271 | goto fail;
|
| 4272 | }
|
| 4273 | }
|
| 4274 |
|
| 4275 | ret = __folio_freeze_and_split_unmapped(folio, new_order, split_at, &xas, mapping,
|
| 4276 | true, list, split_type, end, &nr_shmem_dropped);
|
| 4277 | fail:
|
| 4278 | if (mapping)
|
| 4279 | xas_unlock(&xas);
|
| 4280 |
|
| 4281 | local_irq_enable();
|
| 4282 |
|
| 4283 | if (nr_shmem_dropped)
|
| 4284 | shmem_uncharge(mapping->host, nr_shmem_dropped);
|
| 4285 |
|
| 4286 | if (!ret && is_anon && !folio_is_device_private(folio))
|
| 4287 | ttu_flags = TTU_USE_SHARED_ZEROPAGE;
|
| 4288 |
|
| 4289 | remap_page(folio, 1 << old_order, ttu_flags);
|
| 4290 |
|
| 4291 | |
| 4292 | * Drop the mapping while the inode is still pinned. @folio stays
|
| 4293 | * locked and present in the page cache until the loop below, so
|
| 4294 | * eviction cannot free the inode yet; @lock_at is not enough, it may
|
| 4295 | * be a tail beyond EOF that the split already dropped from the page
|
| 4296 | * cache. Nothing past this point may touch the inode or the mapping.
|
| 4297 |
|
| 4298 | if (mapping) {
|
| 4299 | i_mmap_unlock_read(mapping);
|
| 4300 | mapping = NULL;
|
| 4301 | }
|
| 4302 |
|
| 4303 | |
| 4304 | * Unlock all after-split folios except the one containing
|
| 4305 | * @lock_at page. If @folio is not split, it will be kept locked.
|
| 4306 |
|
| 4307 | for (new_folio = folio; new_folio != end_folio; new_folio = next) {
|
| 4308 | next = folio_next(new_folio);
|
| 4309 | if (new_folio == page_folio(lock_at))
|
| 4310 | continue;
|
| 4311 |
|
| 4312 | folio_unlock(new_folio);
|
| 4313 | |
| 4314 | * Subpages whose mapping has been zapped may be freed
|
| 4315 | * earlier, but freeing them requires taking the
|
| 4316 | * lru_lock, so we defer put_page() on tail pages until
|
| 4317 | * after the split completes.
|
| 4318 |
|
| 4319 | free_folio_and_swap_cache(new_folio);
|
| 4320 | }
|
| 4321 |
|
| 4322 | out_unlock:
|
| 4323 | if (anon_vma) {
|
| 4324 | anon_vma_unlock_write(anon_vma);
|
| 4325 | put_anon_vma(anon_vma);
|
| 4326 | }
|
| 4327 | if (mapping)
|
| 4328 | i_mmap_unlock_read(mapping);
|
| 4329 | out:
|
| 4330 |
|
| 4331 | set_active_memcg(old_memcg);
|
| 4332 | mem_cgroup_put(memcg);
|
| 4333 | out_no_memcg:
|
| 4334 | xas_destroy(&xas);
|
| 4335 | if (is_pmd_order(old_order))
|
| 4336 | count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
|
| 4337 | count_mthp_stat(old_order, !ret ? MTHP_STAT_SPLIT : MTHP_STAT_SPLIT_FAILED);
|
| 4338 | return ret;
|
| 4339 | }
|
| 4340 |
|
| 4341 | |
| 4342 | * folio_split_unmapped() - split a large anon folio that is already unmapped
|
| 4343 | * @folio: folio to split
|
| 4344 | * @new_order: the order of folios after split
|
| 4345 | *
|
| 4346 | * This function is a helper for splitting folios that have already been
|
| 4347 | * unmapped. The use case is that the device or the CPU can refuse to migrate
|
| 4348 | * THP pages in the middle of migration, due to allocation issues on either
|
| 4349 | * side.
|
| 4350 | *
|
| 4351 | * anon_vma_lock is not required to be held, mmap_read_lock() or
|
| 4352 | * mmap_write_lock() should be held. @folio is expected to be locked by the
|
| 4353 | * caller. device-private and non device-private folios are supported along
|
| 4354 | * with folios that are in the swapcache. @folio should also be unmapped and
|
| 4355 | * isolated from LRU (if applicable)
|
| 4356 | *
|
| 4357 | * Upon return, the folio is not remapped, split folios are not added to LRU,
|
| 4358 | * free_folio_and_swap_cache() is not called, and new folios remain locked.
|
| 4359 | *
|
| 4360 | * Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to
|
| 4361 | * insufficient reference count or extra pins).
|
| 4362 |
|
| 4363 | int folio_split_unmapped(struct folio *folio, unsigned int new_order)
|
| 4364 | {
|
| 4365 | int ret = 0;
|
| 4366 |
|
| 4367 | VM_WARN_ON_ONCE_FOLIO(folio_mapped(folio), folio);
|
| 4368 | VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
|
| 4369 | VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio);
|
| 4370 | VM_WARN_ON_ONCE_FOLIO(!folio_test_anon(folio), folio);
|
| 4371 |
|
| 4372 | if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1)
|
| 4373 | return -EAGAIN;
|
| 4374 |
|
| 4375 | local_irq_disable();
|
| 4376 | ret = __folio_freeze_and_split_unmapped(folio, new_order, &folio->page, NULL,
|
| 4377 | NULL, false, NULL, SPLIT_TYPE_UNIFORM,
|
| 4378 | 0, NULL);
|
| 4379 | local_irq_enable();
|
| 4380 | return ret;
|
| 4381 | }
|
| 4382 |
|
| 4383 | |
| 4384 | * This function splits a large folio into smaller folios of order @new_order.
|
| 4385 | * @page can point to any page of the large folio to split. The split operation
|
| 4386 | * does not change the position of @page.
|
| 4387 | *
|
| 4388 | * Prerequisites:
|
| 4389 | *
|
| 4390 | * 1) The caller must hold a reference on the @page's owning folio, also known
|
| 4391 | * as the large folio.
|
| 4392 | *
|
| 4393 | * 2) The large folio must be locked.
|
| 4394 | *
|
| 4395 | * 3) The folio must not be pinned. Any unexpected folio references, including
|
| 4396 | * GUP pins, will result in the folio not getting split; instead, the caller
|
| 4397 | * will receive an -EAGAIN.
|
| 4398 | *
|
| 4399 | * 4) @new_order > 1, usually. Splitting to order-1 anonymous folios is not
|
| 4400 | * supported for non-file-backed folios, because folio->_deferred_list, which
|
| 4401 | * is used by partially mapped folios, is stored in subpage 2, but an order-1
|
| 4402 | * folio only has subpages 0 and 1. File-backed order-1 folios are supported,
|
| 4403 | * since they do not use _deferred_list.
|
| 4404 | *
|
| 4405 | * After splitting, the caller's folio reference will be transferred to @page,
|
| 4406 | * resulting in a raised refcount of @page after this call. The other pages may
|
| 4407 | * be freed if they are not mapped.
|
| 4408 | *
|
| 4409 | * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
|
| 4410 | *
|
| 4411 | * Pages in @new_order will inherit the mapping, flags, and so on from the
|
| 4412 | * huge page.
|
| 4413 | *
|
| 4414 | * Returns 0 if the huge page was split successfully.
|
| 4415 | *
|
| 4416 | * Returns -EAGAIN if the folio has unexpected reference (e.g., GUP) or if
|
| 4417 | * the folio was concurrently removed from the page cache.
|
| 4418 | *
|
| 4419 | * Returns -EBUSY when trying to split the huge zeropage, if the folio is
|
| 4420 | * under writeback, if fs-specific folio metadata cannot currently be
|
| 4421 | * released, or if some unexpected race happened (e.g., anon VMA disappeared,
|
| 4422 | * truncation).
|
| 4423 | *
|
| 4424 | * Callers should ensure that the order respects the address space mapping
|
| 4425 | * min-order if one is set for non-anonymous folios.
|
| 4426 | *
|
| 4427 | * Returns -EINVAL when trying to split to an order that is incompatible
|
| 4428 | * with the folio. Splitting to order 0 is compatible with all folios.
|
| 4429 |
|
| 4430 | int __split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
|
| 4431 | unsigned int new_order)
|
| 4432 | {
|
| 4433 | struct folio *folio = page_folio(page);
|
| 4434 |
|
| 4435 | return __folio_split(folio, new_order, &folio->page, page, list,
|
| 4436 | SPLIT_TYPE_UNIFORM);
|
| 4437 | }
|
| 4438 |
|
| 4439 | |
| 4440 | * folio_split() - split a folio at @split_at to a @new_order folio
|
| 4441 | * @folio: folio to split
|
| 4442 | * @new_order: the order of the new folio
|
| 4443 | * @split_at: a page within the new folio
|
| 4444 | * @list: after-split folios are added to @list if not null, otherwise to LRU
|
| 4445 | * list
|
| 4446 | *
|
| 4447 | * It has the same prerequisites and returns as
|
| 4448 | * split_huge_page_to_list_to_order().
|
| 4449 | *
|
| 4450 | * Split a folio at @split_at to a new_order folio, leave the
|
| 4451 | * remaining subpages of the original folio as large as possible. For example,
|
| 4452 | * in the case of splitting an order-9 folio at its third order-3 subpages to
|
| 4453 | * an order-3 folio, there are 2^(9-3)=64 order-3 subpages in the order-9 folio.
|
| 4454 | * After the split, there will be a group of folios with different orders and
|
| 4455 | * the new folio containing @split_at is marked in bracket:
|
| 4456 | * [order-4, {order-3}, order-3, order-5, order-6, order-7, order-8].
|
| 4457 | *
|
| 4458 | * After split, folio is left locked for caller.
|
| 4459 | *
|
| 4460 | * Return: 0 - successful, <0 - failed (if -ENOMEM is returned, @folio might be
|
| 4461 | * split but not to @new_order, the caller needs to check)
|
| 4462 |
|
| 4463 | int folio_split(struct folio *folio, unsigned int new_order,
|
| 4464 | struct page *split_at, struct list_head *list)
|
| 4465 | {
|
| 4466 | return __folio_split(folio, new_order, split_at, &folio->page, list,
|
| 4467 | SPLIT_TYPE_NON_UNIFORM);
|
| 4468 | }
|
| 4469 |
|
| 4470 | |
| 4471 | * min_order_for_split() - get the minimum order @folio can be split to
|
| 4472 | * @folio: folio to split
|
| 4473 | *
|
| 4474 | * min_order_for_split() tells the minimum order @folio can be split to.
|
| 4475 | * If a file-backed folio is truncated, 0 will be returned. Any subsequent
|
| 4476 | * split attempt should get -EBUSY from split checking code.
|
| 4477 | *
|
| 4478 | * Return: @folio's minimum order for split
|
| 4479 |
|
| 4480 | unsigned int min_order_for_split(struct folio *folio)
|
| 4481 | {
|
| 4482 | if (folio_test_anon(folio))
|
| 4483 | return 0;
|
| 4484 |
|
| 4485 | |
| 4486 | * If the folio got truncated, we don't know the previous mapping and
|
| 4487 | * consequently the old min order. But it doesn't matter, as any split
|
| 4488 | * attempt will immediately fail with -EBUSY as the folio cannot get
|
| 4489 | * split until freed.
|
| 4490 |
|
| 4491 | if (!folio->mapping)
|
| 4492 | return 0;
|
| 4493 |
|
| 4494 | return mapping_min_folio_order(folio->mapping);
|
| 4495 | }
|
| 4496 |
|
| 4497 | int split_folio_to_list(struct folio *folio, struct list_head *list)
|
| 4498 | {
|
| 4499 | return split_huge_page_to_list_to_order(&folio->page, list, 0);
|
| 4500 | }
|
| 4501 |
|
| 4502 | |
| 4503 | * __folio_unqueue_deferred_split() is not to be called directly:
|
| 4504 | * the folio_unqueue_deferred_split() inline wrapper in mm/internal.h
|
| 4505 | * limits its calls to those folios which may have a _deferred_list for
|
| 4506 | * queueing THP splits, and that list is (racily observed to be) non-empty.
|
| 4507 | *
|
| 4508 | * It is unsafe to call folio_unqueue_deferred_split() until folio refcount is
|
| 4509 | * zero: because even when the list_lru lock is held, a non-empty
|
| 4510 | * _deferred_list might be in use on deferred_split_scan()'s unlocked
|
| 4511 | * on-stack list.
|
| 4512 | *
|
| 4513 | * The list_lru sublist is determined by folio's memcg: it is therefore
|
| 4514 | * important to unqueue deferred split before changing folio memcg.
|
| 4515 |
|
| 4516 | bool __folio_unqueue_deferred_split(struct folio *folio)
|
| 4517 | {
|
| 4518 | struct mem_cgroup *memcg;
|
| 4519 | struct list_lru_one *lru;
|
| 4520 | int nid = folio_nid(folio);
|
| 4521 | unsigned long flags;
|
| 4522 | bool unqueued = false;
|
| 4523 |
|
| 4524 | WARN_ON_ONCE(folio_ref_count(folio));
|
| 4525 | WARN_ON_ONCE(!mem_cgroup_disabled() && !folio_memcg_charged(folio));
|
| 4526 |
|
| 4527 | rcu_read_lock();
|
| 4528 | memcg = folio_memcg(folio);
|
| 4529 | lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags);
|
| 4530 | if (__list_lru_del(&deferred_split_lru, lru, &folio->_deferred_list, nid)) {
|
| 4531 | if (folio_test_partially_mapped(folio)) {
|
| 4532 | folio_clear_partially_mapped(folio);
|
| 4533 | mod_mthp_stat(folio_order(folio),
|
| 4534 | MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
|
| 4535 | }
|
| 4536 | unqueued = true;
|
| 4537 | }
|
| 4538 | list_lru_unlock_irqrestore(lru, &flags);
|
| 4539 | rcu_read_unlock();
|
| 4540 |
|
| 4541 | return unqueued;
|
| 4542 | }
|
| 4543 |
|
| 4544 |
|
| 4545 | void deferred_split_folio(struct folio *folio, bool partially_mapped)
|
| 4546 | {
|
| 4547 | struct list_lru_one *lru;
|
| 4548 | int nid;
|
| 4549 | struct mem_cgroup *memcg;
|
| 4550 | unsigned long flags;
|
| 4551 |
|
| 4552 | |
| 4553 | * Order 1 folios have no space for a deferred list, but we also
|
| 4554 | * won't waste much memory by not adding them to the deferred list.
|
| 4555 |
|
| 4556 | if (folio_order(folio) <= 1)
|
| 4557 | return;
|
| 4558 |
|
| 4559 | if (!partially_mapped && !split_underused_thp)
|
| 4560 | return;
|
| 4561 |
|
| 4562 | |
| 4563 | * Exclude swapcache: originally to avoid a corrupt deferred split
|
| 4564 | * queue. Nowadays that is fully prevented by __memcg1_swapout();
|
| 4565 | * but if page reclaim is already handling the same folio, it is
|
| 4566 | * unnecessary to handle it again in the shrinker, so excluding
|
| 4567 | * swapcache here may still be a useful optimization.
|
| 4568 |
|
| 4569 | if (folio_test_swapcache(folio))
|
| 4570 | return;
|
| 4571 |
|
| 4572 | nid = folio_nid(folio);
|
| 4573 |
|
| 4574 | rcu_read_lock();
|
| 4575 | memcg = folio_memcg(folio);
|
| 4576 | lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags);
|
| 4577 | if (partially_mapped) {
|
| 4578 | if (!folio_test_partially_mapped(folio)) {
|
| 4579 | folio_set_partially_mapped(folio);
|
| 4580 | if (folio_test_pmd_mappable(folio))
|
| 4581 | count_vm_event(THP_DEFERRED_SPLIT_PAGE);
|
| 4582 | count_mthp_stat(folio_order(folio), MTHP_STAT_SPLIT_DEFERRED);
|
| 4583 | mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, 1);
|
| 4584 | }
|
| 4585 | } else {
|
| 4586 |
|
| 4587 | VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio);
|
| 4588 | }
|
| 4589 | __list_lru_add(&deferred_split_lru, lru, &folio->_deferred_list, nid, memcg);
|
| 4590 | list_lru_unlock_irqrestore(lru, &flags);
|
| 4591 | rcu_read_unlock();
|
| 4592 | }
|
| 4593 |
|
| 4594 | static unsigned long deferred_split_count(struct shrinker *shrink,
|
| 4595 | struct shrink_control *sc)
|
| 4596 | {
|
| 4597 | unsigned long count;
|
| 4598 |
|
| 4599 | count = list_lru_shrink_count(&deferred_split_lru, sc);
|
| 4600 | return count ?: SHRINK_EMPTY;
|
| 4601 | }
|
| 4602 |
|
| 4603 | static bool thp_underused(struct folio *folio)
|
| 4604 | {
|
| 4605 | int num_zero_pages = 0, num_filled_pages = 0;
|
| 4606 | int i;
|
| 4607 |
|
| 4608 | if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
|
| 4609 | return false;
|
| 4610 |
|
| 4611 | if (folio_contain_hwpoisoned_page(folio))
|
| 4612 | return false;
|
| 4613 |
|
| 4614 | for (i = 0; i < folio_nr_pages(folio); i++) {
|
| 4615 | if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
|
| 4616 | if (++num_zero_pages > khugepaged_max_ptes_none)
|
| 4617 | return true;
|
| 4618 | } else {
|
| 4619 | |
| 4620 | * Another path for early exit once the number
|
| 4621 | * of non-zero filled pages exceeds threshold.
|
| 4622 |
|
| 4623 | if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
|
| 4624 | return false;
|
| 4625 | }
|
| 4626 | }
|
| 4627 | return false;
|
| 4628 | }
|
| 4629 |
|
| 4630 | static enum lru_status deferred_split_isolate(struct list_head *item,
|
| 4631 | struct list_lru_one *lru,
|
| 4632 | void *cb_arg)
|
| 4633 | {
|
| 4634 | struct folio *folio = container_of(item, struct folio, _deferred_list);
|
| 4635 | struct list_head *freeable = cb_arg;
|
| 4636 |
|
| 4637 | if (folio_try_get(folio)) {
|
| 4638 | list_lru_isolate_move(lru, item, freeable);
|
| 4639 | return LRU_REMOVED;
|
| 4640 | }
|
| 4641 |
|
| 4642 | |
| 4643 | * We lost race with folio_put(). Read folio state before the
|
| 4644 | * isolate: folio_unqueue_deferred_split() checks list_empty()
|
| 4645 | * locklessly, so once removed the folio can be freed any time.
|
| 4646 |
|
| 4647 | if (folio_test_partially_mapped(folio)) {
|
| 4648 | folio_clear_partially_mapped(folio);
|
| 4649 | mod_mthp_stat(folio_order(folio),
|
| 4650 | MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
|
| 4651 | }
|
| 4652 | list_lru_isolate(lru, item);
|
| 4653 | return LRU_REMOVED;
|
| 4654 | }
|
| 4655 |
|
| 4656 | static unsigned long deferred_split_scan(struct shrinker *shrink,
|
| 4657 | struct shrink_control *sc)
|
| 4658 | {
|
| 4659 | LIST_HEAD(dispose);
|
| 4660 | struct folio *folio, *next;
|
| 4661 | int split = 0;
|
| 4662 | unsigned long isolated;
|
| 4663 |
|
| 4664 | isolated = list_lru_shrink_walk_irq(&deferred_split_lru, sc,
|
| 4665 | deferred_split_isolate, &dispose);
|
| 4666 |
|
| 4667 | list_for_each_entry_safe(folio, next, &dispose, _deferred_list) {
|
| 4668 | bool did_split = false;
|
| 4669 | bool underused = false;
|
| 4670 |
|
| 4671 | list_del_init(&folio->_deferred_list);
|
| 4672 |
|
| 4673 | if (!folio_test_partially_mapped(folio)) {
|
| 4674 | |
| 4675 | * See try_to_map_unused_to_zeropage(): we cannot
|
| 4676 | * optimize zero-filled pages after splitting an
|
| 4677 | * mlocked folio.
|
| 4678 |
|
| 4679 | if (folio_test_mlocked(folio))
|
| 4680 | goto next;
|
| 4681 | underused = thp_underused(folio);
|
| 4682 | if (!underused)
|
| 4683 | goto next;
|
| 4684 | }
|
| 4685 | if (!folio_trylock(folio))
|
| 4686 | goto requeue;
|
| 4687 | if (!split_folio(folio)) {
|
| 4688 | did_split = true;
|
| 4689 | if (underused)
|
| 4690 | count_vm_event(THP_UNDERUSED_SPLIT_PAGE);
|
| 4691 | split++;
|
| 4692 | }
|
| 4693 | folio_unlock(folio);
|
| 4694 | next:
|
| 4695 | |
| 4696 | * If thp_underused() returns false, or if split_folio()
|
| 4697 | * succeeds, or if split_folio() fails in the case it was
|
| 4698 | * underused, then consider it used and don't add it back to
|
| 4699 | * split_queue.
|
| 4700 |
|
| 4701 | if (!did_split && folio_test_partially_mapped(folio)) {
|
| 4702 | requeue:
|
| 4703 | rcu_read_lock();
|
| 4704 | list_lru_add_irq(&deferred_split_lru,
|
| 4705 | &folio->_deferred_list,
|
| 4706 | folio_nid(folio),
|
| 4707 | folio_memcg(folio));
|
| 4708 | rcu_read_unlock();
|
| 4709 | }
|
| 4710 | folio_put(folio);
|
| 4711 | }
|
| 4712 |
|
| 4713 | if (!split && !isolated)
|
| 4714 | return SHRINK_STOP;
|
| 4715 | return split;
|
| 4716 | }
|
| 4717 |
|
| 4718 | #ifdef CONFIG_DEBUG_FS
|
| 4719 | static void split_huge_pages_all(void)
|
| 4720 | {
|
| 4721 | struct zone *zone;
|
| 4722 | struct page *page;
|
| 4723 | struct folio *folio;
|
| 4724 | unsigned long pfn, max_zone_pfn;
|
| 4725 | unsigned long total = 0, split = 0;
|
| 4726 |
|
| 4727 | pr_debug("Split all THPs\n");
|
| 4728 | for_each_zone(zone) {
|
| 4729 | if (!managed_zone(zone))
|
| 4730 | continue;
|
| 4731 | max_zone_pfn = zone_end_pfn(zone);
|
| 4732 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
|
| 4733 | int nr_pages;
|
| 4734 |
|
| 4735 | page = pfn_to_online_page(pfn);
|
| 4736 | if (!page || PageTail(page))
|
| 4737 | continue;
|
| 4738 | folio = page_folio(page);
|
| 4739 | if (!folio_try_get(folio))
|
| 4740 | continue;
|
| 4741 |
|
| 4742 | if (unlikely(page_folio(page) != folio))
|
| 4743 | goto next;
|
| 4744 |
|
| 4745 | if (zone != folio_zone(folio))
|
| 4746 | goto next;
|
| 4747 |
|
| 4748 | if (!folio_test_large(folio)
|
| 4749 | || folio_test_hugetlb(folio)
|
| 4750 | || !folio_test_lru(folio))
|
| 4751 | goto next;
|
| 4752 |
|
| 4753 | total++;
|
| 4754 | folio_lock(folio);
|
| 4755 | nr_pages = folio_nr_pages(folio);
|
| 4756 | if (!split_folio(folio))
|
| 4757 | split++;
|
| 4758 | pfn += nr_pages - 1;
|
| 4759 | folio_unlock(folio);
|
| 4760 | next:
|
| 4761 | folio_put(folio);
|
| 4762 | cond_resched();
|
| 4763 | }
|
| 4764 | }
|
| 4765 |
|
| 4766 | pr_debug("%lu of %lu THP split\n", split, total);
|
| 4767 | }
|
| 4768 |
|
| 4769 | static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
|
| 4770 | {
|
| 4771 | if (vma_is_dax(vma))
|
| 4772 | return true;
|
| 4773 | if (vma_is_special_huge(vma))
|
| 4774 | return true;
|
| 4775 | if (vma_test(vma, VMA_IO_BIT))
|
| 4776 | return true;
|
| 4777 | if (is_vm_hugetlb_page(vma))
|
| 4778 | return true;
|
| 4779 |
|
| 4780 | return false;
|
| 4781 | }
|
| 4782 |
|
| 4783 | static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
|
| 4784 | unsigned long vaddr_end, unsigned int new_order,
|
| 4785 | long in_folio_offset)
|
| 4786 | {
|
| 4787 | int ret = 0;
|
| 4788 | struct task_struct *task;
|
| 4789 | struct mm_struct *mm;
|
| 4790 | unsigned long total = 0, split = 0;
|
| 4791 | unsigned long addr;
|
| 4792 |
|
| 4793 | vaddr_start &= PAGE_MASK;
|
| 4794 | vaddr_end &= PAGE_MASK;
|
| 4795 |
|
| 4796 | task = find_get_task_by_vpid(pid);
|
| 4797 | if (!task) {
|
| 4798 | ret = -ESRCH;
|
| 4799 | goto out;
|
| 4800 | }
|
| 4801 |
|
| 4802 |
|
| 4803 | mm = get_task_mm(task);
|
| 4804 | put_task_struct(task);
|
| 4805 |
|
| 4806 | if (!mm) {
|
| 4807 | ret = -EINVAL;
|
| 4808 | goto out;
|
| 4809 | }
|
| 4810 |
|
| 4811 | pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx], new_order: %u, in_folio_offset: %ld\n",
|
| 4812 | pid, vaddr_start, vaddr_end, new_order, in_folio_offset);
|
| 4813 |
|
| 4814 | mmap_read_lock(mm);
|
| 4815 | |
| 4816 | * always increase addr by PAGE_SIZE, since we could have a PTE page
|
| 4817 | * table filled with PTE-mapped THPs, each of which is distinct.
|
| 4818 |
|
| 4819 | for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
|
| 4820 | struct vm_area_struct *vma = vma_lookup(mm, addr);
|
| 4821 | struct folio_walk fw;
|
| 4822 | struct folio *folio;
|
| 4823 | struct address_space *mapping;
|
| 4824 | unsigned int target_order = new_order;
|
| 4825 |
|
| 4826 | if (!vma)
|
| 4827 | break;
|
| 4828 |
|
| 4829 |
|
| 4830 | if (vma_not_suitable_for_thp_split(vma)) {
|
| 4831 | addr = vma->vm_end;
|
| 4832 | continue;
|
| 4833 | }
|
| 4834 |
|
| 4835 | folio = folio_walk_start(&fw, vma, addr, 0);
|
| 4836 | if (!folio)
|
| 4837 | continue;
|
| 4838 |
|
| 4839 | if (!is_transparent_hugepage(folio))
|
| 4840 | goto next;
|
| 4841 |
|
| 4842 | if (!folio_test_anon(folio)) {
|
| 4843 | mapping = folio->mapping;
|
| 4844 | target_order = max(new_order,
|
| 4845 | mapping_min_folio_order(mapping));
|
| 4846 | }
|
| 4847 |
|
| 4848 | if (target_order >= folio_order(folio))
|
| 4849 | goto next;
|
| 4850 |
|
| 4851 | total++;
|
| 4852 | |
| 4853 | * For folios with private, split_huge_page_to_list_to_order()
|
| 4854 | * will try to drop it before split and then check if the folio
|
| 4855 | * can be split or not. So skip the check here.
|
| 4856 |
|
| 4857 | if (!folio_test_private(folio) &&
|
| 4858 | folio_expected_ref_count(folio) != folio_ref_count(folio))
|
| 4859 | goto next;
|
| 4860 |
|
| 4861 | if (!folio_trylock(folio))
|
| 4862 | goto next;
|
| 4863 | folio_get(folio);
|
| 4864 | folio_walk_end(&fw, vma);
|
| 4865 |
|
| 4866 | if (!folio_test_anon(folio) && folio->mapping != mapping)
|
| 4867 | goto unlock;
|
| 4868 |
|
| 4869 | if (in_folio_offset < 0 ||
|
| 4870 | in_folio_offset >= folio_nr_pages(folio)) {
|
| 4871 | if (!split_folio_to_order(folio, target_order))
|
| 4872 | split++;
|
| 4873 | } else {
|
| 4874 | struct page *split_at = folio_page(folio,
|
| 4875 | in_folio_offset);
|
| 4876 | if (!folio_split(folio, target_order, split_at, NULL))
|
| 4877 | split++;
|
| 4878 | }
|
| 4879 |
|
| 4880 | unlock:
|
| 4881 |
|
| 4882 | folio_unlock(folio);
|
| 4883 | folio_put(folio);
|
| 4884 |
|
| 4885 | cond_resched();
|
| 4886 | continue;
|
| 4887 | next:
|
| 4888 | folio_walk_end(&fw, vma);
|
| 4889 | cond_resched();
|
| 4890 | }
|
| 4891 | mmap_read_unlock(mm);
|
| 4892 | mmput(mm);
|
| 4893 |
|
| 4894 | pr_debug("%lu of %lu THP split\n", split, total);
|
| 4895 |
|
| 4896 | out:
|
| 4897 | return ret;
|
| 4898 | }
|
| 4899 |
|
| 4900 | static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
|
| 4901 | pgoff_t off_end, unsigned int new_order,
|
| 4902 | long in_folio_offset)
|
| 4903 | {
|
| 4904 | struct file *candidate;
|
| 4905 | struct address_space *mapping;
|
| 4906 | pgoff_t index;
|
| 4907 | int nr_pages = 1;
|
| 4908 | unsigned long total = 0, split = 0;
|
| 4909 | unsigned int min_order;
|
| 4910 | unsigned int target_order;
|
| 4911 |
|
| 4912 | CLASS(filename_kernel, file)(file_path);
|
| 4913 | candidate = file_open_name(file, O_RDONLY, 0);
|
| 4914 | if (IS_ERR(candidate))
|
| 4915 | return -EINVAL;
|
| 4916 |
|
| 4917 | pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx], new_order: %u, in_folio_offset: %ld\n",
|
| 4918 | file_path, off_start, off_end, new_order, in_folio_offset);
|
| 4919 |
|
| 4920 | mapping = candidate->f_mapping;
|
| 4921 | min_order = mapping_min_folio_order(mapping);
|
| 4922 | target_order = max(new_order, min_order);
|
| 4923 |
|
| 4924 | for (index = off_start; index < off_end; index += nr_pages) {
|
| 4925 | struct folio *folio = filemap_get_folio(mapping, index);
|
| 4926 |
|
| 4927 | nr_pages = 1;
|
| 4928 | if (IS_ERR(folio))
|
| 4929 | continue;
|
| 4930 |
|
| 4931 | if (!folio_test_large(folio))
|
| 4932 | goto next;
|
| 4933 |
|
| 4934 | total++;
|
| 4935 | nr_pages = folio_nr_pages(folio);
|
| 4936 |
|
| 4937 | if (target_order >= folio_order(folio))
|
| 4938 | goto next;
|
| 4939 |
|
| 4940 | if (!folio_trylock(folio))
|
| 4941 | goto next;
|
| 4942 |
|
| 4943 | if (folio->mapping != mapping)
|
| 4944 | goto unlock;
|
| 4945 |
|
| 4946 | if (in_folio_offset < 0 || in_folio_offset >= nr_pages) {
|
| 4947 | if (!split_folio_to_order(folio, target_order))
|
| 4948 | split++;
|
| 4949 | } else {
|
| 4950 | struct page *split_at = folio_page(folio,
|
| 4951 | in_folio_offset);
|
| 4952 | if (!folio_split(folio, target_order, split_at, NULL))
|
| 4953 | split++;
|
| 4954 | }
|
| 4955 |
|
| 4956 | unlock:
|
| 4957 | folio_unlock(folio);
|
| 4958 | next:
|
| 4959 | folio_put(folio);
|
| 4960 | cond_resched();
|
| 4961 | }
|
| 4962 |
|
| 4963 | filp_close(candidate, NULL);
|
| 4964 | pr_debug("%lu of %lu file-backed THP split\n", split, total);
|
| 4965 | return 0;
|
| 4966 | }
|
| 4967 |
|
| 4968 | #define MAX_INPUT_BUF_SZ 255
|
| 4969 |
|
| 4970 | static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
|
| 4971 | size_t count, loff_t *ppops)
|
| 4972 | {
|
| 4973 | static DEFINE_MUTEX(split_debug_mutex);
|
| 4974 | ssize_t ret;
|
| 4975 | |
| 4976 | * hold pid, start_vaddr, end_vaddr, new_order or
|
| 4977 | * file_path, off_start, off_end, new_order
|
| 4978 |
|
| 4979 | char input_buf[MAX_INPUT_BUF_SZ];
|
| 4980 | int pid;
|
| 4981 | unsigned long vaddr_start, vaddr_end;
|
| 4982 | unsigned int new_order = 0;
|
| 4983 | long in_folio_offset = -1;
|
| 4984 |
|
| 4985 | ret = mutex_lock_interruptible(&split_debug_mutex);
|
| 4986 | if (ret)
|
| 4987 | return ret;
|
| 4988 |
|
| 4989 | ret = -EFAULT;
|
| 4990 |
|
| 4991 | memset(input_buf, 0, MAX_INPUT_BUF_SZ);
|
| 4992 | if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
|
| 4993 | goto out;
|
| 4994 |
|
| 4995 | input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
|
| 4996 |
|
| 4997 | if (input_buf[0] == '/') {
|
| 4998 | char *tok;
|
| 4999 | char *tok_buf = input_buf;
|
| 5000 | char file_path[MAX_INPUT_BUF_SZ];
|
| 5001 | pgoff_t off_start = 0, off_end = 0;
|
| 5002 | size_t input_len = strlen(input_buf);
|
| 5003 |
|
| 5004 | tok = strsep(&tok_buf, ",");
|
| 5005 | if (tok && tok_buf) {
|
| 5006 | strscpy(file_path, tok);
|
| 5007 | } else {
|
| 5008 | ret = -EINVAL;
|
| 5009 | goto out;
|
| 5010 | }
|
| 5011 |
|
| 5012 | ret = sscanf(tok_buf, "0x%lx,0x%lx,%d,%ld", &off_start, &off_end,
|
| 5013 | &new_order, &in_folio_offset);
|
| 5014 | if (ret != 2 && ret != 3 && ret != 4) {
|
| 5015 | ret = -EINVAL;
|
| 5016 | goto out;
|
| 5017 | }
|
| 5018 | ret = split_huge_pages_in_file(file_path, off_start, off_end,
|
| 5019 | new_order, in_folio_offset);
|
| 5020 | if (!ret)
|
| 5021 | ret = input_len;
|
| 5022 |
|
| 5023 | goto out;
|
| 5024 | }
|
| 5025 |
|
| 5026 | ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d,%ld", &pid, &vaddr_start,
|
| 5027 | &vaddr_end, &new_order, &in_folio_offset);
|
| 5028 | if (ret == 1 && pid == 1) {
|
| 5029 | split_huge_pages_all();
|
| 5030 | ret = strlen(input_buf);
|
| 5031 | goto out;
|
| 5032 | } else if (ret != 3 && ret != 4 && ret != 5) {
|
| 5033 | ret = -EINVAL;
|
| 5034 | goto out;
|
| 5035 | }
|
| 5036 |
|
| 5037 | ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order,
|
| 5038 | in_folio_offset);
|
| 5039 | if (!ret)
|
| 5040 | ret = strlen(input_buf);
|
| 5041 | out:
|
| 5042 | mutex_unlock(&split_debug_mutex);
|
| 5043 | return ret;
|
| 5044 |
|
| 5045 | }
|
| 5046 |
|
| 5047 | static const struct file_operations split_huge_pages_fops = {
|
| 5048 | .owner = THIS_MODULE,
|
| 5049 | .write = split_huge_pages_write,
|
| 5050 | };
|
| 5051 |
|
| 5052 | static int __init split_huge_pages_debugfs(void)
|
| 5053 | {
|
| 5054 | debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
|
| 5055 | &split_huge_pages_fops);
|
| 5056 | return 0;
|
| 5057 | }
|
| 5058 | late_initcall(split_huge_pages_debugfs);
|
| 5059 | #endif
|
| 5060 |
|
| 5061 | #ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
|
| 5062 | int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
|
| 5063 | struct page *page)
|
| 5064 | {
|
| 5065 | struct folio *folio = page_folio(page);
|
| 5066 | struct vm_area_struct *vma = pvmw->vma;
|
| 5067 | struct mm_struct *mm = vma->vm_mm;
|
| 5068 | unsigned long address = pvmw->address;
|
| 5069 | bool anon_exclusive, present, writable, softdirty, uffd_wp;
|
| 5070 | pmd_t pmdval;
|
| 5071 | swp_entry_t entry;
|
| 5072 | pmd_t pmdswp;
|
| 5073 |
|
| 5074 | if (!(pvmw->pmd && !pvmw->pte))
|
| 5075 | return 0;
|
| 5076 |
|
| 5077 | present = pmd_present(*pvmw->pmd);
|
| 5078 | if (likely(present)) {
|
| 5079 | flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
|
| 5080 |
|
| 5081 | pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
|
| 5082 |
|
| 5083 | writable = pmd_write(pmdval);
|
| 5084 | softdirty = pmd_soft_dirty(pmdval);
|
| 5085 | uffd_wp = pmd_uffd(pmdval);
|
| 5086 | } else {
|
| 5087 | softleaf_t old_entry;
|
| 5088 |
|
| 5089 | pmdval = pmdp_huge_get_and_clear(vma->vm_mm, address, pvmw->pmd);
|
| 5090 | old_entry = softleaf_from_pmd(pmdval);
|
| 5091 |
|
| 5092 | writable = softleaf_is_device_private_write(old_entry);
|
| 5093 | softdirty = pmd_swp_soft_dirty(pmdval);
|
| 5094 | uffd_wp = pmd_swp_uffd(pmdval);
|
| 5095 | }
|
| 5096 |
|
| 5097 |
|
| 5098 | anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(page);
|
| 5099 | if (anon_exclusive && folio_try_share_anon_rmap_pmd(folio, page)) {
|
| 5100 | set_pmd_at(mm, address, pvmw->pmd, pmdval);
|
| 5101 | return -EBUSY;
|
| 5102 | }
|
| 5103 |
|
| 5104 |
|
| 5105 | if (writable)
|
| 5106 | entry = make_writable_migration_entry(page_to_pfn(page));
|
| 5107 | else if (anon_exclusive)
|
| 5108 | entry = make_readable_exclusive_migration_entry(page_to_pfn(page));
|
| 5109 | else
|
| 5110 | entry = make_readable_migration_entry(page_to_pfn(page));
|
| 5111 |
|
| 5112 |
|
| 5113 | if (present && pmd_young(pmdval))
|
| 5114 | entry = make_migration_entry_young(entry);
|
| 5115 | if (present && pmd_dirty(pmdval)) {
|
| 5116 | folio_mark_dirty(folio);
|
| 5117 | entry = make_migration_entry_dirty(entry);
|
| 5118 | }
|
| 5119 |
|
| 5120 |
|
| 5121 | pmdswp = softleaf_to_pmd(entry);
|
| 5122 | if (softdirty)
|
| 5123 | pmdswp = pmd_swp_mksoft_dirty(pmdswp);
|
| 5124 | if (uffd_wp)
|
| 5125 | pmdswp = pmd_swp_mkuffd(pmdswp);
|
| 5126 | set_pmd_at(mm, address, pvmw->pmd, pmdswp);
|
| 5127 |
|
| 5128 |
|
| 5129 | folio_remove_rmap_pmd(folio, page, vma);
|
| 5130 | folio_put(folio);
|
| 5131 | trace_set_migration_pmd(address, pmd_val(pmdswp));
|
| 5132 |
|
| 5133 | return 0;
|
| 5134 | }
|
| 5135 |
|
| 5136 | void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct folio *folio)
|
| 5137 | {
|
| 5138 | struct vm_area_struct *vma = pvmw->vma;
|
| 5139 | struct mm_struct *mm = vma->vm_mm;
|
| 5140 | unsigned long address = pvmw->address;
|
| 5141 | unsigned long haddr = address & HPAGE_PMD_MASK;
|
| 5142 | pmd_t pmde;
|
| 5143 | softleaf_t entry;
|
| 5144 |
|
| 5145 | if (!(pvmw->pmd && !pvmw->pte))
|
| 5146 | return;
|
| 5147 |
|
| 5148 | entry = softleaf_from_pmd(*pvmw->pmd);
|
| 5149 | folio_get(folio);
|
| 5150 | pmde = folio_mk_pmd(folio, READ_ONCE(vma->vm_page_prot));
|
| 5151 |
|
| 5152 | if (pmd_swp_soft_dirty(*pvmw->pmd))
|
| 5153 | pmde = pmd_mksoft_dirty(pmde);
|
| 5154 | if (softleaf_is_migration_write(entry))
|
| 5155 | pmde = pmd_mkwrite(pmde, vma);
|
| 5156 | if (pmd_swp_uffd(*pvmw->pmd))
|
| 5157 | pmde = pmd_mkuffd(pmde);
|
| 5158 |
|
| 5159 |
|
| 5160 | if (pmd_swp_uffd(*pvmw->pmd) && userfaultfd_rwp(vma))
|
| 5161 | pmde = pmd_modify(pmde, PAGE_NONE);
|
| 5162 |
|
| 5163 | if (!softleaf_is_migration_young(entry))
|
| 5164 | pmde = pmd_mkold(pmde);
|
| 5165 |
|
| 5166 | if (folio_test_dirty(folio) && softleaf_is_migration_dirty(entry))
|
| 5167 | pmde = pmd_mkdirty(pmde);
|
| 5168 |
|
| 5169 | if (folio_is_device_private(folio)) {
|
| 5170 | swp_entry_t entry;
|
| 5171 |
|
| 5172 | if (pmd_write(pmde))
|
| 5173 | entry = make_writable_device_private_entry(folio_pfn(folio));
|
| 5174 | else
|
| 5175 | entry = make_readable_device_private_entry(folio_pfn(folio));
|
| 5176 | pmde = softleaf_to_pmd(entry);
|
| 5177 |
|
| 5178 | if (pmd_swp_soft_dirty(*pvmw->pmd))
|
| 5179 | pmde = pmd_swp_mksoft_dirty(pmde);
|
| 5180 | if (pmd_swp_uffd(*pvmw->pmd))
|
| 5181 | pmde = pmd_swp_mkuffd(pmde);
|
| 5182 | }
|
| 5183 |
|
| 5184 | if (folio_test_anon(folio)) {
|
| 5185 | rmap_t rmap_flags = RMAP_NONE;
|
| 5186 |
|
| 5187 | if (!softleaf_is_migration_read(entry))
|
| 5188 | rmap_flags |= RMAP_EXCLUSIVE;
|
| 5189 |
|
| 5190 | folio_add_anon_rmap_pmd(folio, &folio->page, vma, haddr, rmap_flags);
|
| 5191 | } else {
|
| 5192 | folio_add_file_rmap_pmd(folio, &folio->page, vma);
|
| 5193 | }
|
| 5194 | VM_WARN_ON_ONCE(pmd_write(pmde) && folio_test_anon(folio) &&
|
| 5195 | !PageAnonExclusive(&folio->page));
|
| 5196 | set_pmd_at(mm, haddr, pvmw->pmd, pmde);
|
| 5197 |
|
| 5198 |
|
| 5199 | update_mmu_cache_pmd(vma, address, pvmw->pmd);
|
| 5200 | trace_remove_migration_pmd(address, pmd_val(pmde));
|
| 5201 | }
|
| 5202 | #endif
|