1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_HUGETLB_H 3 #define _LINUX_HUGETLB_H 4 5 #include <linux/mm.h> 6 #include <linux/mm_types.h> 7 #include <linux/mmdebug.h> 8 #include <linux/fs.h> 9 #include <linux/hugetlb_inline.h> 10 #include <linux/cgroup.h> 11 #include <linux/page_ref.h> 12 #include <linux/list.h> 13 #include <linux/kref.h> 14 #include <linux/pgtable.h> 15 #include <linux/gfp.h> 16 #include <linux/userfaultfd_k.h> 17 18 struct ctl_table; 19 struct user_struct; 20 struct mmu_gather; 21 struct node; 22 23 void free_huge_folio(struct folio *folio); 24 25 #ifdef CONFIG_HUGETLB_PAGE 26 27 #include <linux/pagemap.h> 28 #include <linux/shm.h> 29 #include <asm/tlbflush.h> 30 31 /* 32 * For HugeTLB page, there are more metadata to save in the struct page. But 33 * the head struct page cannot meet our needs, so we have to abuse other tail 34 * struct page to store the metadata. 35 */ 36 #define __NR_USED_SUBPAGE 3 37 38 struct hugepage_subpool { 39 spinlock_t lock; 40 long count; 41 long max_hpages; /* Maximum huge pages or -1 if no maximum. */ 42 long used_hpages; /* Used count against maximum, includes */ 43 /* both allocated and reserved pages. */ 44 struct hstate *hstate; 45 long min_hpages; /* Minimum huge pages or -1 if no minimum. */ 46 long rsv_hpages; /* Pages reserved against global pool to */ 47 /* satisfy minimum size. */ 48 }; 49 50 struct resv_map { 51 struct kref refs; 52 spinlock_t lock; 53 struct list_head regions; 54 long adds_in_progress; 55 struct list_head region_cache; 56 long region_cache_count; 57 struct rw_semaphore rw_sema; 58 #ifdef CONFIG_CGROUP_HUGETLB 59 /* 60 * On private mappings, the counter to uncharge reservations is stored 61 * here. If these fields are 0, then either the mapping is shared, or 62 * cgroup accounting is disabled for this resv_map. 63 */ 64 struct page_counter *reservation_counter; 65 unsigned long pages_per_hpage; 66 struct cgroup_subsys_state *css; 67 #endif 68 }; 69 70 /* 71 * Region tracking -- allows tracking of reservations and instantiated pages 72 * across the pages in a mapping. 73 * 74 * The region data structures are embedded into a resv_map and protected 75 * by a resv_map's lock. The set of regions within the resv_map represent 76 * reservations for huge pages, or huge pages that have already been 77 * instantiated within the map. The from and to elements are huge page 78 * indices into the associated mapping. from indicates the starting index 79 * of the region. to represents the first index past the end of the region. 80 * 81 * For example, a file region structure with from == 0 and to == 4 represents 82 * four huge pages in a mapping. It is important to note that the to element 83 * represents the first element past the end of the region. This is used in 84 * arithmetic as 4(to) - 0(from) = 4 huge pages in the region. 85 * 86 * Interval notation of the form [from, to) will be used to indicate that 87 * the endpoint from is inclusive and to is exclusive. 88 */ 89 struct file_region { 90 struct list_head link; 91 long from; 92 long to; 93 #ifdef CONFIG_CGROUP_HUGETLB 94 /* 95 * On shared mappings, each reserved region appears as a struct 96 * file_region in resv_map. These fields hold the info needed to 97 * uncharge each reservation. 98 */ 99 struct page_counter *reservation_counter; 100 struct cgroup_subsys_state *css; 101 #endif 102 }; 103 104 struct hugetlb_vma_lock { 105 struct kref refs; 106 struct rw_semaphore rw_sema; 107 struct vm_area_struct *vma; 108 }; 109 110 extern struct resv_map *resv_map_alloc(void); 111 void resv_map_release(struct kref *ref); 112 113 extern spinlock_t hugetlb_lock; 114 extern int hugetlb_max_hstate __read_mostly; 115 #define for_each_hstate(h) \ 116 for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++) 117 118 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, 119 long min_hpages); 120 void hugepage_put_subpool(struct hugepage_subpool *spool); 121 122 void hugetlb_dup_vma_private(struct vm_area_struct *vma); 123 void clear_vma_resv_huge_pages(struct vm_area_struct *vma); 124 int move_hugetlb_page_tables(struct vm_area_struct *vma, 125 struct vm_area_struct *new_vma, 126 unsigned long old_addr, unsigned long new_addr, 127 unsigned long len); 128 int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, 129 struct vm_area_struct *, struct vm_area_struct *); 130 void unmap_hugepage_range(struct vm_area_struct *, 131 unsigned long, unsigned long, struct page *, 132 zap_flags_t); 133 void __unmap_hugepage_range(struct mmu_gather *tlb, 134 struct vm_area_struct *vma, 135 unsigned long start, unsigned long end, 136 struct page *ref_page, zap_flags_t zap_flags); 137 void hugetlb_report_meminfo(struct seq_file *); 138 int hugetlb_report_node_meminfo(char *buf, int len, int nid); 139 void hugetlb_show_meminfo_node(int nid); 140 unsigned long hugetlb_total_pages(void); 141 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, 142 unsigned long address, unsigned int flags); 143 #ifdef CONFIG_USERFAULTFD 144 int hugetlb_mfill_atomic_pte(pte_t *dst_pte, 145 struct vm_area_struct *dst_vma, 146 unsigned long dst_addr, 147 unsigned long src_addr, 148 uffd_flags_t flags, 149 struct folio **foliop); 150 #endif /* CONFIG_USERFAULTFD */ 151 bool hugetlb_reserve_pages(struct inode *inode, long from, long to, 152 struct vm_area_struct *vma, 153 vm_flags_t vm_flags); 154 long hugetlb_unreserve_pages(struct inode *inode, long start, long end, 155 long freed); 156 bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list); 157 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison); 158 int get_huge_page_for_hwpoison(unsigned long pfn, int flags, 159 bool *migratable_cleared); 160 void folio_putback_hugetlb(struct folio *folio); 161 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason); 162 void hugetlb_fix_reserve_counts(struct inode *inode); 163 extern struct mutex *hugetlb_fault_mutex_table; 164 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx); 165 166 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 167 unsigned long addr, pud_t *pud); 168 bool hugetlbfs_pagecache_present(struct hstate *h, 169 struct vm_area_struct *vma, 170 unsigned long address); 171 172 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio); 173 174 extern int sysctl_hugetlb_shm_group; 175 extern struct list_head huge_boot_pages[MAX_NUMNODES]; 176 177 /* arch callbacks */ 178 179 #ifndef CONFIG_HIGHPTE 180 /* 181 * pte_offset_huge() and pte_alloc_huge() are helpers for those architectures 182 * which may go down to the lowest PTE level in their huge_pte_offset() and 183 * huge_pte_alloc(): to avoid reliance on pte_offset_map() without pte_unmap(). 184 */ 185 static inline pte_t *pte_offset_huge(pmd_t *pmd, unsigned long address) 186 { 187 return pte_offset_kernel(pmd, address); 188 } 189 static inline pte_t *pte_alloc_huge(struct mm_struct *mm, pmd_t *pmd, 190 unsigned long address) 191 { 192 return pte_alloc(mm, pmd) ? NULL : pte_offset_huge(pmd, address); 193 } 194 #endif 195 196 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, 197 unsigned long addr, unsigned long sz); 198 /* 199 * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE. 200 * Returns the pte_t* if found, or NULL if the address is not mapped. 201 * 202 * IMPORTANT: we should normally not directly call this function, instead 203 * this is only a common interface to implement arch-specific 204 * walker. Please use hugetlb_walk() instead, because that will attempt to 205 * verify the locking for you. 206 * 207 * Since this function will walk all the pgtable pages (including not only 208 * high-level pgtable page, but also PUD entry that can be unshared 209 * concurrently for VM_SHARED), the caller of this function should be 210 * responsible of its thread safety. One can follow this rule: 211 * 212 * (1) For private mappings: pmd unsharing is not possible, so holding the 213 * mmap_lock for either read or write is sufficient. Most callers 214 * already hold the mmap_lock, so normally, no special action is 215 * required. 216 * 217 * (2) For shared mappings: pmd unsharing is possible (so the PUD-ranged 218 * pgtable page can go away from under us! It can be done by a pmd 219 * unshare with a follow up munmap() on the other process), then we 220 * need either: 221 * 222 * (2.1) hugetlb vma lock read or write held, to make sure pmd unshare 223 * won't happen upon the range (it also makes sure the pte_t we 224 * read is the right and stable one), or, 225 * 226 * (2.2) hugetlb mapping i_mmap_rwsem lock held read or write, to make 227 * sure even if unshare happened the racy unmap() will wait until 228 * i_mmap_rwsem is released. 229 * 230 * Option (2.1) is the safest, which guarantees pte stability from pmd 231 * sharing pov, until the vma lock released. Option (2.2) doesn't protect 232 * a concurrent pmd unshare, but it makes sure the pgtable page is safe to 233 * access. 234 */ 235 pte_t *huge_pte_offset(struct mm_struct *mm, 236 unsigned long addr, unsigned long sz); 237 unsigned long hugetlb_mask_last_page(struct hstate *h); 238 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, 239 unsigned long addr, pte_t *ptep); 240 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, 241 unsigned long *start, unsigned long *end); 242 243 extern void __hugetlb_zap_begin(struct vm_area_struct *vma, 244 unsigned long *begin, unsigned long *end); 245 extern void __hugetlb_zap_end(struct vm_area_struct *vma, 246 struct zap_details *details); 247 248 static inline void hugetlb_zap_begin(struct vm_area_struct *vma, 249 unsigned long *start, unsigned long *end) 250 { 251 if (is_vm_hugetlb_page(vma)) 252 __hugetlb_zap_begin(vma, start, end); 253 } 254 255 static inline void hugetlb_zap_end(struct vm_area_struct *vma, 256 struct zap_details *details) 257 { 258 if (is_vm_hugetlb_page(vma)) 259 __hugetlb_zap_end(vma, details); 260 } 261 262 void hugetlb_vma_lock_read(struct vm_area_struct *vma); 263 void hugetlb_vma_unlock_read(struct vm_area_struct *vma); 264 void hugetlb_vma_lock_write(struct vm_area_struct *vma); 265 void hugetlb_vma_unlock_write(struct vm_area_struct *vma); 266 int hugetlb_vma_trylock_write(struct vm_area_struct *vma); 267 void hugetlb_vma_assert_locked(struct vm_area_struct *vma); 268 void hugetlb_vma_lock_release(struct kref *kref); 269 long hugetlb_change_protection(struct vm_area_struct *vma, 270 unsigned long address, unsigned long end, pgprot_t newprot, 271 unsigned long cp_flags); 272 bool is_hugetlb_entry_migration(pte_t pte); 273 bool is_hugetlb_entry_hwpoisoned(pte_t pte); 274 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma); 275 276 #else /* !CONFIG_HUGETLB_PAGE */ 277 278 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma) 279 { 280 } 281 282 static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma) 283 { 284 } 285 286 static inline unsigned long hugetlb_total_pages(void) 287 { 288 return 0; 289 } 290 291 static inline struct address_space *hugetlb_folio_mapping_lock_write( 292 struct folio *folio) 293 { 294 return NULL; 295 } 296 297 static inline int huge_pmd_unshare(struct mm_struct *mm, 298 struct vm_area_struct *vma, 299 unsigned long addr, pte_t *ptep) 300 { 301 return 0; 302 } 303 304 static inline void adjust_range_if_pmd_sharing_possible( 305 struct vm_area_struct *vma, 306 unsigned long *start, unsigned long *end) 307 { 308 } 309 310 static inline void hugetlb_zap_begin( 311 struct vm_area_struct *vma, 312 unsigned long *start, unsigned long *end) 313 { 314 } 315 316 static inline void hugetlb_zap_end( 317 struct vm_area_struct *vma, 318 struct zap_details *details) 319 { 320 } 321 322 static inline int copy_hugetlb_page_range(struct mm_struct *dst, 323 struct mm_struct *src, 324 struct vm_area_struct *dst_vma, 325 struct vm_area_struct *src_vma) 326 { 327 BUG(); 328 return 0; 329 } 330 331 static inline int move_hugetlb_page_tables(struct vm_area_struct *vma, 332 struct vm_area_struct *new_vma, 333 unsigned long old_addr, 334 unsigned long new_addr, 335 unsigned long len) 336 { 337 BUG(); 338 return 0; 339 } 340 341 static inline void hugetlb_report_meminfo(struct seq_file *m) 342 { 343 } 344 345 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid) 346 { 347 return 0; 348 } 349 350 static inline void hugetlb_show_meminfo_node(int nid) 351 { 352 } 353 354 static inline int prepare_hugepage_range(struct file *file, 355 unsigned long addr, unsigned long len) 356 { 357 return -EINVAL; 358 } 359 360 static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma) 361 { 362 } 363 364 static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma) 365 { 366 } 367 368 static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma) 369 { 370 } 371 372 static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma) 373 { 374 } 375 376 static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma) 377 { 378 return 1; 379 } 380 381 static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma) 382 { 383 } 384 385 static inline int is_hugepage_only_range(struct mm_struct *mm, 386 unsigned long addr, unsigned long len) 387 { 388 return 0; 389 } 390 391 static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb, 392 unsigned long addr, unsigned long end, 393 unsigned long floor, unsigned long ceiling) 394 { 395 BUG(); 396 } 397 398 #ifdef CONFIG_USERFAULTFD 399 static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte, 400 struct vm_area_struct *dst_vma, 401 unsigned long dst_addr, 402 unsigned long src_addr, 403 uffd_flags_t flags, 404 struct folio **foliop) 405 { 406 BUG(); 407 return 0; 408 } 409 #endif /* CONFIG_USERFAULTFD */ 410 411 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, 412 unsigned long sz) 413 { 414 return NULL; 415 } 416 417 static inline bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list) 418 { 419 return false; 420 } 421 422 static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison) 423 { 424 return 0; 425 } 426 427 static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags, 428 bool *migratable_cleared) 429 { 430 return 0; 431 } 432 433 static inline void folio_putback_hugetlb(struct folio *folio) 434 { 435 } 436 437 static inline void move_hugetlb_state(struct folio *old_folio, 438 struct folio *new_folio, int reason) 439 { 440 } 441 442 static inline long hugetlb_change_protection( 443 struct vm_area_struct *vma, unsigned long address, 444 unsigned long end, pgprot_t newprot, 445 unsigned long cp_flags) 446 { 447 return 0; 448 } 449 450 static inline void __unmap_hugepage_range(struct mmu_gather *tlb, 451 struct vm_area_struct *vma, unsigned long start, 452 unsigned long end, struct page *ref_page, 453 zap_flags_t zap_flags) 454 { 455 BUG(); 456 } 457 458 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm, 459 struct vm_area_struct *vma, unsigned long address, 460 unsigned int flags) 461 { 462 BUG(); 463 return 0; 464 } 465 466 static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { } 467 468 #endif /* !CONFIG_HUGETLB_PAGE */ 469 470 #ifndef pgd_write 471 static inline int pgd_write(pgd_t pgd) 472 { 473 BUG(); 474 return 0; 475 } 476 #endif 477 478 #define HUGETLB_ANON_FILE "anon_hugepage" 479 480 enum { 481 /* 482 * The file will be used as an shm file so shmfs accounting rules 483 * apply 484 */ 485 HUGETLB_SHMFS_INODE = 1, 486 /* 487 * The file is being created on the internal vfs mount and shmfs 488 * accounting rules do not apply 489 */ 490 HUGETLB_ANONHUGE_INODE = 2, 491 }; 492 493 #ifdef CONFIG_HUGETLBFS 494 struct hugetlbfs_sb_info { 495 long max_inodes; /* inodes allowed */ 496 long free_inodes; /* inodes free */ 497 spinlock_t stat_lock; 498 struct hstate *hstate; 499 struct hugepage_subpool *spool; 500 kuid_t uid; 501 kgid_t gid; 502 umode_t mode; 503 }; 504 505 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb) 506 { 507 return sb->s_fs_info; 508 } 509 510 struct hugetlbfs_inode_info { 511 struct inode vfs_inode; 512 unsigned int seals; 513 }; 514 515 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode) 516 { 517 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode); 518 } 519 520 extern const struct vm_operations_struct hugetlb_vm_ops; 521 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct, 522 int creat_flags, int page_size_log); 523 524 static inline bool is_file_hugepages(const struct file *file) 525 { 526 return file->f_op->fop_flags & FOP_HUGE_PAGES; 527 } 528 529 static inline struct hstate *hstate_inode(struct inode *i) 530 { 531 return HUGETLBFS_SB(i->i_sb)->hstate; 532 } 533 #else /* !CONFIG_HUGETLBFS */ 534 535 #define is_file_hugepages(file) false 536 static inline struct file * 537 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag, 538 int creat_flags, int page_size_log) 539 { 540 return ERR_PTR(-ENOSYS); 541 } 542 543 static inline struct hstate *hstate_inode(struct inode *i) 544 { 545 return NULL; 546 } 547 #endif /* !CONFIG_HUGETLBFS */ 548 549 unsigned long 550 hugetlb_get_unmapped_area(struct file *file, unsigned long addr, 551 unsigned long len, unsigned long pgoff, 552 unsigned long flags); 553 554 /* 555 * huegtlb page specific state flags. These flags are located in page.private 556 * of the hugetlb head page. Functions created via the below macros should be 557 * used to manipulate these flags. 558 * 559 * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at 560 * allocation time. Cleared when page is fully instantiated. Free 561 * routine checks flag to restore a reservation on error paths. 562 * Synchronization: Examined or modified by code that knows it has 563 * the only reference to page. i.e. After allocation but before use 564 * or when the page is being freed. 565 * HPG_migratable - Set after a newly allocated page is added to the page 566 * cache and/or page tables. Indicates the page is a candidate for 567 * migration. 568 * Synchronization: Initially set after new page allocation with no 569 * locking. When examined and modified during migration processing 570 * (isolate, migrate, putback) the hugetlb_lock is held. 571 * HPG_temporary - Set on a page that is temporarily allocated from the buddy 572 * allocator. Typically used for migration target pages when no pages 573 * are available in the pool. The hugetlb free page path will 574 * immediately free pages with this flag set to the buddy allocator. 575 * Synchronization: Can be set after huge page allocation from buddy when 576 * code knows it has only reference. All other examinations and 577 * modifications require hugetlb_lock. 578 * HPG_freed - Set when page is on the free lists. 579 * Synchronization: hugetlb_lock held for examination and modification. 580 * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed. 581 * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page 582 * that is not tracked by raw_hwp_page list. 583 */ 584 enum hugetlb_page_flags { 585 HPG_restore_reserve = 0, 586 HPG_migratable, 587 HPG_temporary, 588 HPG_freed, 589 HPG_vmemmap_optimized, 590 HPG_raw_hwp_unreliable, 591 __NR_HPAGEFLAGS, 592 }; 593 594 /* 595 * Macros to create test, set and clear function definitions for 596 * hugetlb specific page flags. 597 */ 598 #ifdef CONFIG_HUGETLB_PAGE 599 #define TESTHPAGEFLAG(uname, flname) \ 600 static __always_inline \ 601 bool folio_test_hugetlb_##flname(struct folio *folio) \ 602 { void *private = &folio->private; \ 603 return test_bit(HPG_##flname, private); \ 604 } 605 606 #define SETHPAGEFLAG(uname, flname) \ 607 static __always_inline \ 608 void folio_set_hugetlb_##flname(struct folio *folio) \ 609 { void *private = &folio->private; \ 610 set_bit(HPG_##flname, private); \ 611 } 612 613 #define CLEARHPAGEFLAG(uname, flname) \ 614 static __always_inline \ 615 void folio_clear_hugetlb_##flname(struct folio *folio) \ 616 { void *private = &folio->private; \ 617 clear_bit(HPG_##flname, private); \ 618 } 619 #else 620 #define TESTHPAGEFLAG(uname, flname) \ 621 static inline bool \ 622 folio_test_hugetlb_##flname(struct folio *folio) \ 623 { return 0; } 624 625 #define SETHPAGEFLAG(uname, flname) \ 626 static inline void \ 627 folio_set_hugetlb_##flname(struct folio *folio) \ 628 { } 629 630 #define CLEARHPAGEFLAG(uname, flname) \ 631 static inline void \ 632 folio_clear_hugetlb_##flname(struct folio *folio) \ 633 { } 634 #endif 635 636 #define HPAGEFLAG(uname, flname) \ 637 TESTHPAGEFLAG(uname, flname) \ 638 SETHPAGEFLAG(uname, flname) \ 639 CLEARHPAGEFLAG(uname, flname) \ 640 641 /* 642 * Create functions associated with hugetlb page flags 643 */ 644 HPAGEFLAG(RestoreReserve, restore_reserve) 645 HPAGEFLAG(Migratable, migratable) 646 HPAGEFLAG(Temporary, temporary) 647 HPAGEFLAG(Freed, freed) 648 HPAGEFLAG(VmemmapOptimized, vmemmap_optimized) 649 HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable) 650 651 #ifdef CONFIG_HUGETLB_PAGE 652 653 #define HSTATE_NAME_LEN 32 654 /* Defines one hugetlb page size */ 655 struct hstate { 656 struct mutex resize_lock; 657 struct lock_class_key resize_key; 658 int next_nid_to_alloc; 659 int next_nid_to_free; 660 unsigned int order; 661 unsigned int demote_order; 662 unsigned long mask; 663 unsigned long max_huge_pages; 664 unsigned long nr_huge_pages; 665 unsigned long free_huge_pages; 666 unsigned long resv_huge_pages; 667 unsigned long surplus_huge_pages; 668 unsigned long nr_overcommit_huge_pages; 669 struct list_head hugepage_activelist; 670 struct list_head hugepage_freelists[MAX_NUMNODES]; 671 unsigned int max_huge_pages_node[MAX_NUMNODES]; 672 unsigned int nr_huge_pages_node[MAX_NUMNODES]; 673 unsigned int free_huge_pages_node[MAX_NUMNODES]; 674 unsigned int surplus_huge_pages_node[MAX_NUMNODES]; 675 char name[HSTATE_NAME_LEN]; 676 }; 677 678 struct huge_bootmem_page { 679 struct list_head list; 680 struct hstate *hstate; 681 }; 682 683 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list); 684 int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn); 685 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, 686 unsigned long addr, bool cow_from_owner); 687 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, 688 nodemask_t *nmask, gfp_t gfp_mask, 689 bool allow_alloc_fallback); 690 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, 691 nodemask_t *nmask, gfp_t gfp_mask); 692 693 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping, 694 pgoff_t idx); 695 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, 696 unsigned long address, struct folio *folio); 697 698 /* arch callback */ 699 int __init __alloc_bootmem_huge_page(struct hstate *h, int nid); 700 int __init alloc_bootmem_huge_page(struct hstate *h, int nid); 701 bool __init hugetlb_node_alloc_supported(void); 702 703 void __init hugetlb_add_hstate(unsigned order); 704 bool __init arch_hugetlb_valid_size(unsigned long size); 705 struct hstate *size_to_hstate(unsigned long size); 706 707 #ifndef HUGE_MAX_HSTATE 708 #define HUGE_MAX_HSTATE 1 709 #endif 710 711 extern struct hstate hstates[HUGE_MAX_HSTATE]; 712 extern unsigned int default_hstate_idx; 713 714 #define default_hstate (hstates[default_hstate_idx]) 715 716 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio) 717 { 718 return folio->_hugetlb_subpool; 719 } 720 721 static inline void hugetlb_set_folio_subpool(struct folio *folio, 722 struct hugepage_subpool *subpool) 723 { 724 folio->_hugetlb_subpool = subpool; 725 } 726 727 static inline struct hstate *hstate_file(struct file *f) 728 { 729 return hstate_inode(file_inode(f)); 730 } 731 732 static inline struct hstate *hstate_sizelog(int page_size_log) 733 { 734 if (!page_size_log) 735 return &default_hstate; 736 737 if (page_size_log < BITS_PER_LONG) 738 return size_to_hstate(1UL << page_size_log); 739 740 return NULL; 741 } 742 743 static inline struct hstate *hstate_vma(struct vm_area_struct *vma) 744 { 745 return hstate_file(vma->vm_file); 746 } 747 748 static inline unsigned long huge_page_size(const struct hstate *h) 749 { 750 return (unsigned long)PAGE_SIZE << h->order; 751 } 752 753 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma); 754 755 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma); 756 757 static inline unsigned long huge_page_mask(struct hstate *h) 758 { 759 return h->mask; 760 } 761 762 static inline unsigned int huge_page_order(struct hstate *h) 763 { 764 return h->order; 765 } 766 767 static inline unsigned huge_page_shift(struct hstate *h) 768 { 769 return h->order + PAGE_SHIFT; 770 } 771 772 static inline bool hstate_is_gigantic(struct hstate *h) 773 { 774 return huge_page_order(h) > MAX_PAGE_ORDER; 775 } 776 777 static inline unsigned int pages_per_huge_page(const struct hstate *h) 778 { 779 return 1 << h->order; 780 } 781 782 static inline unsigned int blocks_per_huge_page(struct hstate *h) 783 { 784 return huge_page_size(h) / 512; 785 } 786 787 static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, 788 struct address_space *mapping, pgoff_t idx) 789 { 790 return filemap_lock_folio(mapping, idx << huge_page_order(h)); 791 } 792 793 #include <asm/hugetlb.h> 794 795 #ifndef is_hugepage_only_range 796 static inline int is_hugepage_only_range(struct mm_struct *mm, 797 unsigned long addr, unsigned long len) 798 { 799 return 0; 800 } 801 #define is_hugepage_only_range is_hugepage_only_range 802 #endif 803 804 #ifndef arch_clear_hugetlb_flags 805 static inline void arch_clear_hugetlb_flags(struct folio *folio) { } 806 #define arch_clear_hugetlb_flags arch_clear_hugetlb_flags 807 #endif 808 809 #ifndef arch_make_huge_pte 810 static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, 811 vm_flags_t flags) 812 { 813 return pte_mkhuge(entry); 814 } 815 #endif 816 817 static inline struct hstate *folio_hstate(struct folio *folio) 818 { 819 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio); 820 return size_to_hstate(folio_size(folio)); 821 } 822 823 static inline unsigned hstate_index_to_shift(unsigned index) 824 { 825 return hstates[index].order + PAGE_SHIFT; 826 } 827 828 static inline int hstate_index(struct hstate *h) 829 { 830 return h - hstates; 831 } 832 833 int dissolve_free_hugetlb_folio(struct folio *folio); 834 int dissolve_free_hugetlb_folios(unsigned long start_pfn, 835 unsigned long end_pfn); 836 837 #ifdef CONFIG_MEMORY_FAILURE 838 extern void folio_clear_hugetlb_hwpoison(struct folio *folio); 839 #else 840 static inline void folio_clear_hugetlb_hwpoison(struct folio *folio) 841 { 842 } 843 #endif 844 845 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION 846 #ifndef arch_hugetlb_migration_supported 847 static inline bool arch_hugetlb_migration_supported(struct hstate *h) 848 { 849 if ((huge_page_shift(h) == PMD_SHIFT) || 850 (huge_page_shift(h) == PUD_SHIFT) || 851 (huge_page_shift(h) == PGDIR_SHIFT)) 852 return true; 853 else 854 return false; 855 } 856 #endif 857 #else 858 static inline bool arch_hugetlb_migration_supported(struct hstate *h) 859 { 860 return false; 861 } 862 #endif 863 864 static inline bool hugepage_migration_supported(struct hstate *h) 865 { 866 return arch_hugetlb_migration_supported(h); 867 } 868 869 /* 870 * Movability check is different as compared to migration check. 871 * It determines whether or not a huge page should be placed on 872 * movable zone or not. Movability of any huge page should be 873 * required only if huge page size is supported for migration. 874 * There won't be any reason for the huge page to be movable if 875 * it is not migratable to start with. Also the size of the huge 876 * page should be large enough to be placed under a movable zone 877 * and still feasible enough to be migratable. Just the presence 878 * in movable zone does not make the migration feasible. 879 * 880 * So even though large huge page sizes like the gigantic ones 881 * are migratable they should not be movable because its not 882 * feasible to migrate them from movable zone. 883 */ 884 static inline bool hugepage_movable_supported(struct hstate *h) 885 { 886 if (!hugepage_migration_supported(h)) 887 return false; 888 889 if (hstate_is_gigantic(h)) 890 return false; 891 return true; 892 } 893 894 /* Movability of hugepages depends on migration support. */ 895 static inline gfp_t htlb_alloc_mask(struct hstate *h) 896 { 897 gfp_t gfp = __GFP_COMP | __GFP_NOWARN; 898 899 gfp |= hugepage_movable_supported(h) ? GFP_HIGHUSER_MOVABLE : GFP_HIGHUSER; 900 901 return gfp; 902 } 903 904 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) 905 { 906 gfp_t modified_mask = htlb_alloc_mask(h); 907 908 /* Some callers might want to enforce node */ 909 modified_mask |= (gfp_mask & __GFP_THISNODE); 910 911 modified_mask |= (gfp_mask & __GFP_NOWARN); 912 913 return modified_mask; 914 } 915 916 static inline bool htlb_allow_alloc_fallback(int reason) 917 { 918 bool allowed_fallback = false; 919 920 /* 921 * Note: the memory offline, memory failure and migration syscalls will 922 * be allowed to fallback to other nodes due to lack of a better chioce, 923 * that might break the per-node hugetlb pool. While other cases will 924 * set the __GFP_THISNODE to avoid breaking the per-node hugetlb pool. 925 */ 926 switch (reason) { 927 case MR_MEMORY_HOTPLUG: 928 case MR_MEMORY_FAILURE: 929 case MR_SYSCALL: 930 case MR_MEMPOLICY_MBIND: 931 allowed_fallback = true; 932 break; 933 default: 934 break; 935 } 936 937 return allowed_fallback; 938 } 939 940 static inline spinlock_t *huge_pte_lockptr(struct hstate *h, 941 struct mm_struct *mm, pte_t *pte) 942 { 943 const unsigned long size = huge_page_size(h); 944 945 VM_WARN_ON(size == PAGE_SIZE); 946 947 /* 948 * hugetlb must use the exact same PT locks as core-mm page table 949 * walkers would. When modifying a PTE table, hugetlb must take the 950 * PTE PT lock, when modifying a PMD table, hugetlb must take the PMD 951 * PT lock etc. 952 * 953 * The expectation is that any hugetlb folio smaller than a PMD is 954 * always mapped into a single PTE table and that any hugetlb folio 955 * smaller than a PUD (but at least as big as a PMD) is always mapped 956 * into a single PMD table. 957 * 958 * If that does not hold for an architecture, then that architecture 959 * must disable split PT locks such that all *_lockptr() functions 960 * will give us the same result: the per-MM PT lock. 961 * 962 * Note that with e.g., CONFIG_PGTABLE_LEVELS=2 where 963 * PGDIR_SIZE==P4D_SIZE==PUD_SIZE==PMD_SIZE, we'd use pud_lockptr() 964 * and core-mm would use pmd_lockptr(). However, in such configurations 965 * split PMD locks are disabled -- they don't make sense on a single 966 * PGDIR page table -- and the end result is the same. 967 */ 968 if (size >= PUD_SIZE) 969 return pud_lockptr(mm, (pud_t *) pte); 970 else if (size >= PMD_SIZE || IS_ENABLED(CONFIG_HIGHPTE)) 971 return pmd_lockptr(mm, (pmd_t *) pte); 972 /* pte_alloc_huge() only applies with !CONFIG_HIGHPTE */ 973 return ptep_lockptr(mm, pte); 974 } 975 976 #ifndef hugepages_supported 977 /* 978 * Some platform decide whether they support huge pages at boot 979 * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0 980 * when there is no such support 981 */ 982 #define hugepages_supported() (HPAGE_SHIFT != 0) 983 #endif 984 985 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm); 986 987 static inline void hugetlb_count_init(struct mm_struct *mm) 988 { 989 atomic_long_set(&mm->hugetlb_usage, 0); 990 } 991 992 static inline void hugetlb_count_add(long l, struct mm_struct *mm) 993 { 994 atomic_long_add(l, &mm->hugetlb_usage); 995 } 996 997 static inline void hugetlb_count_sub(long l, struct mm_struct *mm) 998 { 999 atomic_long_sub(l, &mm->hugetlb_usage); 1000 } 1001 1002 #ifndef huge_ptep_modify_prot_start 1003 #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start 1004 static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, 1005 unsigned long addr, pte_t *ptep) 1006 { 1007 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep); 1008 } 1009 #endif 1010 1011 #ifndef huge_ptep_modify_prot_commit 1012 #define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit 1013 static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, 1014 unsigned long addr, pte_t *ptep, 1015 pte_t old_pte, pte_t pte) 1016 { 1017 unsigned long psize = huge_page_size(hstate_vma(vma)); 1018 1019 set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize); 1020 } 1021 #endif 1022 1023 #ifdef CONFIG_NUMA 1024 void hugetlb_register_node(struct node *node); 1025 void hugetlb_unregister_node(struct node *node); 1026 #endif 1027 1028 /* 1029 * Check if a given raw @page in a hugepage is HWPOISON. 1030 */ 1031 bool is_raw_hwpoison_page_in_hugepage(struct page *page); 1032 1033 static inline unsigned long huge_page_mask_align(struct file *file) 1034 { 1035 return PAGE_MASK & ~huge_page_mask(hstate_file(file)); 1036 } 1037 1038 #else /* CONFIG_HUGETLB_PAGE */ 1039 struct hstate {}; 1040 1041 static inline unsigned long huge_page_mask_align(struct file *file) 1042 { 1043 return 0; 1044 } 1045 1046 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio) 1047 { 1048 return NULL; 1049 } 1050 1051 static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h, 1052 struct address_space *mapping, pgoff_t idx) 1053 { 1054 return NULL; 1055 } 1056 1057 static inline int isolate_or_dissolve_huge_page(struct page *page, 1058 struct list_head *list) 1059 { 1060 return -ENOMEM; 1061 } 1062 1063 static inline int replace_free_hugepage_folios(unsigned long start_pfn, 1064 unsigned long end_pfn) 1065 { 1066 return 0; 1067 } 1068 1069 static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, 1070 unsigned long addr, 1071 bool cow_from_owner) 1072 { 1073 return NULL; 1074 } 1075 1076 static inline struct folio * 1077 alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid, 1078 nodemask_t *nmask, gfp_t gfp_mask) 1079 { 1080 return NULL; 1081 } 1082 1083 static inline struct folio * 1084 alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, 1085 nodemask_t *nmask, gfp_t gfp_mask, 1086 bool allow_alloc_fallback) 1087 { 1088 return NULL; 1089 } 1090 1091 static inline int __alloc_bootmem_huge_page(struct hstate *h) 1092 { 1093 return 0; 1094 } 1095 1096 static inline struct hstate *hstate_file(struct file *f) 1097 { 1098 return NULL; 1099 } 1100 1101 static inline struct hstate *hstate_sizelog(int page_size_log) 1102 { 1103 return NULL; 1104 } 1105 1106 static inline struct hstate *hstate_vma(struct vm_area_struct *vma) 1107 { 1108 return NULL; 1109 } 1110 1111 static inline struct hstate *folio_hstate(struct folio *folio) 1112 { 1113 return NULL; 1114 } 1115 1116 static inline struct hstate *size_to_hstate(unsigned long size) 1117 { 1118 return NULL; 1119 } 1120 1121 static inline unsigned long huge_page_size(struct hstate *h) 1122 { 1123 return PAGE_SIZE; 1124 } 1125 1126 static inline unsigned long huge_page_mask(struct hstate *h) 1127 { 1128 return PAGE_MASK; 1129 } 1130 1131 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) 1132 { 1133 return PAGE_SIZE; 1134 } 1135 1136 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) 1137 { 1138 return PAGE_SIZE; 1139 } 1140 1141 static inline unsigned int huge_page_order(struct hstate *h) 1142 { 1143 return 0; 1144 } 1145 1146 static inline unsigned int huge_page_shift(struct hstate *h) 1147 { 1148 return PAGE_SHIFT; 1149 } 1150 1151 static inline bool hstate_is_gigantic(struct hstate *h) 1152 { 1153 return false; 1154 } 1155 1156 static inline unsigned int pages_per_huge_page(struct hstate *h) 1157 { 1158 return 1; 1159 } 1160 1161 static inline unsigned hstate_index_to_shift(unsigned index) 1162 { 1163 return 0; 1164 } 1165 1166 static inline int hstate_index(struct hstate *h) 1167 { 1168 return 0; 1169 } 1170 1171 static inline int dissolve_free_hugetlb_folio(struct folio *folio) 1172 { 1173 return 0; 1174 } 1175 1176 static inline int dissolve_free_hugetlb_folios(unsigned long start_pfn, 1177 unsigned long end_pfn) 1178 { 1179 return 0; 1180 } 1181 1182 static inline bool hugepage_migration_supported(struct hstate *h) 1183 { 1184 return false; 1185 } 1186 1187 static inline bool hugepage_movable_supported(struct hstate *h) 1188 { 1189 return false; 1190 } 1191 1192 static inline gfp_t htlb_alloc_mask(struct hstate *h) 1193 { 1194 return 0; 1195 } 1196 1197 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) 1198 { 1199 return 0; 1200 } 1201 1202 static inline bool htlb_allow_alloc_fallback(int reason) 1203 { 1204 return false; 1205 } 1206 1207 static inline spinlock_t *huge_pte_lockptr(struct hstate *h, 1208 struct mm_struct *mm, pte_t *pte) 1209 { 1210 return &mm->page_table_lock; 1211 } 1212 1213 static inline void hugetlb_count_init(struct mm_struct *mm) 1214 { 1215 } 1216 1217 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m) 1218 { 1219 } 1220 1221 static inline void hugetlb_count_sub(long l, struct mm_struct *mm) 1222 { 1223 } 1224 1225 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, 1226 unsigned long addr, pte_t *ptep) 1227 { 1228 #ifdef CONFIG_MMU 1229 return ptep_get(ptep); 1230 #else 1231 return *ptep; 1232 #endif 1233 } 1234 1235 static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, 1236 pte_t *ptep, pte_t pte, unsigned long sz) 1237 { 1238 } 1239 1240 static inline void hugetlb_register_node(struct node *node) 1241 { 1242 } 1243 1244 static inline void hugetlb_unregister_node(struct node *node) 1245 { 1246 } 1247 1248 static inline bool hugetlbfs_pagecache_present( 1249 struct hstate *h, struct vm_area_struct *vma, unsigned long address) 1250 { 1251 return false; 1252 } 1253 #endif /* CONFIG_HUGETLB_PAGE */ 1254 1255 static inline spinlock_t *huge_pte_lock(struct hstate *h, 1256 struct mm_struct *mm, pte_t *pte) 1257 { 1258 spinlock_t *ptl; 1259 1260 ptl = huge_pte_lockptr(h, mm, pte); 1261 spin_lock(ptl); 1262 return ptl; 1263 } 1264 1265 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA) 1266 extern void __init hugetlb_cma_reserve(int order); 1267 #else 1268 static inline __init void hugetlb_cma_reserve(int order) 1269 { 1270 } 1271 #endif 1272 1273 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING 1274 static inline bool hugetlb_pmd_shared(pte_t *pte) 1275 { 1276 return page_count(virt_to_page(pte)) > 1; 1277 } 1278 #else 1279 static inline bool hugetlb_pmd_shared(pte_t *pte) 1280 { 1281 return false; 1282 } 1283 #endif 1284 1285 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr); 1286 1287 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE 1288 /* 1289 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can 1290 * implement this. 1291 */ 1292 #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end) 1293 #endif 1294 1295 static inline bool __vma_shareable_lock(struct vm_area_struct *vma) 1296 { 1297 return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data; 1298 } 1299 1300 bool __vma_private_lock(struct vm_area_struct *vma); 1301 1302 /* 1303 * Safe version of huge_pte_offset() to check the locks. See comments 1304 * above huge_pte_offset(). 1305 */ 1306 static inline pte_t * 1307 hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz) 1308 { 1309 #if defined(CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING) && defined(CONFIG_LOCKDEP) 1310 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; 1311 1312 /* 1313 * If pmd sharing possible, locking needed to safely walk the 1314 * hugetlb pgtables. More information can be found at the comment 1315 * above huge_pte_offset() in the same file. 1316 * 1317 * NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP. 1318 */ 1319 if (__vma_shareable_lock(vma)) 1320 WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) && 1321 !lockdep_is_held( 1322 &vma->vm_file->f_mapping->i_mmap_rwsem)); 1323 #endif 1324 return huge_pte_offset(vma->vm_mm, addr, sz); 1325 } 1326 1327 #endif /* _LINUX_HUGETLB_H */ 1328