pgalloc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_GENERIC_PGALLOC_H
  3. #define __ASM_GENERIC_PGALLOC_H
  4. #ifdef CONFIG_MMU
  5. #define GFP_PGTABLE_KERNEL (GFP_KERNEL | __GFP_ZERO)
  6. #define GFP_PGTABLE_USER (GFP_PGTABLE_KERNEL | __GFP_ACCOUNT)
  7. /**
  8. * __pte_alloc_one_kernel - allocate memory for a PTE-level kernel page table
  9. * @mm: the mm_struct of the current context
  10. *
  11. * This function is intended for architectures that need
  12. * anything beyond simple page allocation.
  13. *
  14. * Return: pointer to the allocated memory or %NULL on error
  15. */
  16. static inline pte_t *__pte_alloc_one_kernel_noprof(struct mm_struct *mm)
  17. {
  18. struct ptdesc *ptdesc = pagetable_alloc_noprof(GFP_PGTABLE_KERNEL, 0);
  19. if (!ptdesc)
  20. return NULL;
  21. if (!pagetable_pte_ctor(mm, ptdesc)) {
  22. pagetable_free(ptdesc);
  23. return NULL;
  24. }
  25. ptdesc_set_kernel(ptdesc);
  26. return ptdesc_address(ptdesc);
  27. }
  28. #define __pte_alloc_one_kernel(...) alloc_hooks(__pte_alloc_one_kernel_noprof(__VA_ARGS__))
  29. #ifndef __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL
  30. /**
  31. * pte_alloc_one_kernel - allocate memory for a PTE-level kernel page table
  32. * @mm: the mm_struct of the current context
  33. *
  34. * Return: pointer to the allocated memory or %NULL on error
  35. */
  36. static inline pte_t *pte_alloc_one_kernel_noprof(struct mm_struct *mm)
  37. {
  38. return __pte_alloc_one_kernel_noprof(mm);
  39. }
  40. #define pte_alloc_one_kernel(...) alloc_hooks(pte_alloc_one_kernel_noprof(__VA_ARGS__))
  41. #endif
  42. /**
  43. * pte_free_kernel - free PTE-level kernel page table memory
  44. * @mm: the mm_struct of the current context
  45. * @pte: pointer to the memory containing the page table
  46. */
  47. static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
  48. {
  49. pagetable_dtor_free(virt_to_ptdesc(pte));
  50. }
  51. /**
  52. * __pte_alloc_one - allocate memory for a PTE-level user page table
  53. * @mm: the mm_struct of the current context
  54. * @gfp: GFP flags to use for the allocation
  55. *
  56. * Allocate memory for a page table and ptdesc and runs pagetable_pte_ctor().
  57. *
  58. * This function is intended for architectures that need
  59. * anything beyond simple page allocation or must have custom GFP flags.
  60. *
  61. * Return: `struct page` referencing the ptdesc or %NULL on error
  62. */
  63. static inline pgtable_t __pte_alloc_one_noprof(struct mm_struct *mm, gfp_t gfp)
  64. {
  65. struct ptdesc *ptdesc;
  66. ptdesc = pagetable_alloc_noprof(gfp, 0);
  67. if (!ptdesc)
  68. return NULL;
  69. if (!pagetable_pte_ctor(mm, ptdesc)) {
  70. pagetable_free(ptdesc);
  71. return NULL;
  72. }
  73. return ptdesc_page(ptdesc);
  74. }
  75. #define __pte_alloc_one(...) alloc_hooks(__pte_alloc_one_noprof(__VA_ARGS__))
  76. #ifndef __HAVE_ARCH_PTE_ALLOC_ONE
  77. /**
  78. * pte_alloc_one - allocate a page for PTE-level user page table
  79. * @mm: the mm_struct of the current context
  80. *
  81. * Allocate memory for a page table and ptdesc and runs pagetable_pte_ctor().
  82. *
  83. * Return: `struct page` referencing the ptdesc or %NULL on error
  84. */
  85. static inline pgtable_t pte_alloc_one_noprof(struct mm_struct *mm)
  86. {
  87. return __pte_alloc_one_noprof(mm, GFP_PGTABLE_USER);
  88. }
  89. #define pte_alloc_one(...) alloc_hooks(pte_alloc_one_noprof(__VA_ARGS__))
  90. #endif
  91. /*
  92. * Should really implement gc for free page table pages. This could be
  93. * done with a reference count in struct page.
  94. */
  95. /**
  96. * pte_free - free PTE-level user page table memory
  97. * @mm: the mm_struct of the current context
  98. * @pte_page: the `struct page` referencing the ptdesc
  99. */
  100. static inline void pte_free(struct mm_struct *mm, struct page *pte_page)
  101. {
  102. struct ptdesc *ptdesc = page_ptdesc(pte_page);
  103. pagetable_dtor_free(ptdesc);
  104. }
  105. #if CONFIG_PGTABLE_LEVELS > 2
  106. #ifndef __HAVE_ARCH_PMD_ALLOC_ONE
  107. /**
  108. * pmd_alloc_one - allocate memory for a PMD-level page table
  109. * @mm: the mm_struct of the current context
  110. *
  111. * Allocate memory for a page table and ptdesc and runs pagetable_pmd_ctor().
  112. *
  113. * Allocations use %GFP_PGTABLE_USER in user context and
  114. * %GFP_PGTABLE_KERNEL in kernel context.
  115. *
  116. * Return: pointer to the allocated memory or %NULL on error
  117. */
  118. static inline pmd_t *pmd_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
  119. {
  120. struct ptdesc *ptdesc;
  121. gfp_t gfp = GFP_PGTABLE_USER;
  122. if (mm == &init_mm)
  123. gfp = GFP_PGTABLE_KERNEL;
  124. ptdesc = pagetable_alloc_noprof(gfp, 0);
  125. if (!ptdesc)
  126. return NULL;
  127. if (!pagetable_pmd_ctor(mm, ptdesc)) {
  128. pagetable_free(ptdesc);
  129. return NULL;
  130. }
  131. if (mm == &init_mm)
  132. ptdesc_set_kernel(ptdesc);
  133. return ptdesc_address(ptdesc);
  134. }
  135. #define pmd_alloc_one(...) alloc_hooks(pmd_alloc_one_noprof(__VA_ARGS__))
  136. #endif
  137. #ifndef __HAVE_ARCH_PMD_FREE
  138. static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
  139. {
  140. struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
  141. BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
  142. pagetable_dtor_free(ptdesc);
  143. }
  144. #endif
  145. #endif /* CONFIG_PGTABLE_LEVELS > 2 */
  146. #if CONFIG_PGTABLE_LEVELS > 3
  147. static inline pud_t *__pud_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
  148. {
  149. gfp_t gfp = GFP_PGTABLE_USER;
  150. struct ptdesc *ptdesc;
  151. if (mm == &init_mm)
  152. gfp = GFP_PGTABLE_KERNEL;
  153. ptdesc = pagetable_alloc_noprof(gfp, 0);
  154. if (!ptdesc)
  155. return NULL;
  156. pagetable_pud_ctor(ptdesc);
  157. if (mm == &init_mm)
  158. ptdesc_set_kernel(ptdesc);
  159. return ptdesc_address(ptdesc);
  160. }
  161. #define __pud_alloc_one(...) alloc_hooks(__pud_alloc_one_noprof(__VA_ARGS__))
  162. #ifndef __HAVE_ARCH_PUD_ALLOC_ONE
  163. /**
  164. * pud_alloc_one - allocate memory for a PUD-level page table
  165. * @mm: the mm_struct of the current context
  166. *
  167. * Allocate memory for a page table using %GFP_PGTABLE_USER for user context
  168. * and %GFP_PGTABLE_KERNEL for kernel context.
  169. *
  170. * Return: pointer to the allocated memory or %NULL on error
  171. */
  172. static inline pud_t *pud_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
  173. {
  174. return __pud_alloc_one_noprof(mm, addr);
  175. }
  176. #define pud_alloc_one(...) alloc_hooks(pud_alloc_one_noprof(__VA_ARGS__))
  177. #endif
  178. static inline void __pud_free(struct mm_struct *mm, pud_t *pud)
  179. {
  180. struct ptdesc *ptdesc = virt_to_ptdesc(pud);
  181. BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
  182. pagetable_dtor_free(ptdesc);
  183. }
  184. #ifndef __HAVE_ARCH_PUD_FREE
  185. static inline void pud_free(struct mm_struct *mm, pud_t *pud)
  186. {
  187. __pud_free(mm, pud);
  188. }
  189. #endif
  190. #endif /* CONFIG_PGTABLE_LEVELS > 3 */
  191. #if CONFIG_PGTABLE_LEVELS > 4
  192. static inline p4d_t *__p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
  193. {
  194. gfp_t gfp = GFP_PGTABLE_USER;
  195. struct ptdesc *ptdesc;
  196. if (mm == &init_mm)
  197. gfp = GFP_PGTABLE_KERNEL;
  198. ptdesc = pagetable_alloc_noprof(gfp, 0);
  199. if (!ptdesc)
  200. return NULL;
  201. pagetable_p4d_ctor(ptdesc);
  202. if (mm == &init_mm)
  203. ptdesc_set_kernel(ptdesc);
  204. return ptdesc_address(ptdesc);
  205. }
  206. #define __p4d_alloc_one(...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS__))
  207. #ifndef __HAVE_ARCH_P4D_ALLOC_ONE
  208. static inline p4d_t *p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long addr)
  209. {
  210. return __p4d_alloc_one_noprof(mm, addr);
  211. }
  212. #define p4d_alloc_one(...) alloc_hooks(p4d_alloc_one_noprof(__VA_ARGS__))
  213. #endif
  214. static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d)
  215. {
  216. struct ptdesc *ptdesc = virt_to_ptdesc(p4d);
  217. BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
  218. pagetable_dtor_free(ptdesc);
  219. }
  220. #ifndef __HAVE_ARCH_P4D_FREE
  221. static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
  222. {
  223. if (!mm_p4d_folded(mm))
  224. __p4d_free(mm, p4d);
  225. }
  226. #endif
  227. #endif /* CONFIG_PGTABLE_LEVELS > 4 */
  228. static inline pgd_t *__pgd_alloc_noprof(struct mm_struct *mm, unsigned int order)
  229. {
  230. gfp_t gfp = GFP_PGTABLE_USER;
  231. struct ptdesc *ptdesc;
  232. if (mm == &init_mm)
  233. gfp = GFP_PGTABLE_KERNEL;
  234. ptdesc = pagetable_alloc_noprof(gfp, order);
  235. if (!ptdesc)
  236. return NULL;
  237. pagetable_pgd_ctor(ptdesc);
  238. if (mm == &init_mm)
  239. ptdesc_set_kernel(ptdesc);
  240. return ptdesc_address(ptdesc);
  241. }
  242. #define __pgd_alloc(...) alloc_hooks(__pgd_alloc_noprof(__VA_ARGS__))
  243. static inline void __pgd_free(struct mm_struct *mm, pgd_t *pgd)
  244. {
  245. struct ptdesc *ptdesc = virt_to_ptdesc(pgd);
  246. BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
  247. pagetable_dtor_free(ptdesc);
  248. }
  249. #ifndef __HAVE_ARCH_PGD_FREE
  250. static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  251. {
  252. __pgd_free(mm, pgd);
  253. }
  254. #endif
  255. #endif /* CONFIG_MMU */
  256. #endif /* __ASM_GENERIC_PGALLOC_H */