at commit 89c2c777
| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_PGTABLE_H |
| 3 | #define _LINUX_PGTABLE_H |
| 4 | |
| 5 | #include <linux/pfn.h> |
| 6 | #include <asm/pgtable.h> |
| 7 | |
| 8 | #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) |
| 9 | #define PUD_ORDER (PUD_SHIFT - PAGE_SHIFT) |
| 10 | |
| 11 | #ifndef __ASSEMBLER__ |
| 12 | #ifdef CONFIG_MMU |
| 13 | |
| 14 | #include <linux/mm_types.h> |
| 15 | #include <linux/bug.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <asm-generic/pgtable_uffd.h> |
| 18 | #include <linux/page_table_check.h> |
| 19 | |
| 20 | #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \ |
| 21 | defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS |
| 22 | #error CONFIG_PGTABLE_LEVELS is not consistent with __PAGETABLE_{P4D,PUD,PMD}_FOLDED |
| 23 | #endif |
| 24 | |
| 25 | /* |
| 26 | * This defines the generic helper for accessing PMD page |
| 27 | * table page. Although platforms can still override this |
| 28 | * via their respective <asm/pgtable.h>. |
| 29 | */ |
| 30 | #ifndef pmd_pgtable |
| 31 | #define pmd_pgtable(pmd) pmd_page(pmd) |
| 32 | #endif |
| 33 | |
| 34 | #define pmd_folio(pmd) page_folio(pmd_page(pmd)) |
| 35 | |
| 36 | /* |
| 37 | * A page table page can be thought of an array like this: pXd_t[PTRS_PER_PxD] |
| 38 | * |
| 39 | * The pXx_index() functions return the index of the entry in the page |
| 40 | * table page which would control the given virtual address |
| 41 | * |
| 42 | * As these functions may be used by the same code for different levels of |
| 43 | * the page table folding, they are always available, regardless of |
| 44 | * CONFIG_PGTABLE_LEVELS value. For the folded levels they simply return 0 |
| 45 | * because in such cases PTRS_PER_PxD equals 1. |
| 46 | */ |
| 47 | |
| 48 | static inline unsigned long pte_index(unsigned long address) |
| 49 | { |
| 50 | return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); |
| 51 | } |
| 52 | |
| 53 | #ifndef pmd_index |
| 54 | static inline unsigned long pmd_index(unsigned long address) |
| 55 | { |
| 56 | return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1); |
| 57 | } |
| 58 | #define pmd_index pmd_index |
| 59 | #endif |
| 60 | |
| 61 | #ifndef pud_index |
| 62 | static inline unsigned long pud_index(unsigned long address) |
| 63 | { |
| 64 | return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1); |
| 65 | } |
| 66 | #define pud_index pud_index |
| 67 | #endif |
| 68 | |
| 69 | #ifndef pgd_index |
| 70 | /* Must be a compile-time constant, so implement it as a macro */ |
| 71 | #define pgd_index(a) (((a) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1)) |
| 72 | #endif |
| 73 | |
| 74 | #ifndef kernel_pte_init |
| 75 | static inline void kernel_pte_init(void *addr) |
| 76 | { |
| 77 | } |
| 78 | #define kernel_pte_init kernel_pte_init |
| 79 | #endif |
| 80 | |
| 81 | #ifndef pmd_init |
| 82 | static inline void pmd_init(void *addr) |
| 83 | { |
| 84 | } |
| 85 | #define pmd_init pmd_init |
| 86 | #endif |
| 87 | |
| 88 | #ifndef pud_init |
| 89 | static inline void pud_init(void *addr) |
| 90 | { |
| 91 | } |
| 92 | #define pud_init pud_init |
| 93 | #endif |
| 94 | |
| 95 | #ifndef pte_offset_kernel |
| 96 | static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address) |
| 97 | { |
| 98 | return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address); |
| 99 | } |
| 100 | #define pte_offset_kernel pte_offset_kernel |
| 101 | #endif |
| 102 | |
| 103 | #ifdef CONFIG_HIGHPTE |
| 104 | #define __pte_map(pmd, address) \ |
| 105 | ((pte_t *)kmap_local_page(pmd_page(*(pmd))) + pte_index((address))) |
| 106 | #define pte_unmap(pte) do { \ |
| 107 | kunmap_local((pte)); \ |
| 108 | rcu_read_unlock(); \ |
| 109 | } while (0) |
| 110 | #else |
| 111 | static inline pte_t *__pte_map(pmd_t *pmd, unsigned long address) |
| 112 | { |
| 113 | return pte_offset_kernel(pmd, address); |
| 114 | } |
| 115 | static inline void pte_unmap(pte_t *pte) |
| 116 | { |
| 117 | rcu_read_unlock(); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable); |
| 122 | |
| 123 | /* Find an entry in the second-level page table.. */ |
| 124 | #ifndef pmd_offset |
| 125 | static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address) |
| 126 | { |
| 127 | return pud_pgtable(*pud) + pmd_index(address); |
| 128 | } |
| 129 | #define pmd_offset pmd_offset |
| 130 | #endif |
| 131 | |
| 132 | #ifndef pud_offset |
| 133 | static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address) |
| 134 | { |
| 135 | return p4d_pgtable(*p4d) + pud_index(address); |
| 136 | } |
| 137 | #define pud_offset pud_offset |
| 138 | #endif |
| 139 | |
| 140 | static inline pgd_t *pgd_offset_pgd(pgd_t *pgd, unsigned long address) |
| 141 | { |
| 142 | return (pgd + pgd_index(address)); |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * a shortcut to get a pgd_t in a given mm |
| 147 | */ |
| 148 | #ifndef pgd_offset |
| 149 | #define pgd_offset(mm, address) pgd_offset_pgd((mm)->pgd, (address)) |
| 150 | #endif |
| 151 | |
| 152 | /* |
| 153 | * a shortcut which implies the use of the kernel's pgd, instead |
| 154 | * of a process's |
| 155 | */ |
| 156 | #define pgd_offset_k(address) pgd_offset(&init_mm, (address)) |
| 157 | |
| 158 | /* |
| 159 | * In many cases it is known that a virtual address is mapped at PMD or PTE |
| 160 | * level, so instead of traversing all the page table levels, we can get a |
| 161 | * pointer to the PMD entry in user or kernel page table or translate a virtual |
| 162 | * address to the pointer in the PTE in the kernel page tables with simple |
| 163 | * helpers. |
| 164 | */ |
| 165 | static inline pmd_t *pmd_off(struct mm_struct *mm, unsigned long va) |
| 166 | { |
| 167 | return pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, va), va), va), va); |
| 168 | } |
| 169 | |
| 170 | static inline pmd_t *pmd_off_k(unsigned long va) |
| 171 | { |
| 172 | return pmd_offset(pud_offset(p4d_offset(pgd_offset_k(va), va), va), va); |
| 173 | } |
| 174 | |
| 175 | static inline pte_t *virt_to_kpte(unsigned long vaddr) |
| 176 | { |
| 177 | pmd_t *pmd = pmd_off_k(vaddr); |
| 178 | |
| 179 | return pmd_none(*pmd) ? NULL : pte_offset_kernel(pmd, vaddr); |
| 180 | } |
| 181 | |
| 182 | #ifndef pmd_young |
| 183 | static inline int pmd_young(pmd_t pmd) |
| 184 | { |
| 185 | return 0; |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | #ifndef pmd_dirty |
| 190 | static inline int pmd_dirty(pmd_t pmd) |
| 191 | { |
| 192 | return 0; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | /* |
| 197 | * A facility to provide lazy MMU batching. This allows PTE updates and |
| 198 | * page invalidations to be delayed until a call to leave lazy MMU mode |
| 199 | * is issued. Some architectures may benefit from doing this, and it is |
| 200 | * beneficial for both shadow and direct mode hypervisors, which may batch |
| 201 | * the PTE updates which happen during this window. Note that using this |
| 202 | * interface requires that read hazards be removed from the code. A read |
| 203 | * hazard could result in the direct mode hypervisor case, since the actual |
| 204 | * write to the page tables may not yet have taken place, so reads though |
| 205 | * a raw PTE pointer after it has been modified are not guaranteed to be |
| 206 | * up to date. |
| 207 | * |
| 208 | * In the general case, no lock is guaranteed to be held between entry and exit |
| 209 | * of the lazy mode. (In practice, for user PTE updates, the appropriate page |
| 210 | * table lock(s) are held, but for kernel PTE updates, no lock is held). |
| 211 | * The implementation must therefore assume preemption may be enabled upon |
| 212 | * entry to the mode and cpu migration is possible; it must take steps to be |
| 213 | * robust against this. An implementation may handle this by disabling |
| 214 | * preemption, as a consequence generic code may not sleep while the lazy MMU |
| 215 | * mode is active. |
| 216 | * |
| 217 | * The mode is disabled in interrupt context and calls to the lazy_mmu API have |
| 218 | * no effect. |
| 219 | * |
| 220 | * The lazy MMU mode is enabled for a given block of code using: |
| 221 | * |
| 222 | * lazy_mmu_mode_enable(); |
| 223 | * <code> |
| 224 | * lazy_mmu_mode_disable(); |
| 225 | * |
| 226 | * Nesting is permitted: <code> may itself use an enable()/disable() pair. |
| 227 | * A nested call to enable() has no functional effect; however disable() causes |
| 228 | * any batched architectural state to be flushed regardless of nesting. After a |
| 229 | * call to disable(), the caller can therefore rely on all previous page table |
| 230 | * modifications to have taken effect, but the lazy MMU mode may still be |
| 231 | * enabled. |
| 232 | * |
| 233 | * In certain cases, it may be desirable to temporarily pause the lazy MMU mode. |
| 234 | * This can be done using: |
| 235 | * |
| 236 | * lazy_mmu_mode_pause(); |
| 237 | * <code> |
| 238 | * lazy_mmu_mode_resume(); |
| 239 | * |
| 240 | * pause() ensures that the mode is exited regardless of the nesting level; |
| 241 | * resume() re-enters the mode at the same nesting level. Any call to the |
| 242 | * lazy_mmu_mode_* API between those two calls has no effect. In particular, |
| 243 | * this means that pause()/resume() pairs may nest. |
| 244 | * |
| 245 | * is_lazy_mmu_mode_active() can be used to check whether the lazy MMU mode is |
| 246 | * currently enabled. |
| 247 | */ |
| 248 | #ifdef CONFIG_ARCH_HAS_LAZY_MMU_MODE |
| 249 | /** |
| 250 | * lazy_mmu_mode_enable() - Enable the lazy MMU mode. |
| 251 | * |
| 252 | * Enters a new lazy MMU mode section; if the mode was not already enabled, |
| 253 | * enables it and calls arch_enter_lazy_mmu_mode(). |
| 254 | * |
| 255 | * Must be paired with a call to lazy_mmu_mode_disable(). |
| 256 | * |
| 257 | * Has no effect if called: |
| 258 | * - While paused - see lazy_mmu_mode_pause() |
| 259 | * - In interrupt context |
| 260 | */ |
| 261 | static inline void lazy_mmu_mode_enable(void) |
| 262 | { |
| 263 | struct lazy_mmu_state *state = ¤t->lazy_mmu_state; |
| 264 | |
| 265 | if (in_interrupt() || state->pause_count > 0) |
| 266 | return; |
| 267 | |
| 268 | VM_WARN_ON_ONCE(state->enable_count == U8_MAX); |
| 269 | |
| 270 | if (state->enable_count++ == 0) |
| 271 | arch_enter_lazy_mmu_mode(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * lazy_mmu_mode_disable() - Disable the lazy MMU mode. |
| 276 | * |
| 277 | * Exits the current lazy MMU mode section. If it is the outermost section, |
| 278 | * disables the mode and calls arch_leave_lazy_mmu_mode(). Otherwise (nested |
| 279 | * section), calls arch_flush_lazy_mmu_mode(). |
| 280 | * |
| 281 | * Must match a call to lazy_mmu_mode_enable(). |
| 282 | * |
| 283 | * Has no effect if called: |
| 284 | * - While paused - see lazy_mmu_mode_pause() |
| 285 | * - In interrupt context |
| 286 | */ |
| 287 | static inline void lazy_mmu_mode_disable(void) |
| 288 | { |
| 289 | struct lazy_mmu_state *state = ¤t->lazy_mmu_state; |
| 290 | |
| 291 | if (in_interrupt() || state->pause_count > 0) |
| 292 | return; |
| 293 | |
| 294 | VM_WARN_ON_ONCE(state->enable_count == 0); |
| 295 | |
| 296 | if (--state->enable_count == 0) |
| 297 | arch_leave_lazy_mmu_mode(); |
| 298 | else /* Exiting a nested section */ |
| 299 | arch_flush_lazy_mmu_mode(); |
| 300 | |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * __task_lazy_mmu_mode_pause() - Pause the lazy MMU mode for a task. |
| 305 | * @tsk: The task to check. |
| 306 | * |
| 307 | * Pauses the lazy MMU mode of @tsk. |
| 308 | * |
| 309 | * This function only operates on the state saved in task_struct; to pause |
| 310 | * current lazy_mmu_mode_pause() should be used instead. |
| 311 | * |
| 312 | * This function is intended for architectures that implement the lazy MMU |
| 313 | * mode; it must not be called from generic code. |
| 314 | */ |
| 315 | static inline void __task_lazy_mmu_mode_pause(struct task_struct *tsk) |
| 316 | { |
| 317 | struct lazy_mmu_state *state = &tsk->lazy_mmu_state; |
| 318 | |
| 319 | VM_WARN_ON_ONCE(state->pause_count == U8_MAX); |
| 320 | |
| 321 | if (state->pause_count++ == 0 && state->enable_count > 0) |
| 322 | arch_leave_lazy_mmu_mode(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * lazy_mmu_mode_pause() - Pause the lazy MMU mode. |
| 327 | * |
| 328 | * Pauses the lazy MMU mode; if it is currently active, disables it and calls |
| 329 | * arch_leave_lazy_mmu_mode(). |
| 330 | * |
| 331 | * Must be paired with a call to lazy_mmu_mode_resume(). Calls to the |
| 332 | * lazy_mmu_mode_* API have no effect until the matching resume() call. |
| 333 | * |
| 334 | * Has no effect if called: |
| 335 | * - While paused (inside another pause()/resume() pair) |
| 336 | * - In interrupt context |
| 337 | */ |
| 338 | static inline void lazy_mmu_mode_pause(void) |
| 339 | { |
| 340 | if (in_interrupt()) |
| 341 | return; |
| 342 | |
| 343 | __task_lazy_mmu_mode_pause(current); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * __task_lazy_mmu_mode_resume() - Resume the lazy MMU mode for a task. |
| 348 | * @tsk: The task to check. |
| 349 | * |
| 350 | * Resumes the lazy MMU mode of @tsk. |
| 351 | * |
| 352 | * This function only operates on the state saved in task_struct; to resume |
| 353 | * current lazy_mmu_mode_resume() should be used instead. |
| 354 | * |
| 355 | * This function is intended for architectures that implement the lazy MMU |
| 356 | * mode; it must not be called from generic code. |
| 357 | */ |
| 358 | static inline void __task_lazy_mmu_mode_resume(struct task_struct *tsk) |
| 359 | { |
| 360 | struct lazy_mmu_state *state = &tsk->lazy_mmu_state; |
| 361 | |
| 362 | VM_WARN_ON_ONCE(state->pause_count == 0); |
| 363 | |
| 364 | if (--state->pause_count == 0 && state->enable_count > 0) |
| 365 | arch_enter_lazy_mmu_mode(); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * lazy_mmu_mode_resume() - Resume the lazy MMU mode. |
| 370 | * |
| 371 | * Resumes the lazy MMU mode; if it was active at the point where the matching |
| 372 | * call to lazy_mmu_mode_pause() was made, re-enables it and calls |
| 373 | * arch_enter_lazy_mmu_mode(). |
| 374 | * |
| 375 | * Must match a call to lazy_mmu_mode_pause(). |
| 376 | * |
| 377 | * Has no effect if called: |
| 378 | * - While paused (inside another pause()/resume() pair) |
| 379 | * - In interrupt context |
| 380 | */ |
| 381 | static inline void lazy_mmu_mode_resume(void) |
| 382 | { |
| 383 | if (in_interrupt()) |
| 384 | return; |
| 385 | |
| 386 | __task_lazy_mmu_mode_resume(current); |
| 387 | } |
| 388 | #else |
| 389 | static inline void lazy_mmu_mode_enable(void) {} |
| 390 | static inline void lazy_mmu_mode_disable(void) {} |
| 391 | static inline void lazy_mmu_mode_pause(void) {} |
| 392 | static inline void lazy_mmu_mode_resume(void) {} |
| 393 | #endif |
| 394 | |
| 395 | #ifndef pte_batch_hint |
| 396 | /** |
| 397 | * pte_batch_hint - Number of pages that can be added to batch without scanning. |
| 398 | * @ptep: Page table pointer for the entry. |
| 399 | * @pte: Page table entry. |
| 400 | * |
| 401 | * Some architectures know that a set of contiguous ptes all map the same |
| 402 | * contiguous memory with the same permissions. In this case, it can provide a |
| 403 | * hint to aid pte batching without the core code needing to scan every pte. |
| 404 | * |
| 405 | * An architecture implementation may ignore the PTE accessed state. Further, |
| 406 | * the dirty state must apply atomically to all the PTEs described by the hint. |
| 407 | * |
| 408 | * May be overridden by the architecture, else pte_batch_hint is always 1. |
| 409 | */ |
| 410 | static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) |
| 411 | { |
| 412 | return 1; |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | #ifndef pte_advance_pfn |
| 417 | static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) |
| 418 | { |
| 419 | return __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT)); |
| 420 | } |
| 421 | #endif |
| 422 | |
| 423 | #define pte_next_pfn(pte) pte_advance_pfn(pte, 1) |
| 424 | |
| 425 | #ifndef set_ptes |
| 426 | /** |
| 427 | * set_ptes - Map consecutive pages to a contiguous range of addresses. |
| 428 | * @mm: Address space to map the pages into. |
| 429 | * @addr: Address to map the first page at. |
| 430 | * @ptep: Page table pointer for the first entry. |
| 431 | * @pte: Page table entry for the first page. |
| 432 | * @nr: Number of pages to map. |
| 433 | * |
| 434 | * When nr==1, initial state of pte may be present or not present, and new state |
| 435 | * may be present or not present. When nr>1, initial state of all ptes must be |
| 436 | * not present, and new state must be present. |
| 437 | * |
| 438 | * May be overridden by the architecture, or the architecture can define |
| 439 | * set_pte() and PFN_PTE_SHIFT. |
| 440 | * |
| 441 | * Context: The caller holds the page table lock. The pages all belong |
| 442 | * to the same folio. The PTEs are all in the same PMD. |
| 443 | */ |
| 444 | static inline void set_ptes(struct mm_struct *mm, unsigned long addr, |
| 445 | pte_t *ptep, pte_t pte, unsigned int nr) |
| 446 | { |
| 447 | page_table_check_ptes_set(mm, addr, ptep, pte, nr); |
| 448 | |
| 449 | for (;;) { |
| 450 | set_pte(ptep, pte); |
| 451 | if (--nr == 0) |
| 452 | break; |
| 453 | ptep++; |
| 454 | pte = pte_next_pfn(pte); |
| 455 | } |
| 456 | } |
| 457 | #endif |
| 458 | #define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) |
| 459 | |
| 460 | #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS |
| 461 | extern int ptep_set_access_flags(struct vm_area_struct *vma, |
| 462 | unsigned long address, pte_t *ptep, |
| 463 | pte_t entry, int dirty); |
| 464 | #endif |
| 465 | |
| 466 | #ifndef __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS |
| 467 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 468 | extern int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 469 | unsigned long address, pmd_t *pmdp, |
| 470 | pmd_t entry, int dirty); |
| 471 | extern int pudp_set_access_flags(struct vm_area_struct *vma, |
| 472 | unsigned long address, pud_t *pudp, |
| 473 | pud_t entry, int dirty); |
| 474 | #else |
| 475 | static inline int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 476 | unsigned long address, pmd_t *pmdp, |
| 477 | pmd_t entry, int dirty) |
| 478 | { |
| 479 | BUILD_BUG(); |
| 480 | return 0; |
| 481 | } |
| 482 | static inline int pudp_set_access_flags(struct vm_area_struct *vma, |
| 483 | unsigned long address, pud_t *pudp, |
| 484 | pud_t entry, int dirty) |
| 485 | { |
| 486 | BUILD_BUG(); |
| 487 | return 0; |
| 488 | } |
| 489 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 490 | #endif |
| 491 | |
| 492 | #ifndef ptep_get |
| 493 | static inline pte_t ptep_get(pte_t *ptep) |
| 494 | { |
| 495 | return READ_ONCE(*ptep); |
| 496 | } |
| 497 | #endif |
| 498 | |
| 499 | #ifndef pmdp_get |
| 500 | static inline pmd_t pmdp_get(pmd_t *pmdp) |
| 501 | { |
| 502 | return READ_ONCE(*pmdp); |
| 503 | } |
| 504 | #endif |
| 505 | |
| 506 | #ifndef pudp_get |
| 507 | static inline pud_t pudp_get(pud_t *pudp) |
| 508 | { |
| 509 | return READ_ONCE(*pudp); |
| 510 | } |
| 511 | #endif |
| 512 | |
| 513 | #ifndef p4dp_get |
| 514 | static inline p4d_t p4dp_get(p4d_t *p4dp) |
| 515 | { |
| 516 | return READ_ONCE(*p4dp); |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | #ifndef pgdp_get |
| 521 | static inline pgd_t pgdp_get(pgd_t *pgdp) |
| 522 | { |
| 523 | return READ_ONCE(*pgdp); |
| 524 | } |
| 525 | #endif |
| 526 | |
| 527 | #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG |
| 528 | static inline bool ptep_test_and_clear_young(struct vm_area_struct *vma, |
| 529 | unsigned long address, pte_t *ptep) |
| 530 | { |
| 531 | pte_t pte = ptep_get(ptep); |
| 532 | bool young = true; |
| 533 | |
| 534 | if (!pte_young(pte)) |
| 535 | young = false; |
| 536 | else |
| 537 | set_pte_at(vma->vm_mm, address, ptep, pte_mkold(pte)); |
| 538 | return young; |
| 539 | } |
| 540 | #endif |
| 541 | |
| 542 | #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG |
| 543 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) |
| 544 | static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma, |
| 545 | unsigned long address, pmd_t *pmdp) |
| 546 | { |
| 547 | pmd_t pmd = *pmdp; |
| 548 | bool young = true; |
| 549 | |
| 550 | if (!pmd_young(pmd)) |
| 551 | young = false; |
| 552 | else |
| 553 | set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd)); |
| 554 | return young; |
| 555 | } |
| 556 | #else |
| 557 | static inline bool pmdp_test_and_clear_young(struct vm_area_struct *vma, |
| 558 | unsigned long address, pmd_t *pmdp) |
| 559 | { |
| 560 | BUILD_BUG(); |
| 561 | return false; |
| 562 | } |
| 563 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */ |
| 564 | #endif |
| 565 | |
| 566 | #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH |
| 567 | bool ptep_clear_flush_young(struct vm_area_struct *vma, |
| 568 | unsigned long address, pte_t *ptep); |
| 569 | #endif |
| 570 | |
| 571 | #ifndef __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH |
| 572 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 573 | bool pmdp_clear_flush_young(struct vm_area_struct *vma, |
| 574 | unsigned long address, pmd_t *pmdp); |
| 575 | #else |
| 576 | /* |
| 577 | * Despite relevant to THP only, this API is called from generic rmap code |
| 578 | * under PageTransHuge(), hence needs a dummy implementation for !THP |
| 579 | */ |
| 580 | static inline bool pmdp_clear_flush_young(struct vm_area_struct *vma, |
| 581 | unsigned long address, pmd_t *pmdp) |
| 582 | { |
| 583 | BUILD_BUG(); |
| 584 | return false; |
| 585 | } |
| 586 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 587 | #endif |
| 588 | |
| 589 | #ifndef arch_has_hw_nonleaf_pmd_young |
| 590 | /* |
| 591 | * Return whether the accessed bit in non-leaf PMD entries is supported on the |
| 592 | * local CPU. |
| 593 | */ |
| 594 | static inline bool arch_has_hw_nonleaf_pmd_young(void) |
| 595 | { |
| 596 | return IS_ENABLED(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG); |
| 597 | } |
| 598 | #endif |
| 599 | |
| 600 | #ifndef arch_has_hw_pte_young |
| 601 | /* |
| 602 | * Return whether the accessed bit is supported on the local CPU. |
| 603 | * |
| 604 | * This stub assumes accessing through an old PTE triggers a page fault. |
| 605 | * Architectures that automatically set the access bit should overwrite it. |
| 606 | */ |
| 607 | static inline bool arch_has_hw_pte_young(void) |
| 608 | { |
| 609 | return IS_ENABLED(CONFIG_ARCH_HAS_HW_PTE_YOUNG); |
| 610 | } |
| 611 | #endif |
| 612 | |
| 613 | #ifndef exec_folio_order |
| 614 | /* |
| 615 | * Returns preferred minimum folio order for executable file-backed memory. Must |
| 616 | * be in range [0, PMD_ORDER). Default to order-0. |
| 617 | */ |
| 618 | static inline unsigned int exec_folio_order(void) |
| 619 | { |
| 620 | return 0; |
| 621 | } |
| 622 | #endif |
| 623 | |
| 624 | #ifndef arch_check_zapped_pte |
| 625 | static inline void arch_check_zapped_pte(struct vm_area_struct *vma, |
| 626 | pte_t pte) |
| 627 | { |
| 628 | } |
| 629 | #endif |
| 630 | |
| 631 | #ifndef arch_check_zapped_pmd |
| 632 | static inline void arch_check_zapped_pmd(struct vm_area_struct *vma, |
| 633 | pmd_t pmd) |
| 634 | { |
| 635 | } |
| 636 | #endif |
| 637 | |
| 638 | #ifndef arch_check_zapped_pud |
| 639 | static inline void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) |
| 640 | { |
| 641 | } |
| 642 | #endif |
| 643 | |
| 644 | #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR |
| 645 | static inline pte_t ptep_get_and_clear(struct mm_struct *mm, |
| 646 | unsigned long address, |
| 647 | pte_t *ptep) |
| 648 | { |
| 649 | pte_t pte = ptep_get(ptep); |
| 650 | pte_clear(mm, address, ptep); |
| 651 | page_table_check_pte_clear(mm, address, pte); |
| 652 | return pte; |
| 653 | } |
| 654 | #endif |
| 655 | |
| 656 | #ifndef clear_young_dirty_ptes |
| 657 | /** |
| 658 | * clear_young_dirty_ptes - Mark PTEs that map consecutive pages of the |
| 659 | * same folio as old/clean. |
| 660 | * @mm: Address space the pages are mapped into. |
| 661 | * @addr: Address the first page is mapped at. |
| 662 | * @ptep: Page table pointer for the first entry. |
| 663 | * @nr: Number of entries to mark old/clean. |
| 664 | * @flags: Flags to modify the PTE batch semantics. |
| 665 | * |
| 666 | * May be overridden by the architecture; otherwise, implemented by |
| 667 | * get_and_clear/modify/set for each pte in the range. |
| 668 | * |
| 669 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 670 | * some PTEs might be write-protected. |
| 671 | * |
| 672 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 673 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 674 | */ |
| 675 | static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, |
| 676 | unsigned long addr, pte_t *ptep, |
| 677 | unsigned int nr, cydp_t flags) |
| 678 | { |
| 679 | pte_t pte; |
| 680 | |
| 681 | for (;;) { |
| 682 | if (flags == CYDP_CLEAR_YOUNG) |
| 683 | ptep_test_and_clear_young(vma, addr, ptep); |
| 684 | else { |
| 685 | pte = ptep_get_and_clear(vma->vm_mm, addr, ptep); |
| 686 | if (flags & CYDP_CLEAR_YOUNG) |
| 687 | pte = pte_mkold(pte); |
| 688 | if (flags & CYDP_CLEAR_DIRTY) |
| 689 | pte = pte_mkclean(pte); |
| 690 | set_pte_at(vma->vm_mm, addr, ptep, pte); |
| 691 | } |
| 692 | if (--nr == 0) |
| 693 | break; |
| 694 | ptep++; |
| 695 | addr += PAGE_SIZE; |
| 696 | } |
| 697 | } |
| 698 | #endif |
| 699 | |
| 700 | static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, |
| 701 | pte_t *ptep) |
| 702 | { |
| 703 | pte_t pte = ptep_get(ptep); |
| 704 | |
| 705 | pte_clear(mm, addr, ptep); |
| 706 | /* |
| 707 | * No need for ptep_get_and_clear(): page table check doesn't care about |
| 708 | * any bits that could have been set by HW concurrently. |
| 709 | */ |
| 710 | page_table_check_pte_clear(mm, addr, pte); |
| 711 | } |
| 712 | |
| 713 | #ifdef CONFIG_GUP_GET_PXX_LOW_HIGH |
| 714 | /* |
| 715 | * For walking the pagetables without holding any locks. Some architectures |
| 716 | * (eg x86-32 PAE) cannot load the entries atomically without using expensive |
| 717 | * instructions. We are guaranteed that a PTE will only either go from not |
| 718 | * present to present, or present to not present -- it will not switch to a |
| 719 | * completely different present page without a TLB flush inbetween; which we |
| 720 | * are blocking by holding interrupts off. |
| 721 | * |
| 722 | * Setting ptes from not present to present goes: |
| 723 | * |
| 724 | * ptep->pte_high = h; |
| 725 | * smp_wmb(); |
| 726 | * ptep->pte_low = l; |
| 727 | * |
| 728 | * And present to not present goes: |
| 729 | * |
| 730 | * ptep->pte_low = 0; |
| 731 | * smp_wmb(); |
| 732 | * ptep->pte_high = 0; |
| 733 | * |
| 734 | * We must ensure here that the load of pte_low sees 'l' IFF pte_high sees 'h'. |
| 735 | * We load pte_high *after* loading pte_low, which ensures we don't see an older |
| 736 | * value of pte_high. *Then* we recheck pte_low, which ensures that we haven't |
| 737 | * picked up a changed pte high. We might have gotten rubbish values from |
| 738 | * pte_low and pte_high, but we are guaranteed that pte_low will not have the |
| 739 | * present bit set *unless* it is 'l'. Because get_user_pages_fast() only |
| 740 | * operates on present ptes we're safe. |
| 741 | */ |
| 742 | static inline pte_t ptep_get_lockless(pte_t *ptep) |
| 743 | { |
| 744 | pte_t pte; |
| 745 | |
| 746 | do { |
| 747 | pte.pte_low = ptep->pte_low; |
| 748 | smp_rmb(); |
| 749 | pte.pte_high = ptep->pte_high; |
| 750 | smp_rmb(); |
| 751 | } while (unlikely(pte.pte_low != ptep->pte_low)); |
| 752 | |
| 753 | return pte; |
| 754 | } |
| 755 | #define ptep_get_lockless ptep_get_lockless |
| 756 | |
| 757 | #if CONFIG_PGTABLE_LEVELS > 2 |
| 758 | static inline pmd_t pmdp_get_lockless(pmd_t *pmdp) |
| 759 | { |
| 760 | pmd_t pmd; |
| 761 | |
| 762 | do { |
| 763 | pmd.pmd_low = pmdp->pmd_low; |
| 764 | smp_rmb(); |
| 765 | pmd.pmd_high = pmdp->pmd_high; |
| 766 | smp_rmb(); |
| 767 | } while (unlikely(pmd.pmd_low != pmdp->pmd_low)); |
| 768 | |
| 769 | return pmd; |
| 770 | } |
| 771 | #define pmdp_get_lockless pmdp_get_lockless |
| 772 | #define pmdp_get_lockless_sync() tlb_remove_table_sync_one() |
| 773 | #endif /* CONFIG_PGTABLE_LEVELS > 2 */ |
| 774 | #endif /* CONFIG_GUP_GET_PXX_LOW_HIGH */ |
| 775 | |
| 776 | /* |
| 777 | * We require that the PTE can be read atomically. |
| 778 | */ |
| 779 | #ifndef ptep_get_lockless |
| 780 | static inline pte_t ptep_get_lockless(pte_t *ptep) |
| 781 | { |
| 782 | return ptep_get(ptep); |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | #ifndef pmdp_get_lockless |
| 787 | static inline pmd_t pmdp_get_lockless(pmd_t *pmdp) |
| 788 | { |
| 789 | return pmdp_get(pmdp); |
| 790 | } |
| 791 | static inline void pmdp_get_lockless_sync(void) |
| 792 | { |
| 793 | } |
| 794 | #endif |
| 795 | |
| 796 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 797 | #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR |
| 798 | static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, |
| 799 | unsigned long address, |
| 800 | pmd_t *pmdp) |
| 801 | { |
| 802 | pmd_t pmd = *pmdp; |
| 803 | |
| 804 | pmd_clear(pmdp); |
| 805 | page_table_check_pmd_clear(mm, address, pmd); |
| 806 | |
| 807 | return pmd; |
| 808 | } |
| 809 | #endif /* __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR */ |
| 810 | #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR |
| 811 | static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, |
| 812 | unsigned long address, |
| 813 | pud_t *pudp) |
| 814 | { |
| 815 | pud_t pud = *pudp; |
| 816 | |
| 817 | pud_clear(pudp); |
| 818 | page_table_check_pud_clear(mm, address, pud); |
| 819 | |
| 820 | return pud; |
| 821 | } |
| 822 | #endif /* __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR */ |
| 823 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 824 | |
| 825 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 826 | #ifndef __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR_FULL |
| 827 | static inline pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma, |
| 828 | unsigned long address, pmd_t *pmdp, |
| 829 | int full) |
| 830 | { |
| 831 | return pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); |
| 832 | } |
| 833 | #endif |
| 834 | |
| 835 | #ifndef __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR_FULL |
| 836 | static inline pud_t pudp_huge_get_and_clear_full(struct vm_area_struct *vma, |
| 837 | unsigned long address, pud_t *pudp, |
| 838 | int full) |
| 839 | { |
| 840 | return pudp_huge_get_and_clear(vma->vm_mm, address, pudp); |
| 841 | } |
| 842 | #endif |
| 843 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 844 | |
| 845 | #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL |
| 846 | static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, |
| 847 | unsigned long address, pte_t *ptep, |
| 848 | int full) |
| 849 | { |
| 850 | return ptep_get_and_clear(mm, address, ptep); |
| 851 | } |
| 852 | #endif |
| 853 | |
| 854 | #ifndef get_and_clear_full_ptes |
| 855 | /** |
| 856 | * get_and_clear_full_ptes - Clear present PTEs that map consecutive pages of |
| 857 | * the same folio, collecting dirty/accessed bits. |
| 858 | * @mm: Address space the pages are mapped into. |
| 859 | * @addr: Address the first page is mapped at. |
| 860 | * @ptep: Page table pointer for the first entry. |
| 861 | * @nr: Number of entries to clear. |
| 862 | * @full: Whether we are clearing a full mm. |
| 863 | * |
| 864 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 865 | * loop over ptep_get_and_clear_full(), merging dirty/accessed bits into the |
| 866 | * returned PTE. |
| 867 | * |
| 868 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 869 | * some PTEs might be write-protected. |
| 870 | * |
| 871 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 872 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 873 | */ |
| 874 | static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm, |
| 875 | unsigned long addr, pte_t *ptep, unsigned int nr, int full) |
| 876 | { |
| 877 | pte_t pte, tmp_pte; |
| 878 | |
| 879 | pte = ptep_get_and_clear_full(mm, addr, ptep, full); |
| 880 | while (--nr) { |
| 881 | ptep++; |
| 882 | addr += PAGE_SIZE; |
| 883 | tmp_pte = ptep_get_and_clear_full(mm, addr, ptep, full); |
| 884 | if (pte_dirty(tmp_pte)) |
| 885 | pte = pte_mkdirty(pte); |
| 886 | if (pte_young(tmp_pte)) |
| 887 | pte = pte_mkyoung(pte); |
| 888 | } |
| 889 | return pte; |
| 890 | } |
| 891 | #endif |
| 892 | |
| 893 | /** |
| 894 | * get_and_clear_ptes - Clear present PTEs that map consecutive pages of |
| 895 | * the same folio, collecting dirty/accessed bits. |
| 896 | * @mm: Address space the pages are mapped into. |
| 897 | * @addr: Address the first page is mapped at. |
| 898 | * @ptep: Page table pointer for the first entry. |
| 899 | * @nr: Number of entries to clear. |
| 900 | * |
| 901 | * Use this instead of get_and_clear_full_ptes() if it is known that we don't |
| 902 | * need to clear the full mm, which is mostly the case. |
| 903 | * |
| 904 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 905 | * some PTEs might be write-protected. |
| 906 | * |
| 907 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 908 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 909 | */ |
| 910 | static inline pte_t get_and_clear_ptes(struct mm_struct *mm, unsigned long addr, |
| 911 | pte_t *ptep, unsigned int nr) |
| 912 | { |
| 913 | return get_and_clear_full_ptes(mm, addr, ptep, nr, 0); |
| 914 | } |
| 915 | |
| 916 | #ifndef clear_full_ptes |
| 917 | /** |
| 918 | * clear_full_ptes - Clear present PTEs that map consecutive pages of the same |
| 919 | * folio. |
| 920 | * @mm: Address space the pages are mapped into. |
| 921 | * @addr: Address the first page is mapped at. |
| 922 | * @ptep: Page table pointer for the first entry. |
| 923 | * @nr: Number of entries to clear. |
| 924 | * @full: Whether we are clearing a full mm. |
| 925 | * |
| 926 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 927 | * loop over ptep_get_and_clear_full(). |
| 928 | * |
| 929 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 930 | * some PTEs might be write-protected. |
| 931 | * |
| 932 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 933 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 934 | */ |
| 935 | static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr, |
| 936 | pte_t *ptep, unsigned int nr, int full) |
| 937 | { |
| 938 | for (;;) { |
| 939 | ptep_get_and_clear_full(mm, addr, ptep, full); |
| 940 | if (--nr == 0) |
| 941 | break; |
| 942 | ptep++; |
| 943 | addr += PAGE_SIZE; |
| 944 | } |
| 945 | } |
| 946 | #endif |
| 947 | |
| 948 | /** |
| 949 | * clear_ptes - Clear present PTEs that map consecutive pages of the same folio. |
| 950 | * @mm: Address space the pages are mapped into. |
| 951 | * @addr: Address the first page is mapped at. |
| 952 | * @ptep: Page table pointer for the first entry. |
| 953 | * @nr: Number of entries to clear. |
| 954 | * |
| 955 | * Use this instead of clear_full_ptes() if it is known that we don't need to |
| 956 | * clear the full mm, which is mostly the case. |
| 957 | * |
| 958 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 959 | * some PTEs might be write-protected. |
| 960 | * |
| 961 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 962 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 963 | */ |
| 964 | static inline void clear_ptes(struct mm_struct *mm, unsigned long addr, |
| 965 | pte_t *ptep, unsigned int nr) |
| 966 | { |
| 967 | clear_full_ptes(mm, addr, ptep, nr, 0); |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * If two threads concurrently fault at the same page, the thread that |
| 972 | * won the race updates the PTE and its local TLB/Cache. The other thread |
| 973 | * gives up, simply does nothing, and continues; on architectures where |
| 974 | * software can update TLB, local TLB can be updated here to avoid next page |
| 975 | * fault. This function updates TLB only, do nothing with cache or others. |
| 976 | * It is the difference with function update_mmu_cache. |
| 977 | */ |
| 978 | #ifndef update_mmu_tlb_range |
| 979 | static inline void update_mmu_tlb_range(struct vm_area_struct *vma, |
| 980 | unsigned long address, pte_t *ptep, unsigned int nr) |
| 981 | { |
| 982 | } |
| 983 | #endif |
| 984 | |
| 985 | static inline void update_mmu_tlb(struct vm_area_struct *vma, |
| 986 | unsigned long address, pte_t *ptep) |
| 987 | { |
| 988 | update_mmu_tlb_range(vma, address, ptep, 1); |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * clear_nonpresent_ptes - Clear multiple non-present PTEs which are |
| 993 | * consecutive in the pgtable. |
| 994 | * @mm: Address space the ptes represent. |
| 995 | * @addr: Address of the first pte. |
| 996 | * @ptep: Page table pointer for the first entry. |
| 997 | * @nr: Number of entries to clear. |
| 998 | * |
| 999 | * Context: The caller holds the page table lock. The PTEs are all not present. |
| 1000 | * The PTEs are all in the same PMD. |
| 1001 | */ |
| 1002 | static inline void clear_nonpresent_ptes(struct mm_struct *mm, |
| 1003 | unsigned long addr, pte_t *ptep, unsigned int nr) |
| 1004 | { |
| 1005 | (void)addr; |
| 1006 | |
| 1007 | for (;;) { |
| 1008 | pte_clear(mm, addr, ptep); |
| 1009 | if (--nr == 0) |
| 1010 | break; |
| 1011 | ptep++; |
| 1012 | addr += PAGE_SIZE; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | #ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH |
| 1017 | extern pte_t ptep_clear_flush(struct vm_area_struct *vma, |
| 1018 | unsigned long address, |
| 1019 | pte_t *ptep); |
| 1020 | #endif |
| 1021 | |
| 1022 | #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH |
| 1023 | extern pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, |
| 1024 | unsigned long address, |
| 1025 | pmd_t *pmdp); |
| 1026 | extern pud_t pudp_huge_clear_flush(struct vm_area_struct *vma, |
| 1027 | unsigned long address, |
| 1028 | pud_t *pudp); |
| 1029 | #endif |
| 1030 | |
| 1031 | #ifndef pte_mkwrite |
| 1032 | static inline pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) |
| 1033 | { |
| 1034 | return pte_mkwrite_novma(pte); |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
| 1038 | #if defined(CONFIG_ARCH_WANT_PMD_MKWRITE) && !defined(pmd_mkwrite) |
| 1039 | static inline pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
| 1040 | { |
| 1041 | return pmd_mkwrite_novma(pmd); |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
| 1045 | #ifndef __HAVE_ARCH_PTEP_SET_WRPROTECT |
| 1046 | struct mm_struct; |
| 1047 | static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) |
| 1048 | { |
| 1049 | pte_t old_pte = ptep_get(ptep); |
| 1050 | set_pte_at(mm, address, ptep, pte_wrprotect(old_pte)); |
| 1051 | } |
| 1052 | #endif |
| 1053 | |
| 1054 | #ifndef ptep_try_set |
| 1055 | /** |
| 1056 | * ptep_try_set - atomically set an empty kernel PTE |
| 1057 | * @ptep: page table entry |
| 1058 | * @new_pte: value to install |
| 1059 | * |
| 1060 | * Atomically set *@ptep to @new_pte iff *@ptep is pte_none(). Return true on |
| 1061 | * success, false if the slot was already populated or the arch has no |
| 1062 | * implementation. |
| 1063 | * |
| 1064 | * For special kernel page tables only - never user page tables. The caller must |
| 1065 | * prevent concurrent teardown of @ptep and must accept that other writers may |
| 1066 | * race. Concurrent clearers must use ptep_get_and_clear() so racing accesses |
| 1067 | * agree on the outcome. |
| 1068 | * |
| 1069 | * Architectures opt in by providing a cmpxchg-based override and defining |
| 1070 | * ptep_try_set as an identity macro. The generic stub returns false, which is |
| 1071 | * correct for callers that fall through to oops on failure. |
| 1072 | */ |
| 1073 | static inline bool ptep_try_set(pte_t *ptep, pte_t new_pte) |
| 1074 | { |
| 1075 | return false; |
| 1076 | } |
| 1077 | #endif |
| 1078 | |
| 1079 | #ifndef flush_tlb_before_set |
| 1080 | /** |
| 1081 | * flush_tlb_before_set - invalidate a kernel PTE's TLB before re-setting it |
| 1082 | * @addr: kernel virtual address whose PTE was just cleared |
| 1083 | * |
| 1084 | * Some architectures (e.g. arm64) do not allow a live page-table entry to be |
| 1085 | * repointed at a different page in one step. The old entry must first be made |
| 1086 | * invalid and its translation flushed from every TLB, and only then may the new |
| 1087 | * entry be written. |
| 1088 | * |
| 1089 | * This is only for the lockless atomic kernel-PTE installers (ptep_try_set()). |
| 1090 | * It must be callable with interrupts disabled. |
| 1091 | */ |
| 1092 | static inline void flush_tlb_before_set(unsigned long addr) |
| 1093 | { |
| 1094 | } |
| 1095 | #endif |
| 1096 | |
| 1097 | #ifndef wrprotect_ptes |
| 1098 | /** |
| 1099 | * wrprotect_ptes - Write-protect PTEs that map consecutive pages of the same |
| 1100 | * folio. |
| 1101 | * @mm: Address space the pages are mapped into. |
| 1102 | * @addr: Address the first page is mapped at. |
| 1103 | * @ptep: Page table pointer for the first entry. |
| 1104 | * @nr: Number of entries to write-protect. |
| 1105 | * |
| 1106 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 1107 | * loop over ptep_set_wrprotect(). |
| 1108 | * |
| 1109 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 1110 | * some PTEs might be write-protected. |
| 1111 | * |
| 1112 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 1113 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 1114 | */ |
| 1115 | static inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr, |
| 1116 | pte_t *ptep, unsigned int nr) |
| 1117 | { |
| 1118 | for (;;) { |
| 1119 | ptep_set_wrprotect(mm, addr, ptep); |
| 1120 | if (--nr == 0) |
| 1121 | break; |
| 1122 | ptep++; |
| 1123 | addr += PAGE_SIZE; |
| 1124 | } |
| 1125 | } |
| 1126 | #endif |
| 1127 | |
| 1128 | #ifndef clear_flush_young_ptes |
| 1129 | /** |
| 1130 | * clear_flush_young_ptes - Mark PTEs that map consecutive pages of the same |
| 1131 | * folio as old and flush the TLB. |
| 1132 | * @vma: The virtual memory area the pages are mapped into. |
| 1133 | * @addr: Address the first page is mapped at. |
| 1134 | * @ptep: Page table pointer for the first entry. |
| 1135 | * @nr: Number of entries to clear access bit. |
| 1136 | * |
| 1137 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 1138 | * loop over ptep_clear_flush_young(). |
| 1139 | * |
| 1140 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 1141 | * some PTEs might be write-protected. |
| 1142 | * |
| 1143 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 1144 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 1145 | */ |
| 1146 | static inline bool clear_flush_young_ptes(struct vm_area_struct *vma, |
| 1147 | unsigned long addr, pte_t *ptep, unsigned int nr) |
| 1148 | { |
| 1149 | bool young = false; |
| 1150 | |
| 1151 | for (;;) { |
| 1152 | young |= ptep_clear_flush_young(vma, addr, ptep); |
| 1153 | if (--nr == 0) |
| 1154 | break; |
| 1155 | ptep++; |
| 1156 | addr += PAGE_SIZE; |
| 1157 | } |
| 1158 | |
| 1159 | return young; |
| 1160 | } |
| 1161 | #endif |
| 1162 | |
| 1163 | #ifndef test_and_clear_young_ptes |
| 1164 | /** |
| 1165 | * test_and_clear_young_ptes - Mark PTEs that map consecutive pages of the same |
| 1166 | * folio as old |
| 1167 | * @vma: The virtual memory area the pages are mapped into. |
| 1168 | * @addr: Address the first page is mapped at. |
| 1169 | * @ptep: Page table pointer for the first entry. |
| 1170 | * @nr: Number of entries to clear access bit. |
| 1171 | * |
| 1172 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 1173 | * loop over ptep_test_and_clear_young(). |
| 1174 | * |
| 1175 | * Note that PTE bits in the PTE range besides the PFN can differ. For example, |
| 1176 | * some PTEs might be write-protected. |
| 1177 | * |
| 1178 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 1179 | * pages that belong to the same folio. The PTEs are all in the same PMD. |
| 1180 | * |
| 1181 | * Returns: whether any PTE was young. |
| 1182 | */ |
| 1183 | static inline bool test_and_clear_young_ptes(struct vm_area_struct *vma, |
| 1184 | unsigned long addr, pte_t *ptep, unsigned int nr) |
| 1185 | { |
| 1186 | bool young = false; |
| 1187 | |
| 1188 | for (;;) { |
| 1189 | young |= ptep_test_and_clear_young(vma, addr, ptep); |
| 1190 | if (--nr == 0) |
| 1191 | break; |
| 1192 | ptep++; |
| 1193 | addr += PAGE_SIZE; |
| 1194 | } |
| 1195 | |
| 1196 | return young; |
| 1197 | } |
| 1198 | #endif |
| 1199 | |
| 1200 | /* |
| 1201 | * On some architectures hardware does not set page access bit when accessing |
| 1202 | * memory page, it is responsibility of software setting this bit. It brings |
| 1203 | * out extra page fault penalty to track page access bit. For optimization page |
| 1204 | * access bit can be set during all page fault flow on these arches. |
| 1205 | * To be differentiate with macro pte_mkyoung, this macro is used on platforms |
| 1206 | * where software maintains page access bit. |
| 1207 | */ |
| 1208 | #ifndef pte_sw_mkyoung |
| 1209 | static inline pte_t pte_sw_mkyoung(pte_t pte) |
| 1210 | { |
| 1211 | return pte; |
| 1212 | } |
| 1213 | #define pte_sw_mkyoung pte_sw_mkyoung |
| 1214 | #endif |
| 1215 | |
| 1216 | #ifndef __HAVE_ARCH_PMDP_SET_WRPROTECT |
| 1217 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1218 | static inline void pmdp_set_wrprotect(struct mm_struct *mm, |
| 1219 | unsigned long address, pmd_t *pmdp) |
| 1220 | { |
| 1221 | pmd_t old_pmd = *pmdp; |
| 1222 | set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd)); |
| 1223 | } |
| 1224 | #else |
| 1225 | static inline void pmdp_set_wrprotect(struct mm_struct *mm, |
| 1226 | unsigned long address, pmd_t *pmdp) |
| 1227 | { |
| 1228 | BUILD_BUG(); |
| 1229 | } |
| 1230 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 1231 | #endif |
| 1232 | #ifndef __HAVE_ARCH_PUDP_SET_WRPROTECT |
| 1233 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 1234 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1235 | static inline void pudp_set_wrprotect(struct mm_struct *mm, |
| 1236 | unsigned long address, pud_t *pudp) |
| 1237 | { |
| 1238 | pud_t old_pud = *pudp; |
| 1239 | |
| 1240 | set_pud_at(mm, address, pudp, pud_wrprotect(old_pud)); |
| 1241 | } |
| 1242 | #else |
| 1243 | static inline void pudp_set_wrprotect(struct mm_struct *mm, |
| 1244 | unsigned long address, pud_t *pudp) |
| 1245 | { |
| 1246 | BUILD_BUG(); |
| 1247 | } |
| 1248 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 1249 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 1250 | #endif |
| 1251 | |
| 1252 | #ifndef pmdp_collapse_flush |
| 1253 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1254 | extern pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, |
| 1255 | unsigned long address, pmd_t *pmdp); |
| 1256 | #else |
| 1257 | static inline pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, |
| 1258 | unsigned long address, |
| 1259 | pmd_t *pmdp) |
| 1260 | { |
| 1261 | BUILD_BUG(); |
| 1262 | return *pmdp; |
| 1263 | } |
| 1264 | #define pmdp_collapse_flush pmdp_collapse_flush |
| 1265 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 1266 | #endif |
| 1267 | |
| 1268 | #ifndef __HAVE_ARCH_PGTABLE_DEPOSIT |
| 1269 | extern void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, |
| 1270 | pgtable_t pgtable); |
| 1271 | #endif |
| 1272 | |
| 1273 | #ifndef __HAVE_ARCH_PGTABLE_WITHDRAW |
| 1274 | extern pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp); |
| 1275 | #endif |
| 1276 | |
| 1277 | #ifndef arch_needs_pgtable_deposit |
| 1278 | #define arch_needs_pgtable_deposit() (false) |
| 1279 | #endif |
| 1280 | |
| 1281 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 1282 | /* |
| 1283 | * This is an implementation of pmdp_establish() that is only suitable for an |
| 1284 | * architecture that doesn't have hardware dirty/accessed bits. In this case we |
| 1285 | * can't race with CPU which sets these bits and non-atomic approach is fine. |
| 1286 | */ |
| 1287 | static inline pmd_t generic_pmdp_establish(struct vm_area_struct *vma, |
| 1288 | unsigned long address, pmd_t *pmdp, pmd_t pmd) |
| 1289 | { |
| 1290 | pmd_t old_pmd = *pmdp; |
| 1291 | set_pmd_at(vma->vm_mm, address, pmdp, pmd); |
| 1292 | return old_pmd; |
| 1293 | } |
| 1294 | #endif |
| 1295 | |
| 1296 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE |
| 1297 | extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, |
| 1298 | pmd_t *pmdp); |
| 1299 | #endif |
| 1300 | |
| 1301 | #ifndef __HAVE_ARCH_PMDP_INVALIDATE_AD |
| 1302 | |
| 1303 | /* |
| 1304 | * pmdp_invalidate_ad() invalidates the PMD while changing a transparent |
| 1305 | * hugepage mapping in the page tables. This function is similar to |
| 1306 | * pmdp_invalidate(), but should only be used if the access and dirty bits would |
| 1307 | * not be cleared by the software in the new PMD value. The function ensures |
| 1308 | * that hardware changes of the access and dirty bits updates would not be lost. |
| 1309 | * |
| 1310 | * Doing so can allow in certain architectures to avoid a TLB flush in most |
| 1311 | * cases. Yet, another TLB flush might be necessary later if the PMD update |
| 1312 | * itself requires such flush (e.g., if protection was set to be stricter). Yet, |
| 1313 | * even when a TLB flush is needed because of the update, the caller may be able |
| 1314 | * to batch these TLB flushing operations, so fewer TLB flush operations are |
| 1315 | * needed. |
| 1316 | */ |
| 1317 | extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, |
| 1318 | unsigned long address, pmd_t *pmdp); |
| 1319 | #endif |
| 1320 | |
| 1321 | #ifndef __HAVE_ARCH_PTE_SAME |
| 1322 | static inline int pte_same(pte_t pte_a, pte_t pte_b) |
| 1323 | { |
| 1324 | return pte_val(pte_a) == pte_val(pte_b); |
| 1325 | } |
| 1326 | #endif |
| 1327 | |
| 1328 | #ifndef __HAVE_ARCH_PTE_UNUSED |
| 1329 | /* |
| 1330 | * Some architectures provide facilities to virtualization guests |
| 1331 | * so that they can flag allocated pages as unused. This allows the |
| 1332 | * host to transparently reclaim unused pages. This function returns |
| 1333 | * whether the pte's page is unused. |
| 1334 | */ |
| 1335 | static inline int pte_unused(pte_t pte) |
| 1336 | { |
| 1337 | return 0; |
| 1338 | } |
| 1339 | #endif |
| 1340 | |
| 1341 | #ifndef pte_access_permitted |
| 1342 | #define pte_access_permitted(pte, write) \ |
| 1343 | (pte_present(pte) && (!(write) || pte_write(pte))) |
| 1344 | #endif |
| 1345 | |
| 1346 | #ifndef pmd_access_permitted |
| 1347 | #define pmd_access_permitted(pmd, write) \ |
| 1348 | (pmd_present(pmd) && (!(write) || pmd_write(pmd))) |
| 1349 | #endif |
| 1350 | |
| 1351 | #ifndef pud_access_permitted |
| 1352 | #define pud_access_permitted(pud, write) \ |
| 1353 | (pud_present(pud) && (!(write) || pud_write(pud))) |
| 1354 | #endif |
| 1355 | |
| 1356 | #ifndef p4d_access_permitted |
| 1357 | #define p4d_access_permitted(p4d, write) \ |
| 1358 | (p4d_present(p4d) && (!(write) || p4d_write(p4d))) |
| 1359 | #endif |
| 1360 | |
| 1361 | #ifndef pgd_access_permitted |
| 1362 | #define pgd_access_permitted(pgd, write) \ |
| 1363 | (pgd_present(pgd) && (!(write) || pgd_write(pgd))) |
| 1364 | #endif |
| 1365 | |
| 1366 | #ifndef __HAVE_ARCH_PMD_SAME |
| 1367 | static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b) |
| 1368 | { |
| 1369 | return pmd_val(pmd_a) == pmd_val(pmd_b); |
| 1370 | } |
| 1371 | #endif |
| 1372 | |
| 1373 | #ifndef pud_same |
| 1374 | static inline int pud_same(pud_t pud_a, pud_t pud_b) |
| 1375 | { |
| 1376 | return pud_val(pud_a) == pud_val(pud_b); |
| 1377 | } |
| 1378 | #define pud_same pud_same |
| 1379 | #endif |
| 1380 | |
| 1381 | #ifndef __HAVE_ARCH_P4D_SAME |
| 1382 | static inline int p4d_same(p4d_t p4d_a, p4d_t p4d_b) |
| 1383 | { |
| 1384 | return p4d_val(p4d_a) == p4d_val(p4d_b); |
| 1385 | } |
| 1386 | #endif |
| 1387 | |
| 1388 | #ifndef __HAVE_ARCH_PGD_SAME |
| 1389 | static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b) |
| 1390 | { |
| 1391 | return pgd_val(pgd_a) == pgd_val(pgd_b); |
| 1392 | } |
| 1393 | #endif |
| 1394 | |
| 1395 | #ifndef __HAVE_ARCH_DO_SWAP_PAGE |
| 1396 | static inline void arch_do_swap_page_nr(struct mm_struct *mm, |
| 1397 | struct vm_area_struct *vma, |
| 1398 | unsigned long addr, |
| 1399 | pte_t pte, pte_t oldpte, |
| 1400 | int nr) |
| 1401 | { |
| 1402 | |
| 1403 | } |
| 1404 | #else |
| 1405 | /* |
| 1406 | * Some architectures support metadata associated with a page. When a |
| 1407 | * page is being swapped out, this metadata must be saved so it can be |
| 1408 | * restored when the page is swapped back in. SPARC M7 and newer |
| 1409 | * processors support an ADI (Application Data Integrity) tag for the |
| 1410 | * page as metadata for the page. arch_do_swap_page() can restore this |
| 1411 | * metadata when a page is swapped back in. |
| 1412 | */ |
| 1413 | static inline void arch_do_swap_page_nr(struct mm_struct *mm, |
| 1414 | struct vm_area_struct *vma, |
| 1415 | unsigned long addr, |
| 1416 | pte_t pte, pte_t oldpte, |
| 1417 | int nr) |
| 1418 | { |
| 1419 | for (int i = 0; i < nr; i++) { |
| 1420 | arch_do_swap_page(vma->vm_mm, vma, addr + i * PAGE_SIZE, |
| 1421 | pte_advance_pfn(pte, i), |
| 1422 | pte_advance_pfn(oldpte, i)); |
| 1423 | } |
| 1424 | } |
| 1425 | #endif |
| 1426 | |
| 1427 | #ifndef __HAVE_ARCH_UNMAP_ONE |
| 1428 | /* |
| 1429 | * Some architectures support metadata associated with a page. When a |
| 1430 | * page is being swapped out, this metadata must be saved so it can be |
| 1431 | * restored when the page is swapped back in. SPARC M7 and newer |
| 1432 | * processors support an ADI (Application Data Integrity) tag for the |
| 1433 | * page as metadata for the page. arch_unmap_one() can save this |
| 1434 | * metadata on a swap-out of a page. |
| 1435 | */ |
| 1436 | static inline int arch_unmap_one(struct mm_struct *mm, |
| 1437 | struct vm_area_struct *vma, |
| 1438 | unsigned long addr, |
| 1439 | pte_t orig_pte) |
| 1440 | { |
| 1441 | return 0; |
| 1442 | } |
| 1443 | #endif |
| 1444 | |
| 1445 | /* |
| 1446 | * Allow architectures to preserve additional metadata associated with |
| 1447 | * swapped-out pages. The corresponding __HAVE_ARCH_SWAP_* macros and function |
| 1448 | * prototypes must be defined in the arch-specific asm/pgtable.h file. |
| 1449 | */ |
| 1450 | #ifndef __HAVE_ARCH_PREPARE_TO_SWAP |
| 1451 | static inline int arch_prepare_to_swap(struct folio *folio) |
| 1452 | { |
| 1453 | return 0; |
| 1454 | } |
| 1455 | #endif |
| 1456 | |
| 1457 | #ifndef __HAVE_ARCH_SWAP_INVALIDATE |
| 1458 | static inline void arch_swap_invalidate_page(int type, pgoff_t offset) |
| 1459 | { |
| 1460 | } |
| 1461 | |
| 1462 | static inline void arch_swap_invalidate_area(int type) |
| 1463 | { |
| 1464 | } |
| 1465 | #endif |
| 1466 | |
| 1467 | #ifndef __HAVE_ARCH_SWAP_RESTORE |
| 1468 | static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio) |
| 1469 | { |
| 1470 | } |
| 1471 | #endif |
| 1472 | |
| 1473 | #ifndef __HAVE_ARCH_MOVE_PTE |
| 1474 | #define move_pte(pte, old_addr, new_addr) (pte) |
| 1475 | #endif |
| 1476 | |
| 1477 | #ifndef pte_accessible |
| 1478 | # define pte_accessible(mm, pte) ((void)(pte), 1) |
| 1479 | #endif |
| 1480 | |
| 1481 | #ifndef flush_tlb_fix_spurious_fault |
| 1482 | #define flush_tlb_fix_spurious_fault(vma, address, ptep) flush_tlb_page(vma, address) |
| 1483 | #endif |
| 1484 | |
| 1485 | #ifndef flush_tlb_fix_spurious_fault_pmd |
| 1486 | #define flush_tlb_fix_spurious_fault_pmd(vma, address, pmdp) do { } while (0) |
| 1487 | #endif |
| 1488 | |
| 1489 | /* |
| 1490 | * When walking page tables, get the address of the next boundary, |
| 1491 | * or the end address of the range if that comes earlier. Although no |
| 1492 | * vma end wraps to 0, rounded up __boundary may wrap to 0 throughout. |
| 1493 | */ |
| 1494 | |
| 1495 | #define pgd_addr_end(addr, end) \ |
| 1496 | ({ unsigned long __boundary = ((addr) + PGDIR_SIZE) & PGDIR_MASK; \ |
| 1497 | (__boundary - 1 < (end) - 1)? __boundary: (end); \ |
| 1498 | }) |
| 1499 | |
| 1500 | #ifndef p4d_addr_end |
| 1501 | #define p4d_addr_end(addr, end) \ |
| 1502 | ({ unsigned long __boundary = ((addr) + P4D_SIZE) & P4D_MASK; \ |
| 1503 | (__boundary - 1 < (end) - 1)? __boundary: (end); \ |
| 1504 | }) |
| 1505 | #endif |
| 1506 | |
| 1507 | #ifndef pud_addr_end |
| 1508 | #define pud_addr_end(addr, end) \ |
| 1509 | ({ unsigned long __boundary = ((addr) + PUD_SIZE) & PUD_MASK; \ |
| 1510 | (__boundary - 1 < (end) - 1)? __boundary: (end); \ |
| 1511 | }) |
| 1512 | #endif |
| 1513 | |
| 1514 | #ifndef pmd_addr_end |
| 1515 | #define pmd_addr_end(addr, end) \ |
| 1516 | ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \ |
| 1517 | (__boundary - 1 < (end) - 1)? __boundary: (end); \ |
| 1518 | }) |
| 1519 | #endif |
| 1520 | |
| 1521 | /* |
| 1522 | * When walking page tables, we usually want to skip any p?d_none entries; |
| 1523 | * and any p?d_bad entries - reporting the error before resetting to none. |
| 1524 | * Do the tests inline, but report and clear the bad entry in mm/memory.c. |
| 1525 | */ |
| 1526 | void pgd_clear_bad(pgd_t *); |
| 1527 | |
| 1528 | #ifndef __PAGETABLE_P4D_FOLDED |
| 1529 | void p4d_clear_bad(p4d_t *); |
| 1530 | #else |
| 1531 | #define p4d_clear_bad(p4d) do { } while (0) |
| 1532 | #endif |
| 1533 | |
| 1534 | #ifndef __PAGETABLE_PUD_FOLDED |
| 1535 | void pud_clear_bad(pud_t *); |
| 1536 | #else |
| 1537 | #define pud_clear_bad(p4d) do { } while (0) |
| 1538 | #endif |
| 1539 | |
| 1540 | void pmd_clear_bad(pmd_t *); |
| 1541 | |
| 1542 | static inline int pgd_none_or_clear_bad(pgd_t *pgd) |
| 1543 | { |
| 1544 | if (pgd_none(*pgd)) |
| 1545 | return 1; |
| 1546 | if (unlikely(pgd_bad(*pgd))) { |
| 1547 | pgd_clear_bad(pgd); |
| 1548 | return 1; |
| 1549 | } |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | static inline int p4d_none_or_clear_bad(p4d_t *p4d) |
| 1554 | { |
| 1555 | if (p4d_none(*p4d)) |
| 1556 | return 1; |
| 1557 | if (unlikely(p4d_bad(*p4d))) { |
| 1558 | p4d_clear_bad(p4d); |
| 1559 | return 1; |
| 1560 | } |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static inline int pud_none_or_clear_bad(pud_t *pud) |
| 1565 | { |
| 1566 | if (pud_none(*pud)) |
| 1567 | return 1; |
| 1568 | if (unlikely(pud_bad(*pud))) { |
| 1569 | pud_clear_bad(pud); |
| 1570 | return 1; |
| 1571 | } |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | static inline int pmd_none_or_clear_bad(pmd_t *pmd) |
| 1576 | { |
| 1577 | if (pmd_none(*pmd)) |
| 1578 | return 1; |
| 1579 | if (unlikely(pmd_bad(*pmd))) { |
| 1580 | pmd_clear_bad(pmd); |
| 1581 | return 1; |
| 1582 | } |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | static inline pte_t __ptep_modify_prot_start(struct vm_area_struct *vma, |
| 1587 | unsigned long addr, |
| 1588 | pte_t *ptep) |
| 1589 | { |
| 1590 | /* |
| 1591 | * Get the current pte state, but zero it out to make it |
| 1592 | * non-present, preventing the hardware from asynchronously |
| 1593 | * updating it. |
| 1594 | */ |
| 1595 | return ptep_get_and_clear(vma->vm_mm, addr, ptep); |
| 1596 | } |
| 1597 | |
| 1598 | static inline void __ptep_modify_prot_commit(struct vm_area_struct *vma, |
| 1599 | unsigned long addr, |
| 1600 | pte_t *ptep, pte_t pte) |
| 1601 | { |
| 1602 | /* |
| 1603 | * The pte is non-present, so there's no hardware state to |
| 1604 | * preserve. |
| 1605 | */ |
| 1606 | set_pte_at(vma->vm_mm, addr, ptep, pte); |
| 1607 | } |
| 1608 | |
| 1609 | #ifndef __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION |
| 1610 | /* |
| 1611 | * Start a pte protection read-modify-write transaction, which |
| 1612 | * protects against asynchronous hardware modifications to the pte. |
| 1613 | * The intention is not to prevent the hardware from making pte |
| 1614 | * updates, but to prevent any updates it may make from being lost. |
| 1615 | * |
| 1616 | * This does not protect against other software modifications of the |
| 1617 | * pte; the appropriate pte lock must be held over the transaction. |
| 1618 | * |
| 1619 | * Note that this interface is intended to be batchable, meaning that |
| 1620 | * ptep_modify_prot_commit may not actually update the pte, but merely |
| 1621 | * queue the update to be done at some later time. The update must be |
| 1622 | * actually committed before the pte lock is released, however. |
| 1623 | */ |
| 1624 | static inline pte_t ptep_modify_prot_start(struct vm_area_struct *vma, |
| 1625 | unsigned long addr, |
| 1626 | pte_t *ptep) |
| 1627 | { |
| 1628 | return __ptep_modify_prot_start(vma, addr, ptep); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * Commit an update to a pte, leaving any hardware-controlled bits in |
| 1633 | * the PTE unmodified. The pte returned from ptep_modify_prot_start() may |
| 1634 | * additionally have young and/or dirty bits set where previously they were not, |
| 1635 | * so the updated pte may have these additional changes. |
| 1636 | */ |
| 1637 | static inline void ptep_modify_prot_commit(struct vm_area_struct *vma, |
| 1638 | unsigned long addr, |
| 1639 | pte_t *ptep, pte_t old_pte, pte_t pte) |
| 1640 | { |
| 1641 | __ptep_modify_prot_commit(vma, addr, ptep, pte); |
| 1642 | } |
| 1643 | #endif /* __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION */ |
| 1644 | |
| 1645 | /** |
| 1646 | * modify_prot_start_ptes - Start a pte protection read-modify-write transaction |
| 1647 | * over a batch of ptes, which protects against asynchronous hardware |
| 1648 | * modifications to the ptes. The intention is not to prevent the hardware from |
| 1649 | * making pte updates, but to prevent any updates it may make from being lost. |
| 1650 | * Please see the comment above ptep_modify_prot_start() for full description. |
| 1651 | * |
| 1652 | * @vma: The virtual memory area the pages are mapped into. |
| 1653 | * @addr: Address the first page is mapped at. |
| 1654 | * @ptep: Page table pointer for the first entry. |
| 1655 | * @nr: Number of entries. |
| 1656 | * |
| 1657 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 1658 | * loop over ptep_modify_prot_start(), collecting the a/d bits from each pte |
| 1659 | * in the batch. |
| 1660 | * |
| 1661 | * Note that PTE bits in the PTE batch besides the PFN can differ. |
| 1662 | * |
| 1663 | * Context: The caller holds the page table lock. The PTEs map consecutive |
| 1664 | * pages that belong to the same folio. All other PTE bits must be identical for |
| 1665 | * all PTEs in the batch except for young and dirty bits. The PTEs are all in |
| 1666 | * the same PMD. |
| 1667 | */ |
| 1668 | #ifndef modify_prot_start_ptes |
| 1669 | static inline pte_t modify_prot_start_ptes(struct vm_area_struct *vma, |
| 1670 | unsigned long addr, pte_t *ptep, unsigned int nr) |
| 1671 | { |
| 1672 | pte_t pte, tmp_pte; |
| 1673 | |
| 1674 | pte = ptep_modify_prot_start(vma, addr, ptep); |
| 1675 | while (--nr) { |
| 1676 | ptep++; |
| 1677 | addr += PAGE_SIZE; |
| 1678 | tmp_pte = ptep_modify_prot_start(vma, addr, ptep); |
| 1679 | if (pte_dirty(tmp_pte)) |
| 1680 | pte = pte_mkdirty(pte); |
| 1681 | if (pte_young(tmp_pte)) |
| 1682 | pte = pte_mkyoung(pte); |
| 1683 | } |
| 1684 | return pte; |
| 1685 | } |
| 1686 | #endif |
| 1687 | |
| 1688 | /** |
| 1689 | * modify_prot_commit_ptes - Commit an update to a batch of ptes, leaving any |
| 1690 | * hardware-controlled bits in the PTE unmodified. |
| 1691 | * |
| 1692 | * @vma: The virtual memory area the pages are mapped into. |
| 1693 | * @addr: Address the first page is mapped at. |
| 1694 | * @ptep: Page table pointer for the first entry. |
| 1695 | * @old_pte: Old page table entry (for the first entry) which is now cleared. |
| 1696 | * @pte: New page table entry to be set. |
| 1697 | * @nr: Number of entries. |
| 1698 | * |
| 1699 | * May be overridden by the architecture; otherwise, implemented as a simple |
| 1700 | * loop over ptep_modify_prot_commit(). |
| 1701 | * |
| 1702 | * Context: The caller holds the page table lock. The PTEs are all in the same |
| 1703 | * PMD. On exit, the set ptes in the batch map the same folio. The ptes set by |
| 1704 | * ptep_modify_prot_start() may additionally have young and/or dirty bits set |
| 1705 | * where previously they were not, so the updated ptes may have these |
| 1706 | * additional changes. |
| 1707 | */ |
| 1708 | #ifndef modify_prot_commit_ptes |
| 1709 | static inline void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr, |
| 1710 | pte_t *ptep, pte_t old_pte, pte_t pte, unsigned int nr) |
| 1711 | { |
| 1712 | int i; |
| 1713 | |
| 1714 | for (i = 0; i < nr; ++i, ++ptep, addr += PAGE_SIZE) { |
| 1715 | ptep_modify_prot_commit(vma, addr, ptep, old_pte, pte); |
| 1716 | |
| 1717 | /* Advance PFN only, set same prot */ |
| 1718 | old_pte = pte_next_pfn(old_pte); |
| 1719 | pte = pte_next_pfn(pte); |
| 1720 | } |
| 1721 | } |
| 1722 | #endif |
| 1723 | |
| 1724 | /* |
| 1725 | * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values |
| 1726 | * and let generic vmalloc, ioremap and page table update code know when |
| 1727 | * arch_sync_kernel_mappings() needs to be called. |
| 1728 | */ |
| 1729 | #ifndef ARCH_PAGE_TABLE_SYNC_MASK |
| 1730 | #define ARCH_PAGE_TABLE_SYNC_MASK 0 |
| 1731 | #endif |
| 1732 | |
| 1733 | /* |
| 1734 | * There is no default implementation for arch_sync_kernel_mappings(). It is |
| 1735 | * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK |
| 1736 | * is 0. |
| 1737 | */ |
| 1738 | void arch_sync_kernel_mappings(unsigned long start, unsigned long end); |
| 1739 | |
| 1740 | #endif /* CONFIG_MMU */ |
| 1741 | |
| 1742 | /* |
| 1743 | * On almost all architectures and configurations, 0 can be used as the |
| 1744 | * upper ceiling to free_pgtables(): on many architectures it has the same |
| 1745 | * effect as using TASK_SIZE. However, there is one configuration which |
| 1746 | * must impose a more careful limit, to avoid freeing kernel pgtables. |
| 1747 | */ |
| 1748 | #ifndef USER_PGTABLES_CEILING |
| 1749 | #define USER_PGTABLES_CEILING 0UL |
| 1750 | #endif |
| 1751 | |
| 1752 | /* |
| 1753 | * This defines the first usable user address. Platforms |
| 1754 | * can override its value with custom FIRST_USER_ADDRESS |
| 1755 | * defined in their respective <asm/pgtable.h>. |
| 1756 | */ |
| 1757 | #ifndef FIRST_USER_ADDRESS |
| 1758 | #define FIRST_USER_ADDRESS 0UL |
| 1759 | #endif |
| 1760 | |
| 1761 | /* |
| 1762 | * No-op macros that just return the current protection value. Defined here |
| 1763 | * because these macros can be used even if CONFIG_MMU is not defined. |
| 1764 | */ |
| 1765 | |
| 1766 | #ifndef pgprot_nx |
| 1767 | #define pgprot_nx(prot) (prot) |
| 1768 | #endif |
| 1769 | |
| 1770 | #ifndef pgprot_noncached |
| 1771 | #define pgprot_noncached(prot) (prot) |
| 1772 | #endif |
| 1773 | |
| 1774 | #ifndef pgprot_writecombine |
| 1775 | #define pgprot_writecombine pgprot_noncached |
| 1776 | #endif |
| 1777 | |
| 1778 | #ifndef pgprot_writethrough |
| 1779 | #define pgprot_writethrough pgprot_noncached |
| 1780 | #endif |
| 1781 | |
| 1782 | #ifndef pgprot_device |
| 1783 | #define pgprot_device pgprot_noncached |
| 1784 | #endif |
| 1785 | |
| 1786 | #ifndef pgprot_mhp |
| 1787 | #define pgprot_mhp(prot) (prot) |
| 1788 | #endif |
| 1789 | |
| 1790 | #ifdef CONFIG_MMU |
| 1791 | #ifndef pgprot_modify |
| 1792 | #define pgprot_modify pgprot_modify |
| 1793 | static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot) |
| 1794 | { |
| 1795 | if (pgprot_val(oldprot) == pgprot_val(pgprot_noncached(oldprot))) |
| 1796 | newprot = pgprot_noncached(newprot); |
| 1797 | if (pgprot_val(oldprot) == pgprot_val(pgprot_writecombine(oldprot))) |
| 1798 | newprot = pgprot_writecombine(newprot); |
| 1799 | if (pgprot_val(oldprot) == pgprot_val(pgprot_device(oldprot))) |
| 1800 | newprot = pgprot_device(newprot); |
| 1801 | return newprot; |
| 1802 | } |
| 1803 | #endif |
| 1804 | #endif /* CONFIG_MMU */ |
| 1805 | |
| 1806 | #ifndef pgprot_encrypted |
| 1807 | #define pgprot_encrypted(prot) (prot) |
| 1808 | #endif |
| 1809 | |
| 1810 | #ifndef pgprot_decrypted |
| 1811 | #define pgprot_decrypted(prot) (prot) |
| 1812 | #endif |
| 1813 | |
| 1814 | /* |
| 1815 | * A facility to provide batching of the reload of page tables and |
| 1816 | * other process state with the actual context switch code for |
| 1817 | * paravirtualized guests. By convention, only one of the batched |
| 1818 | * update (lazy) modes (CPU, MMU) should be active at any given time, |
| 1819 | * entry should never be nested, and entry and exits should always be |
| 1820 | * paired. This is for sanity of maintaining and reasoning about the |
| 1821 | * kernel code. In this case, the exit (end of the context switch) is |
| 1822 | * in architecture-specific code, and so doesn't need a generic |
| 1823 | * definition. |
| 1824 | */ |
| 1825 | #ifndef __HAVE_ARCH_START_CONTEXT_SWITCH |
| 1826 | #define arch_start_context_switch(prev) do {} while (0) |
| 1827 | #endif |
| 1828 | |
| 1829 | /* |
| 1830 | * Some platforms can customize the PTE soft-dirty bit making it unavailable |
| 1831 | * even if the architecture provides the resource. |
| 1832 | * Adding this API allows architectures to add their own checks for the |
| 1833 | * devices on which the kernel is running. |
| 1834 | * Note: When overriding it, please make sure the CONFIG_MEM_SOFT_DIRTY |
| 1835 | * is part of this macro. |
| 1836 | */ |
| 1837 | #ifndef pgtable_supports_soft_dirty |
| 1838 | #define pgtable_supports_soft_dirty() IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) |
| 1839 | #endif |
| 1840 | |
| 1841 | #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY |
| 1842 | #ifndef CONFIG_ARCH_HAS_PMD_SOFTLEAVES |
| 1843 | static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd) |
| 1844 | { |
| 1845 | return pmd; |
| 1846 | } |
| 1847 | |
| 1848 | static inline int pmd_swp_soft_dirty(pmd_t pmd) |
| 1849 | { |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) |
| 1854 | { |
| 1855 | return pmd; |
| 1856 | } |
| 1857 | #endif |
| 1858 | #else /* !CONFIG_HAVE_ARCH_SOFT_DIRTY */ |
| 1859 | static inline int pte_soft_dirty(pte_t pte) |
| 1860 | { |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | static inline int pmd_soft_dirty(pmd_t pmd) |
| 1865 | { |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | static inline pte_t pte_mksoft_dirty(pte_t pte) |
| 1870 | { |
| 1871 | return pte; |
| 1872 | } |
| 1873 | |
| 1874 | static inline pmd_t pmd_mksoft_dirty(pmd_t pmd) |
| 1875 | { |
| 1876 | return pmd; |
| 1877 | } |
| 1878 | |
| 1879 | static inline pte_t pte_clear_soft_dirty(pte_t pte) |
| 1880 | { |
| 1881 | return pte; |
| 1882 | } |
| 1883 | |
| 1884 | static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd) |
| 1885 | { |
| 1886 | return pmd; |
| 1887 | } |
| 1888 | |
| 1889 | static inline pte_t pte_swp_mksoft_dirty(pte_t pte) |
| 1890 | { |
| 1891 | return pte; |
| 1892 | } |
| 1893 | |
| 1894 | static inline int pte_swp_soft_dirty(pte_t pte) |
| 1895 | { |
| 1896 | return 0; |
| 1897 | } |
| 1898 | |
| 1899 | static inline pte_t pte_swp_clear_soft_dirty(pte_t pte) |
| 1900 | { |
| 1901 | return pte; |
| 1902 | } |
| 1903 | |
| 1904 | static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd) |
| 1905 | { |
| 1906 | return pmd; |
| 1907 | } |
| 1908 | |
| 1909 | static inline int pmd_swp_soft_dirty(pmd_t pmd) |
| 1910 | { |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) |
| 1915 | { |
| 1916 | return pmd; |
| 1917 | } |
| 1918 | #endif |
| 1919 | |
| 1920 | #ifndef __HAVE_PFNMAP_TRACKING |
| 1921 | /* |
| 1922 | * Interfaces that can be used by architecture code to keep track of |
| 1923 | * memory type of pfn mappings specified by the remap_pfn_range, |
| 1924 | * vmf_insert_pfn. |
| 1925 | */ |
| 1926 | |
| 1927 | static inline int pfnmap_setup_cachemode(unsigned long pfn, unsigned long size, |
| 1928 | pgprot_t *prot) |
| 1929 | { |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static inline int pfnmap_track(unsigned long pfn, unsigned long size, |
| 1934 | pgprot_t *prot) |
| 1935 | { |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | static inline void pfnmap_untrack(unsigned long pfn, unsigned long size) |
| 1940 | { |
| 1941 | } |
| 1942 | #else |
| 1943 | /** |
| 1944 | * pfnmap_setup_cachemode - setup the cachemode in the pgprot for a pfn range |
| 1945 | * @pfn: the start of the pfn range |
| 1946 | * @size: the size of the pfn range in bytes |
| 1947 | * @prot: the pgprot to modify |
| 1948 | * |
| 1949 | * Lookup the cachemode for the pfn range starting at @pfn with the size |
| 1950 | * @size and store it in @prot, leaving other data in @prot unchanged. |
| 1951 | * |
| 1952 | * This allows for a hardware implementation to have fine-grained control of |
| 1953 | * memory cache behavior at page level granularity. Without a hardware |
| 1954 | * implementation, this function does nothing. |
| 1955 | * |
| 1956 | * Currently there is only one implementation for this - x86 Page Attribute |
| 1957 | * Table (PAT). See Documentation/arch/x86/pat.rst for more details. |
| 1958 | * |
| 1959 | * This function can fail if the pfn range spans pfns that require differing |
| 1960 | * cachemodes. If the pfn range was previously verified to have a single |
| 1961 | * cachemode, it is sufficient to query only a single pfn. The assumption is |
| 1962 | * that this is the case for drivers using the vmf_insert_pfn*() interface. |
| 1963 | * |
| 1964 | * Returns 0 on success and -EINVAL on error. |
| 1965 | */ |
| 1966 | int pfnmap_setup_cachemode(unsigned long pfn, unsigned long size, |
| 1967 | pgprot_t *prot); |
| 1968 | |
| 1969 | /** |
| 1970 | * pfnmap_track - track a pfn range |
| 1971 | * @pfn: the start of the pfn range |
| 1972 | * @size: the size of the pfn range in bytes |
| 1973 | * @prot: the pgprot to track |
| 1974 | * |
| 1975 | * Requested the pfn range to be 'tracked' by a hardware implementation and |
| 1976 | * setup the cachemode in @prot similar to pfnmap_setup_cachemode(). |
| 1977 | * |
| 1978 | * This allows for fine-grained control of memory cache behaviour at page |
| 1979 | * level granularity. Tracking memory this way is persisted across VMA splits |
| 1980 | * (VMA merging does not apply for VM_PFNMAP). |
| 1981 | * |
| 1982 | * Currently, there is only one implementation for this - x86 Page Attribute |
| 1983 | * Table (PAT). See Documentation/arch/x86/pat.rst for more details. |
| 1984 | * |
| 1985 | * Returns 0 on success and -EINVAL on error. |
| 1986 | */ |
| 1987 | int pfnmap_track(unsigned long pfn, unsigned long size, pgprot_t *prot); |
| 1988 | |
| 1989 | /** |
| 1990 | * pfnmap_untrack - untrack a pfn range |
| 1991 | * @pfn: the start of the pfn range |
| 1992 | * @size: the size of the pfn range in bytes |
| 1993 | * |
| 1994 | * Untrack a pfn range previously tracked through pfnmap_track(). |
| 1995 | */ |
| 1996 | void pfnmap_untrack(unsigned long pfn, unsigned long size); |
| 1997 | #endif |
| 1998 | |
| 1999 | /** |
| 2000 | * pfnmap_setup_cachemode_pfn - setup the cachemode in the pgprot for a pfn |
| 2001 | * @pfn: the pfn |
| 2002 | * @prot: the pgprot to modify |
| 2003 | * |
| 2004 | * Lookup the cachemode for @pfn and store it in @prot, leaving other |
| 2005 | * data in @prot unchanged. |
| 2006 | * |
| 2007 | * See pfnmap_setup_cachemode() for details. |
| 2008 | */ |
| 2009 | static inline void pfnmap_setup_cachemode_pfn(unsigned long pfn, pgprot_t *prot) |
| 2010 | { |
| 2011 | pfnmap_setup_cachemode(pfn, PAGE_SIZE, prot); |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * ZERO_PAGE() is global shared page(s) that is always zero. It is used for |
| 2016 | * zero-mapped memory areas, CoW etc. |
| 2017 | * |
| 2018 | * On architectures that __HAVE_COLOR_ZERO_PAGE there are several such pages |
| 2019 | * for different ranges in the virtual address space. |
| 2020 | * |
| 2021 | * zero_page_pfn identifies the first (or the only) pfn for these pages. |
| 2022 | * |
| 2023 | * For architectures that don't __HAVE_COLOR_ZERO_PAGE the zero page lives in |
| 2024 | * empty_zero_page in BSS. |
| 2025 | */ |
| 2026 | void arch_setup_zero_pages(void); |
| 2027 | |
| 2028 | #ifdef __HAVE_COLOR_ZERO_PAGE |
| 2029 | static inline int is_zero_pfn(unsigned long pfn) |
| 2030 | { |
| 2031 | extern unsigned long zero_page_pfn; |
| 2032 | unsigned long offset_from_zero_pfn = pfn - zero_page_pfn; |
| 2033 | |
| 2034 | return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT); |
| 2035 | } |
| 2036 | |
| 2037 | #define zero_pfn(addr) page_to_pfn(ZERO_PAGE(addr)) |
| 2038 | |
| 2039 | #else |
| 2040 | static inline int is_zero_pfn(unsigned long pfn) |
| 2041 | { |
| 2042 | extern unsigned long zero_page_pfn; |
| 2043 | |
| 2044 | return pfn == zero_page_pfn; |
| 2045 | } |
| 2046 | |
| 2047 | static inline unsigned long zero_pfn(unsigned long addr) |
| 2048 | { |
| 2049 | extern unsigned long zero_page_pfn; |
| 2050 | |
| 2051 | return zero_page_pfn; |
| 2052 | } |
| 2053 | |
| 2054 | extern const uint8_t empty_zero_page[PAGE_SIZE]; |
| 2055 | extern struct page *__zero_page; |
| 2056 | |
| 2057 | static inline struct page *_zero_page(unsigned long addr) |
| 2058 | { |
| 2059 | return __zero_page; |
| 2060 | } |
| 2061 | #define ZERO_PAGE(vaddr) _zero_page(vaddr) |
| 2062 | |
| 2063 | #endif /* __HAVE_COLOR_ZERO_PAGE */ |
| 2064 | |
| 2065 | #ifdef CONFIG_MMU |
| 2066 | |
| 2067 | #ifndef CONFIG_TRANSPARENT_HUGEPAGE |
| 2068 | static inline int pmd_trans_huge(pmd_t pmd) |
| 2069 | { |
| 2070 | return 0; |
| 2071 | } |
| 2072 | #ifndef pmd_write |
| 2073 | static inline int pmd_write(pmd_t pmd) |
| 2074 | { |
| 2075 | BUG(); |
| 2076 | return 0; |
| 2077 | } |
| 2078 | #endif /* pmd_write */ |
| 2079 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 2080 | |
| 2081 | #ifndef pud_write |
| 2082 | static inline int pud_write(pud_t pud) |
| 2083 | { |
| 2084 | BUG(); |
| 2085 | return 0; |
| 2086 | } |
| 2087 | #endif /* pud_write */ |
| 2088 | |
| 2089 | #if !defined(CONFIG_TRANSPARENT_HUGEPAGE) || \ |
| 2090 | !defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) |
| 2091 | static inline int pud_trans_huge(pud_t pud) |
| 2092 | { |
| 2093 | return 0; |
| 2094 | } |
| 2095 | #endif |
| 2096 | |
| 2097 | static inline int pud_trans_unstable(pud_t *pud) |
| 2098 | { |
| 2099 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ |
| 2100 | defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) |
| 2101 | pud_t pudval = pudp_get(pud); |
| 2102 | |
| 2103 | if (pud_none(pudval) || pud_trans_huge(pudval)) |
| 2104 | return 1; |
| 2105 | if (unlikely(pud_bad(pudval))) { |
| 2106 | pud_clear_bad(pud); |
| 2107 | return 1; |
| 2108 | } |
| 2109 | #endif |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | #ifndef CONFIG_ARCH_HAS_PTE_PROTNONE |
| 2114 | /* |
| 2115 | * In an inaccessible (PROT_NONE) VMA, pte_protnone() may indicate "yes". It |
| 2116 | * is perfectly valid to indicate "no" in that case, which is why our |
| 2117 | * default implementation defaults to "always no". |
| 2118 | * |
| 2119 | * In an accessible VMA, pte_protnone() reliably indicates a present |
| 2120 | * PROT_NONE page protection. Today the kernel uses such PTEs for two |
| 2121 | * purposes: NUMA hinting faults, and userfaultfd RWP tracking on |
| 2122 | * VM_UFFD_RWP VMAs. The two are distinguished by the uffd PTE bit and |
| 2123 | * the VMA flag; see include/linux/userfaultfd_k.h. |
| 2124 | * |
| 2125 | * So, to reliably identify PROT_NONE PTEs that require kernel handling, |
| 2126 | * looking at the VMA accessibility (and the uffd bit on RWP VMAs) is |
| 2127 | * sufficient. |
| 2128 | * |
| 2129 | * Architectures without CONFIG_ARCH_HAS_PTE_PROTNONE get the always-zero |
| 2130 | * stubs below; PAGE_NONE references that survive to runtime fire the |
| 2131 | * BUILD_BUG() fallback, since callers should have folded such paths to |
| 2132 | * dead code via IS_ENABLED(CONFIG_ARCH_HAS_PTE_PROTNONE). |
| 2133 | */ |
| 2134 | static inline int pte_protnone(pte_t pte) |
| 2135 | { |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | static inline int pmd_protnone(pmd_t pmd) |
| 2140 | { |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | #ifndef PAGE_NONE |
| 2145 | #define PAGE_NONE ({ BUILD_BUG(); (pgprot_t){0}; }) |
| 2146 | #endif |
| 2147 | #endif /* CONFIG_ARCH_HAS_PTE_PROTNONE */ |
| 2148 | |
| 2149 | #endif /* CONFIG_MMU */ |
| 2150 | |
| 2151 | #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP |
| 2152 | |
| 2153 | #ifndef __PAGETABLE_P4D_FOLDED |
| 2154 | int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot); |
| 2155 | void p4d_clear_huge(p4d_t *p4d); |
| 2156 | #else |
| 2157 | static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot) |
| 2158 | { |
| 2159 | return 0; |
| 2160 | } |
| 2161 | static inline void p4d_clear_huge(p4d_t *p4d) { } |
| 2162 | #endif /* !__PAGETABLE_P4D_FOLDED */ |
| 2163 | |
| 2164 | int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot); |
| 2165 | int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot); |
| 2166 | int pud_clear_huge(pud_t *pud); |
| 2167 | int pmd_clear_huge(pmd_t *pmd); |
| 2168 | int p4d_free_pud_page(p4d_t *p4d, unsigned long addr); |
| 2169 | int pud_free_pmd_page(pud_t *pud, unsigned long addr); |
| 2170 | int pmd_free_pte_page(pmd_t *pmd, unsigned long addr); |
| 2171 | #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ |
| 2172 | static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot) |
| 2173 | { |
| 2174 | return 0; |
| 2175 | } |
| 2176 | static inline int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot) |
| 2177 | { |
| 2178 | return 0; |
| 2179 | } |
| 2180 | static inline int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot) |
| 2181 | { |
| 2182 | return 0; |
| 2183 | } |
| 2184 | static inline void p4d_clear_huge(p4d_t *p4d) { } |
| 2185 | static inline int pud_clear_huge(pud_t *pud) |
| 2186 | { |
| 2187 | return 0; |
| 2188 | } |
| 2189 | static inline int pmd_clear_huge(pmd_t *pmd) |
| 2190 | { |
| 2191 | return 0; |
| 2192 | } |
| 2193 | static inline int p4d_free_pud_page(p4d_t *p4d, unsigned long addr) |
| 2194 | { |
| 2195 | return 0; |
| 2196 | } |
| 2197 | static inline int pud_free_pmd_page(pud_t *pud, unsigned long addr) |
| 2198 | { |
| 2199 | return 0; |
| 2200 | } |
| 2201 | static inline int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) |
| 2202 | { |
| 2203 | return 0; |
| 2204 | } |
| 2205 | #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ |
| 2206 | |
| 2207 | #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE |
| 2208 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 2209 | /* |
| 2210 | * ARCHes with special requirements for evicting THP backing TLB entries can |
| 2211 | * implement this. Otherwise also, it can help optimize normal TLB flush in |
| 2212 | * THP regime. Stock flush_tlb_range() typically has optimization to nuke the |
| 2213 | * entire TLB if flush span is greater than a threshold, which will |
| 2214 | * likely be true for a single huge page. Thus a single THP flush will |
| 2215 | * invalidate the entire TLB which is not desirable. |
| 2216 | * e.g. see arch/arc: flush_pmd_tlb_range |
| 2217 | */ |
| 2218 | #define flush_pmd_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end) |
| 2219 | #define flush_pud_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end) |
| 2220 | #else |
| 2221 | #define flush_pmd_tlb_range(vma, addr, end) BUILD_BUG() |
| 2222 | #define flush_pud_tlb_range(vma, addr, end) BUILD_BUG() |
| 2223 | #endif |
| 2224 | #endif |
| 2225 | |
| 2226 | struct file; |
| 2227 | int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn, |
| 2228 | unsigned long size, pgprot_t *vma_prot); |
| 2229 | |
| 2230 | #ifndef CONFIG_X86_ESPFIX64 |
| 2231 | static inline void init_espfix_bsp(void) { } |
| 2232 | #endif |
| 2233 | |
| 2234 | extern void __init pgtable_cache_init(void); |
| 2235 | |
| 2236 | #ifndef __HAVE_ARCH_PFN_MODIFY_ALLOWED |
| 2237 | static inline bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot) |
| 2238 | { |
| 2239 | return true; |
| 2240 | } |
| 2241 | |
| 2242 | static inline bool arch_has_pfn_modify_check(void) |
| 2243 | { |
| 2244 | return false; |
| 2245 | } |
| 2246 | #endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */ |
| 2247 | |
| 2248 | /* |
| 2249 | * Architecture PAGE_KERNEL_* fallbacks |
| 2250 | * |
| 2251 | * Some architectures don't define certain PAGE_KERNEL_* flags. This is either |
| 2252 | * because they really don't support them, or the port needs to be updated to |
| 2253 | * reflect the required functionality. Below are a set of relatively safe |
| 2254 | * fallbacks, as best effort, which we can count on in lieu of the architectures |
| 2255 | * not defining them on their own yet. |
| 2256 | */ |
| 2257 | |
| 2258 | #ifndef PAGE_KERNEL_RO |
| 2259 | # define PAGE_KERNEL_RO PAGE_KERNEL |
| 2260 | #endif |
| 2261 | |
| 2262 | #ifndef PAGE_KERNEL_EXEC |
| 2263 | # define PAGE_KERNEL_EXEC PAGE_KERNEL |
| 2264 | #endif |
| 2265 | |
| 2266 | /* |
| 2267 | * Page Table Modification bits for pgtbl_mod_mask. |
| 2268 | * |
| 2269 | * These are used by the p?d_alloc_track*() and p*d_populate_kernel() |
| 2270 | * functions in the generic vmalloc, ioremap and page table update code |
| 2271 | * to track at which page-table levels entries have been modified. |
| 2272 | * Based on that the code can better decide when page table changes need |
| 2273 | * to be synchronized to other page-tables in the system. |
| 2274 | */ |
| 2275 | #define __PGTBL_PGD_MODIFIED 0 |
| 2276 | #define __PGTBL_P4D_MODIFIED 1 |
| 2277 | #define __PGTBL_PUD_MODIFIED 2 |
| 2278 | #define __PGTBL_PMD_MODIFIED 3 |
| 2279 | #define __PGTBL_PTE_MODIFIED 4 |
| 2280 | |
| 2281 | #define PGTBL_PGD_MODIFIED BIT(__PGTBL_PGD_MODIFIED) |
| 2282 | #define PGTBL_P4D_MODIFIED BIT(__PGTBL_P4D_MODIFIED) |
| 2283 | #define PGTBL_PUD_MODIFIED BIT(__PGTBL_PUD_MODIFIED) |
| 2284 | #define PGTBL_PMD_MODIFIED BIT(__PGTBL_PMD_MODIFIED) |
| 2285 | #define PGTBL_PTE_MODIFIED BIT(__PGTBL_PTE_MODIFIED) |
| 2286 | |
| 2287 | /* Page-Table Modification Mask */ |
| 2288 | typedef unsigned int pgtbl_mod_mask; |
| 2289 | |
| 2290 | enum pgtable_level { |
| 2291 | PGTABLE_LEVEL_PTE = 0, |
| 2292 | PGTABLE_LEVEL_PMD, |
| 2293 | PGTABLE_LEVEL_PUD, |
| 2294 | PGTABLE_LEVEL_P4D, |
| 2295 | PGTABLE_LEVEL_PGD, |
| 2296 | }; |
| 2297 | |
| 2298 | static inline const char *pgtable_level_to_str(enum pgtable_level level) |
| 2299 | { |
| 2300 | switch (level) { |
| 2301 | case PGTABLE_LEVEL_PTE: |
| 2302 | return "pte"; |
| 2303 | case PGTABLE_LEVEL_PMD: |
| 2304 | return "pmd"; |
| 2305 | case PGTABLE_LEVEL_PUD: |
| 2306 | return "pud"; |
| 2307 | case PGTABLE_LEVEL_P4D: |
| 2308 | return "p4d"; |
| 2309 | case PGTABLE_LEVEL_PGD: |
| 2310 | return "pgd"; |
| 2311 | default: |
| 2312 | return "unknown"; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | #endif /* !__ASSEMBLER__ */ |
| 2317 | |
| 2318 | #if !defined(MAX_POSSIBLE_PHYSMEM_BITS) && !defined(CONFIG_64BIT) |
| 2319 | #ifdef CONFIG_PHYS_ADDR_T_64BIT |
| 2320 | /* |
| 2321 | * ZSMALLOC needs to know the highest PFN on 32-bit architectures |
| 2322 | * with physical address space extension, but falls back to |
| 2323 | * BITS_PER_LONG otherwise. |
| 2324 | */ |
| 2325 | #error Missing MAX_POSSIBLE_PHYSMEM_BITS definition |
| 2326 | #else |
| 2327 | #define MAX_POSSIBLE_PHYSMEM_BITS 32 |
| 2328 | #endif |
| 2329 | #endif |
| 2330 | |
| 2331 | #ifndef has_transparent_hugepage |
| 2332 | #define has_transparent_hugepage() IS_BUILTIN(CONFIG_TRANSPARENT_HUGEPAGE) |
| 2333 | #endif |
| 2334 | |
| 2335 | #ifndef has_transparent_pud_hugepage |
| 2336 | #define has_transparent_pud_hugepage() IS_BUILTIN(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) |
| 2337 | #endif |
| 2338 | /* |
| 2339 | * On some architectures it depends on the mm if the p4d/pud or pmd |
| 2340 | * layer of the page table hierarchy is folded or not. |
| 2341 | */ |
| 2342 | #ifndef mm_p4d_folded |
| 2343 | #define mm_p4d_folded(mm) __is_defined(__PAGETABLE_P4D_FOLDED) |
| 2344 | #endif |
| 2345 | |
| 2346 | #ifndef mm_pud_folded |
| 2347 | #define mm_pud_folded(mm) __is_defined(__PAGETABLE_PUD_FOLDED) |
| 2348 | #endif |
| 2349 | |
| 2350 | #ifndef mm_pmd_folded |
| 2351 | #define mm_pmd_folded(mm) __is_defined(__PAGETABLE_PMD_FOLDED) |
| 2352 | #endif |
| 2353 | |
| 2354 | #ifndef p4d_offset_lockless |
| 2355 | #define p4d_offset_lockless(pgdp, pgd, address) p4d_offset(&(pgd), address) |
| 2356 | #endif |
| 2357 | #ifndef pud_offset_lockless |
| 2358 | #define pud_offset_lockless(p4dp, p4d, address) pud_offset(&(p4d), address) |
| 2359 | #endif |
| 2360 | #ifndef pmd_offset_lockless |
| 2361 | #define pmd_offset_lockless(pudp, pud, address) pmd_offset(&(pud), address) |
| 2362 | #endif |
| 2363 | |
| 2364 | /* |
| 2365 | * pXd_leaf() is the API to check whether a pgtable entry is a huge page |
| 2366 | * mapping. It should work globally across all archs, without any |
| 2367 | * dependency on CONFIG_* options. For architectures that do not support |
| 2368 | * huge mappings on specific levels, below fallbacks will be used. |
| 2369 | * |
| 2370 | * A leaf pgtable entry should always imply the following: |
| 2371 | * |
| 2372 | * - It is a "present" entry. IOW, before using this API, please check it |
| 2373 | * with pXd_present() first. NOTE: it may not always mean the "present |
| 2374 | * bit" is set. For example, PROT_NONE entries are always "present". |
| 2375 | * |
| 2376 | * - It should _never_ be a swap entry of any type. Above "present" check |
| 2377 | * should have guarded this, but let's be crystal clear on this. |
| 2378 | * |
| 2379 | * - It should contain a huge PFN, which points to a huge page larger than |
| 2380 | * PAGE_SIZE of the platform. The PFN format isn't important here. |
| 2381 | * |
| 2382 | * - It should cover all kinds of huge mappings (i.e. pXd_trans_huge() |
| 2383 | * or hugetlb mappings). |
| 2384 | */ |
| 2385 | #ifndef pgd_leaf |
| 2386 | #define pgd_leaf(x) false |
| 2387 | #endif |
| 2388 | #ifndef p4d_leaf |
| 2389 | #define p4d_leaf(x) false |
| 2390 | #endif |
| 2391 | #ifndef pud_leaf |
| 2392 | #define pud_leaf(x) false |
| 2393 | #endif |
| 2394 | #ifndef pmd_leaf |
| 2395 | #define pmd_leaf(x) false |
| 2396 | #endif |
| 2397 | |
| 2398 | #ifndef pgd_leaf_size |
| 2399 | #define pgd_leaf_size(x) (1ULL << PGDIR_SHIFT) |
| 2400 | #endif |
| 2401 | #ifndef p4d_leaf_size |
| 2402 | #define p4d_leaf_size(x) P4D_SIZE |
| 2403 | #endif |
| 2404 | #ifndef pud_leaf_size |
| 2405 | #define pud_leaf_size(x) PUD_SIZE |
| 2406 | #endif |
| 2407 | #ifndef pmd_leaf_size |
| 2408 | #define pmd_leaf_size(x) PMD_SIZE |
| 2409 | #endif |
| 2410 | #ifndef __pte_leaf_size |
| 2411 | #ifndef pte_leaf_size |
| 2412 | #define pte_leaf_size(x) PAGE_SIZE |
| 2413 | #endif |
| 2414 | #define __pte_leaf_size(x,y) pte_leaf_size(y) |
| 2415 | #endif |
| 2416 | |
| 2417 | /* |
| 2418 | * We always define pmd_pfn for all archs as it's used in lots of generic |
| 2419 | * code. Now it happens too for pud_pfn (and can happen for larger |
| 2420 | * mappings too in the future; we're not there yet). Instead of defining |
| 2421 | * it for all archs (like pmd_pfn), provide a fallback. |
| 2422 | * |
| 2423 | * Note that returning 0 here means any arch that didn't define this can |
| 2424 | * get severely wrong when it hits a real pud leaf. It's arch's |
| 2425 | * responsibility to properly define it when a huge pud is possible. |
| 2426 | */ |
| 2427 | #ifndef pud_pfn |
| 2428 | #define pud_pfn(x) 0 |
| 2429 | #endif |
| 2430 | |
| 2431 | /* |
| 2432 | * Some architectures have MMUs that are configurable or selectable at boot |
| 2433 | * time. These lead to variable PTRS_PER_x. For statically allocated arrays it |
| 2434 | * helps to have a static maximum value. |
| 2435 | */ |
| 2436 | |
| 2437 | #ifndef MAX_PTRS_PER_PTE |
| 2438 | #define MAX_PTRS_PER_PTE PTRS_PER_PTE |
| 2439 | #endif |
| 2440 | |
| 2441 | #ifndef MAX_PTRS_PER_PMD |
| 2442 | #define MAX_PTRS_PER_PMD PTRS_PER_PMD |
| 2443 | #endif |
| 2444 | |
| 2445 | #ifndef MAX_PTRS_PER_PUD |
| 2446 | #define MAX_PTRS_PER_PUD PTRS_PER_PUD |
| 2447 | #endif |
| 2448 | |
| 2449 | #ifndef MAX_PTRS_PER_P4D |
| 2450 | #define MAX_PTRS_PER_P4D PTRS_PER_P4D |
| 2451 | #endif |
| 2452 | |
| 2453 | #ifndef pte_pgprot |
| 2454 | #define pte_pgprot(x) ((pgprot_t) {0}) |
| 2455 | #endif |
| 2456 | |
| 2457 | #ifndef pmd_pgprot |
| 2458 | #define pmd_pgprot(x) ((pgprot_t) {0}) |
| 2459 | #endif |
| 2460 | |
| 2461 | #ifndef pud_pgprot |
| 2462 | #define pud_pgprot(x) ((pgprot_t) {0}) |
| 2463 | #endif |
| 2464 | |
| 2465 | /* description of effects of mapping type and prot in current implementation. |
| 2466 | * this is due to the limited x86 page protection hardware. The expected |
| 2467 | * behavior is in parens: |
| 2468 | * |
| 2469 | * map_type prot |
| 2470 | * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC |
| 2471 | * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes |
| 2472 | * w: (no) no w: (no) no w: (yes) yes w: (no) no |
| 2473 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes |
| 2474 | * |
| 2475 | * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes |
| 2476 | * w: (no) no w: (no) no w: (copy) copy w: (no) no |
| 2477 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes |
| 2478 | * |
| 2479 | * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and |
| 2480 | * MAP_PRIVATE (with Enhanced PAN supported): |
| 2481 | * r: (no) no |
| 2482 | * w: (no) no |
| 2483 | * x: (yes) yes |
| 2484 | */ |
| 2485 | #define DECLARE_VM_GET_PAGE_PROT \ |
| 2486 | pgprot_t vm_get_page_prot(vm_flags_t vm_flags) \ |
| 2487 | { \ |
| 2488 | return protection_map[vm_flags & \ |
| 2489 | (VM_READ | VM_WRITE | VM_EXEC | VM_SHARED)]; \ |
| 2490 | } \ |
| 2491 | EXPORT_SYMBOL(vm_get_page_prot); |
| 2492 | |
| 2493 | #endif /* _LINUX_PGTABLE_H */ |