1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_HUGE_MM_H 3 #define _LINUX_HUGE_MM_H 4 5 #include <linux/sched/coredump.h> 6 #include <linux/mm_types.h> 7 8 #include <linux/fs.h> /* only for vma_is_dax() */ 9 10 extern vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf); 11 extern int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, 12 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, 13 struct vm_area_struct *vma); 14 extern void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd); 15 extern int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, 16 pud_t *dst_pud, pud_t *src_pud, unsigned long addr, 17 struct vm_area_struct *vma); 18 19 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 20 extern void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud); 21 #else 22 static inline void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) 23 { 24 } 25 #endif 26 27 extern vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd); 28 extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, 29 unsigned long addr, 30 pmd_t *pmd, 31 unsigned int flags); 32 extern bool madvise_free_huge_pmd(struct mmu_gather *tlb, 33 struct vm_area_struct *vma, 34 pmd_t *pmd, unsigned long addr, unsigned long next); 35 extern int zap_huge_pmd(struct mmu_gather *tlb, 36 struct vm_area_struct *vma, 37 pmd_t *pmd, unsigned long addr); 38 extern int zap_huge_pud(struct mmu_gather *tlb, 39 struct vm_area_struct *vma, 40 pud_t *pud, unsigned long addr); 41 extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, 42 unsigned long addr, unsigned long end, 43 unsigned char *vec); 44 extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, 45 unsigned long new_addr, unsigned long old_end, 46 pmd_t *old_pmd, pmd_t *new_pmd); 47 extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, 48 unsigned long addr, pgprot_t newprot, 49 int prot_numa); 50 vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write); 51 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write); 52 enum transparent_hugepage_flag { 53 TRANSPARENT_HUGEPAGE_FLAG, 54 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 55 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, 56 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, 57 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, 58 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, 59 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG, 60 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG, 61 #ifdef CONFIG_DEBUG_VM 62 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG, 63 #endif 64 }; 65 66 struct kobject; 67 struct kobj_attribute; 68 69 extern ssize_t single_hugepage_flag_store(struct kobject *kobj, 70 struct kobj_attribute *attr, 71 const char *buf, size_t count, 72 enum transparent_hugepage_flag flag); 73 extern ssize_t single_hugepage_flag_show(struct kobject *kobj, 74 struct kobj_attribute *attr, char *buf, 75 enum transparent_hugepage_flag flag); 76 extern struct kobj_attribute shmem_enabled_attr; 77 78 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT) 79 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER) 80 81 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 82 #define HPAGE_PMD_SHIFT PMD_SHIFT 83 #define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT) 84 #define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1)) 85 86 #define HPAGE_PUD_SHIFT PUD_SHIFT 87 #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT) 88 #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1)) 89 90 extern unsigned long transparent_hugepage_flags; 91 92 /* 93 * to be used on vmas which are known to support THP. 94 * Use transparent_hugepage_enabled otherwise 95 */ 96 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma) 97 { 98 if (vma->vm_flags & VM_NOHUGEPAGE) 99 return false; 100 101 if (vma_is_temporary_stack(vma)) 102 return false; 103 104 if (test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 105 return false; 106 107 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG)) 108 return true; 109 /* 110 * For dax vmas, try to always use hugepage mappings. If the kernel does 111 * not support hugepages, fsdax mappings will fallback to PAGE_SIZE 112 * mappings, and device-dax namespaces, that try to guarantee a given 113 * mapping size, will fail to enable 114 */ 115 if (vma_is_dax(vma)) 116 return true; 117 118 if (transparent_hugepage_flags & 119 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)) 120 return !!(vma->vm_flags & VM_HUGEPAGE); 121 122 return false; 123 } 124 125 bool transparent_hugepage_enabled(struct vm_area_struct *vma); 126 127 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1) 128 129 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 130 unsigned long haddr) 131 { 132 /* Don't have to check pgoff for anonymous vma */ 133 if (!vma_is_anonymous(vma)) { 134 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) != 135 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK)) 136 return false; 137 } 138 139 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) 140 return false; 141 return true; 142 } 143 144 #define transparent_hugepage_use_zero_page() \ 145 (transparent_hugepage_flags & \ 146 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG)) 147 #ifdef CONFIG_DEBUG_VM 148 #define transparent_hugepage_debug_cow() \ 149 (transparent_hugepage_flags & \ 150 (1<<TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG)) 151 #else /* CONFIG_DEBUG_VM */ 152 #define transparent_hugepage_debug_cow() 0 153 #endif /* CONFIG_DEBUG_VM */ 154 155 extern unsigned long thp_get_unmapped_area(struct file *filp, 156 unsigned long addr, unsigned long len, unsigned long pgoff, 157 unsigned long flags); 158 159 extern void prep_transhuge_page(struct page *page); 160 extern void free_transhuge_page(struct page *page); 161 bool is_transparent_hugepage(struct page *page); 162 163 bool can_split_huge_page(struct page *page, int *pextra_pins); 164 int split_huge_page_to_list(struct page *page, struct list_head *list); 165 static inline int split_huge_page(struct page *page) 166 { 167 return split_huge_page_to_list(page, NULL); 168 } 169 void deferred_split_huge_page(struct page *page); 170 171 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, 172 unsigned long address, bool freeze, struct page *page); 173 174 #define split_huge_pmd(__vma, __pmd, __address) \ 175 do { \ 176 pmd_t *____pmd = (__pmd); \ 177 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \ 178 || pmd_devmap(*____pmd)) \ 179 __split_huge_pmd(__vma, __pmd, __address, \ 180 false, NULL); \ 181 } while (0) 182 183 184 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, 185 bool freeze, struct page *page); 186 187 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, 188 unsigned long address); 189 190 #define split_huge_pud(__vma, __pud, __address) \ 191 do { \ 192 pud_t *____pud = (__pud); \ 193 if (pud_trans_huge(*____pud) \ 194 || pud_devmap(*____pud)) \ 195 __split_huge_pud(__vma, __pud, __address); \ 196 } while (0) 197 198 extern int hugepage_madvise(struct vm_area_struct *vma, 199 unsigned long *vm_flags, int advice); 200 extern void vma_adjust_trans_huge(struct vm_area_struct *vma, 201 unsigned long start, 202 unsigned long end, 203 long adjust_next); 204 extern spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, 205 struct vm_area_struct *vma); 206 extern spinlock_t *__pud_trans_huge_lock(pud_t *pud, 207 struct vm_area_struct *vma); 208 209 static inline int is_swap_pmd(pmd_t pmd) 210 { 211 return !pmd_none(pmd) && !pmd_present(pmd); 212 } 213 214 /* mmap_sem must be held on entry */ 215 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd, 216 struct vm_area_struct *vma) 217 { 218 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) 219 return __pmd_trans_huge_lock(pmd, vma); 220 else 221 return NULL; 222 } 223 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud, 224 struct vm_area_struct *vma) 225 { 226 if (pud_trans_huge(*pud) || pud_devmap(*pud)) 227 return __pud_trans_huge_lock(pud, vma); 228 else 229 return NULL; 230 } 231 static inline int hpage_nr_pages(struct page *page) 232 { 233 if (unlikely(PageTransHuge(page))) 234 return HPAGE_PMD_NR; 235 return 1; 236 } 237 238 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr, 239 pmd_t *pmd, int flags, struct dev_pagemap **pgmap); 240 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr, 241 pud_t *pud, int flags, struct dev_pagemap **pgmap); 242 243 extern vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t orig_pmd); 244 245 extern struct page *huge_zero_page; 246 247 static inline bool is_huge_zero_page(struct page *page) 248 { 249 return READ_ONCE(huge_zero_page) == page; 250 } 251 252 static inline bool is_huge_zero_pmd(pmd_t pmd) 253 { 254 return is_huge_zero_page(pmd_page(pmd)); 255 } 256 257 static inline bool is_huge_zero_pud(pud_t pud) 258 { 259 return false; 260 } 261 262 struct page *mm_get_huge_zero_page(struct mm_struct *mm); 263 void mm_put_huge_zero_page(struct mm_struct *mm); 264 265 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot)) 266 267 static inline bool thp_migration_supported(void) 268 { 269 return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); 270 } 271 272 static inline struct list_head *page_deferred_list(struct page *page) 273 { 274 /* 275 * Global or memcg deferred list in the second tail pages is 276 * occupied by compound_head. 277 */ 278 return &page[2].deferred_list; 279 } 280 281 #else /* CONFIG_TRANSPARENT_HUGEPAGE */ 282 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; }) 283 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; }) 284 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; }) 285 286 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; }) 287 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; }) 288 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; }) 289 290 static inline int hpage_nr_pages(struct page *page) 291 { 292 VM_BUG_ON_PAGE(PageTail(page), page); 293 return 1; 294 } 295 296 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma) 297 { 298 return false; 299 } 300 301 static inline bool transparent_hugepage_enabled(struct vm_area_struct *vma) 302 { 303 return false; 304 } 305 306 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, 307 unsigned long haddr) 308 { 309 return false; 310 } 311 312 static inline void prep_transhuge_page(struct page *page) {} 313 314 static inline bool is_transparent_hugepage(struct page *page) 315 { 316 return false; 317 } 318 319 #define transparent_hugepage_flags 0UL 320 321 #define thp_get_unmapped_area NULL 322 323 static inline bool 324 can_split_huge_page(struct page *page, int *pextra_pins) 325 { 326 BUILD_BUG(); 327 return false; 328 } 329 static inline int 330 split_huge_page_to_list(struct page *page, struct list_head *list) 331 { 332 return 0; 333 } 334 static inline int split_huge_page(struct page *page) 335 { 336 return 0; 337 } 338 static inline void deferred_split_huge_page(struct page *page) {} 339 #define split_huge_pmd(__vma, __pmd, __address) \ 340 do { } while (0) 341 342 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, 343 unsigned long address, bool freeze, struct page *page) {} 344 static inline void split_huge_pmd_address(struct vm_area_struct *vma, 345 unsigned long address, bool freeze, struct page *page) {} 346 347 #define split_huge_pud(__vma, __pmd, __address) \ 348 do { } while (0) 349 350 static inline int hugepage_madvise(struct vm_area_struct *vma, 351 unsigned long *vm_flags, int advice) 352 { 353 BUG(); 354 return 0; 355 } 356 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma, 357 unsigned long start, 358 unsigned long end, 359 long adjust_next) 360 { 361 } 362 static inline int is_swap_pmd(pmd_t pmd) 363 { 364 return 0; 365 } 366 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd, 367 struct vm_area_struct *vma) 368 { 369 return NULL; 370 } 371 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud, 372 struct vm_area_struct *vma) 373 { 374 return NULL; 375 } 376 377 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, 378 pmd_t orig_pmd) 379 { 380 return 0; 381 } 382 383 static inline bool is_huge_zero_page(struct page *page) 384 { 385 return false; 386 } 387 388 static inline bool is_huge_zero_pud(pud_t pud) 389 { 390 return false; 391 } 392 393 static inline void mm_put_huge_zero_page(struct mm_struct *mm) 394 { 395 return; 396 } 397 398 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma, 399 unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap) 400 { 401 return NULL; 402 } 403 404 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma, 405 unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap) 406 { 407 return NULL; 408 } 409 410 static inline bool thp_migration_supported(void) 411 { 412 return false; 413 } 414 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 415 416 #endif /* _LINUX_HUGE_MM_H */ 417