^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __ASM_GENERIC_PGALLOC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __ASM_GENERIC_PGALLOC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define GFP_PGTABLE_KERNEL (GFP_KERNEL | __GFP_ZERO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define GFP_PGTABLE_USER (GFP_PGTABLE_KERNEL | __GFP_ACCOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * __pte_alloc_one_kernel - allocate a page for PTE-level kernel page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * This function is intended for architectures that need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * anything beyond simple page allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Return: pointer to the allocated memory or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static inline pte_t *__pte_alloc_one_kernel(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return (pte_t *)__get_free_page(GFP_PGTABLE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifndef __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * pte_alloc_one_kernel - allocate a page for PTE-level kernel page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Return: pointer to the allocated memory or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return __pte_alloc_one_kernel(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * pte_free_kernel - free PTE-level kernel page table page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @pte: pointer to the memory containing the page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) free_page((unsigned long)pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * __pte_alloc_one - allocate a page for PTE-level user page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @gfp: GFP flags to use for the allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Allocates a page and runs the pgtable_pte_page_ctor().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * This function is intended for architectures that need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * anything beyond simple page allocation or must have custom GFP flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Return: `struct page` initialized as page table or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static inline pgtable_t __pte_alloc_one(struct mm_struct *mm, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct page *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pte = alloc_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!pgtable_pte_page_ctor(pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) __free_page(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifndef __HAVE_ARCH_PTE_ALLOC_ONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * pte_alloc_one - allocate a page for PTE-level user page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Allocates a page and runs the pgtable_pte_page_ctor().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Return: `struct page` initialized as page table or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return __pte_alloc_one(mm, GFP_PGTABLE_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Should really implement gc for free page table pages. This could be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * done with a reference count in struct page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * pte_free - free PTE-level user page table page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @pte_page: the `struct page` representing the page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static inline void pte_free(struct mm_struct *mm, struct page *pte_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) pgtable_pte_page_dtor(pte_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __free_page(pte_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #if CONFIG_PGTABLE_LEVELS > 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #ifndef __HAVE_ARCH_PMD_ALLOC_ONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * pmd_alloc_one - allocate a page for PMD-level page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Allocates a page and runs the pgtable_pmd_page_ctor().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Allocations use %GFP_PGTABLE_USER in user context and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * %GFP_PGTABLE_KERNEL in kernel context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Return: pointer to the allocated memory or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) gfp_t gfp = GFP_PGTABLE_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (mm == &init_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) gfp = GFP_PGTABLE_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) page = alloc_pages(gfp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!pgtable_pmd_page_ctor(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) __free_pages(page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return (pmd_t *)page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #ifndef __HAVE_ARCH_PMD_FREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pgtable_pmd_page_dtor(virt_to_page(pmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) free_page((unsigned long)pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #endif /* CONFIG_PGTABLE_LEVELS > 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #if CONFIG_PGTABLE_LEVELS > 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #ifndef __HAVE_ARCH_PUD_ALLOC_ONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * pud_alloc_one - allocate a page for PUD-level page table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @mm: the mm_struct of the current context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Allocates a page using %GFP_PGTABLE_USER for user context and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * %GFP_PGTABLE_KERNEL for kernel context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Return: pointer to the allocated memory or %NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) gfp_t gfp = GFP_PGTABLE_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (mm == &init_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) gfp = GFP_PGTABLE_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return (pud_t *)get_zeroed_page(gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static inline void pud_free(struct mm_struct *mm, pud_t *pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) free_page((unsigned long)pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #endif /* CONFIG_PGTABLE_LEVELS > 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #ifndef __HAVE_ARCH_PGD_FREE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) free_page((unsigned long)pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #endif /* CONFIG_MMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #endif /* __ASM_GENERIC_PGALLOC_H */