Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / asm-generic / pgalloc.h
blob7c48f5fbf8aa79b088d21ed2b918c5804988186c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_GENERIC_PGALLOC_H
3 #define __ASM_GENERIC_PGALLOC_H
5 #ifdef CONFIG_MMU
7 #define GFP_PGTABLE_KERNEL (GFP_KERNEL | __GFP_ZERO)
8 #define GFP_PGTABLE_USER (GFP_PGTABLE_KERNEL | __GFP_ACCOUNT)
10 /**
11 * __pte_alloc_one_kernel - allocate memory for a PTE-level kernel page table
12 * @mm: the mm_struct of the current context
14 * This function is intended for architectures that need
15 * anything beyond simple page allocation.
17 * Return: pointer to the allocated memory or %NULL on error
19 static inline pte_t *__pte_alloc_one_kernel_noprof(struct mm_struct *mm)
21 struct ptdesc *ptdesc = pagetable_alloc_noprof(GFP_PGTABLE_KERNEL &
22 ~__GFP_HIGHMEM, 0);
24 if (!ptdesc)
25 return NULL;
26 return ptdesc_address(ptdesc);
28 #define __pte_alloc_one_kernel(...) alloc_hooks(__pte_alloc_one_kernel_noprof(__VA_ARGS__))
30 #ifndef __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL
31 /**
32 * pte_alloc_one_kernel - allocate memory for a PTE-level kernel page table
33 * @mm: the mm_struct of the current context
35 * Return: pointer to the allocated memory or %NULL on error
37 static inline pte_t *pte_alloc_one_kernel_noprof(struct mm_struct *mm)
39 return __pte_alloc_one_kernel_noprof(mm);
41 #define pte_alloc_one_kernel(...) alloc_hooks(pte_alloc_one_kernel_noprof(__VA_ARGS__))
42 #endif
44 /**
45 * pte_free_kernel - free PTE-level kernel page table memory
46 * @mm: the mm_struct of the current context
47 * @pte: pointer to the memory containing the page table
49 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
51 pagetable_free(virt_to_ptdesc(pte));
54 /**
55 * __pte_alloc_one - allocate memory for a PTE-level user page table
56 * @mm: the mm_struct of the current context
57 * @gfp: GFP flags to use for the allocation
59 * Allocate memory for a page table and ptdesc and runs pagetable_pte_ctor().
61 * This function is intended for architectures that need
62 * anything beyond simple page allocation or must have custom GFP flags.
64 * Return: `struct page` referencing the ptdesc or %NULL on error
66 static inline pgtable_t __pte_alloc_one_noprof(struct mm_struct *mm, gfp_t gfp)
68 struct ptdesc *ptdesc;
70 ptdesc = pagetable_alloc_noprof(gfp, 0);
71 if (!ptdesc)
72 return NULL;
73 if (!pagetable_pte_ctor(ptdesc)) {
74 pagetable_free(ptdesc);
75 return NULL;
78 return ptdesc_page(ptdesc);
80 #define __pte_alloc_one(...) alloc_hooks(__pte_alloc_one_noprof(__VA_ARGS__))
82 #ifndef __HAVE_ARCH_PTE_ALLOC_ONE
83 /**
84 * pte_alloc_one - allocate a page for PTE-level user page table
85 * @mm: the mm_struct of the current context
87 * Allocate memory for a page table and ptdesc and runs pagetable_pte_ctor().
89 * Return: `struct page` referencing the ptdesc or %NULL on error
91 static inline pgtable_t pte_alloc_one_noprof(struct mm_struct *mm)
93 return __pte_alloc_one_noprof(mm, GFP_PGTABLE_USER);
95 #define pte_alloc_one(...) alloc_hooks(pte_alloc_one_noprof(__VA_ARGS__))
96 #endif
99 * Should really implement gc for free page table pages. This could be
100 * done with a reference count in struct page.
104 * pte_free - free PTE-level user page table memory
105 * @mm: the mm_struct of the current context
106 * @pte_page: the `struct page` referencing the ptdesc
108 static inline void pte_free(struct mm_struct *mm, struct page *pte_page)
110 struct ptdesc *ptdesc = page_ptdesc(pte_page);
112 pagetable_pte_dtor(ptdesc);
113 pagetable_free(ptdesc);
117 #if CONFIG_PGTABLE_LEVELS > 2
119 #ifndef __HAVE_ARCH_PMD_ALLOC_ONE
121 * pmd_alloc_one - allocate memory for a PMD-level page table
122 * @mm: the mm_struct of the current context
124 * Allocate memory for a page table and ptdesc and runs pagetable_pmd_ctor().
126 * Allocations use %GFP_PGTABLE_USER in user context and
127 * %GFP_PGTABLE_KERNEL in kernel context.
129 * Return: pointer to the allocated memory or %NULL on error
131 static inline pmd_t *pmd_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
133 struct ptdesc *ptdesc;
134 gfp_t gfp = GFP_PGTABLE_USER;
136 if (mm == &init_mm)
137 gfp = GFP_PGTABLE_KERNEL;
138 ptdesc = pagetable_alloc_noprof(gfp, 0);
139 if (!ptdesc)
140 return NULL;
141 if (!pagetable_pmd_ctor(ptdesc)) {
142 pagetable_free(ptdesc);
143 return NULL;
145 return ptdesc_address(ptdesc);
147 #define pmd_alloc_one(...) alloc_hooks(pmd_alloc_one_noprof(__VA_ARGS__))
148 #endif
150 #ifndef __HAVE_ARCH_PMD_FREE
151 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
153 struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
155 BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
156 pagetable_pmd_dtor(ptdesc);
157 pagetable_free(ptdesc);
159 #endif
161 #endif /* CONFIG_PGTABLE_LEVELS > 2 */
163 #if CONFIG_PGTABLE_LEVELS > 3
165 static inline pud_t *__pud_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
167 gfp_t gfp = GFP_PGTABLE_USER;
168 struct ptdesc *ptdesc;
170 if (mm == &init_mm)
171 gfp = GFP_PGTABLE_KERNEL;
172 gfp &= ~__GFP_HIGHMEM;
174 ptdesc = pagetable_alloc_noprof(gfp, 0);
175 if (!ptdesc)
176 return NULL;
178 pagetable_pud_ctor(ptdesc);
179 return ptdesc_address(ptdesc);
181 #define __pud_alloc_one(...) alloc_hooks(__pud_alloc_one_noprof(__VA_ARGS__))
183 #ifndef __HAVE_ARCH_PUD_ALLOC_ONE
185 * pud_alloc_one - allocate memory for a PUD-level page table
186 * @mm: the mm_struct of the current context
188 * Allocate memory for a page table using %GFP_PGTABLE_USER for user context
189 * and %GFP_PGTABLE_KERNEL for kernel context.
191 * Return: pointer to the allocated memory or %NULL on error
193 static inline pud_t *pud_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
195 return __pud_alloc_one_noprof(mm, addr);
197 #define pud_alloc_one(...) alloc_hooks(pud_alloc_one_noprof(__VA_ARGS__))
198 #endif
200 static inline void __pud_free(struct mm_struct *mm, pud_t *pud)
202 struct ptdesc *ptdesc = virt_to_ptdesc(pud);
204 BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
205 pagetable_pud_dtor(ptdesc);
206 pagetable_free(ptdesc);
209 #ifndef __HAVE_ARCH_PUD_FREE
210 static inline void pud_free(struct mm_struct *mm, pud_t *pud)
212 __pud_free(mm, pud);
214 #endif
216 #endif /* CONFIG_PGTABLE_LEVELS > 3 */
218 #ifndef __HAVE_ARCH_PGD_FREE
219 static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
221 pagetable_free(virt_to_ptdesc(pgd));
223 #endif
225 #endif /* CONFIG_MMU */
227 #endif /* __ASM_GENERIC_PGALLOC_H */