vmalloc.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_VMALLOC_H
  3. #define _LINUX_VMALLOC_H
  4. #include <linux/spinlock.h>
  5. #include <linux/init.h>
  6. #include <linux/list.h>
  7. #include <linux/llist.h>
  8. #include <asm/page.h> /* pgprot_t */
  9. #include <linux/rbtree.h>
  10. #include <linux/overflow.h>
  11. #include <asm/vmalloc.h>
  12. struct vm_area_struct; /* vma defining user mapping in mm_types.h */
  13. struct notifier_block; /* in notifier.h */
  14. /* bits in flags of vmalloc's vm_struct below */
  15. #define VM_IOREMAP 0x00000001 /* ioremap() and friends */
  16. #define VM_ALLOC 0x00000002 /* vmalloc() */
  17. #define VM_MAP 0x00000004 /* vmap()ed pages */
  18. #define VM_USERMAP 0x00000008 /* suitable for remap_vmalloc_range */
  19. #define VM_DMA_COHERENT 0x00000010 /* dma_alloc_coherent */
  20. #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */
  21. #define VM_NO_GUARD 0x00000040 /* ***DANGEROUS*** don't add guard page */
  22. #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */
  23. #define VM_FLUSH_RESET_PERMS 0x00000100 /* reset direct map and flush TLB on unmap, can't be freed in atomic context */
  24. #define VM_MAP_PUT_PAGES 0x00000200 /* put pages and free array in vfree */
  25. #define VM_ALLOW_HUGE_VMAP 0x00000400 /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */
  26. #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
  27. !defined(CONFIG_KASAN_VMALLOC)
  28. #define VM_DEFER_KMEMLEAK 0x00000800 /* defer kmemleak object creation */
  29. #else
  30. #define VM_DEFER_KMEMLEAK 0
  31. #endif
  32. /* bits [20..32] reserved for arch specific ioremap internals */
  33. /*
  34. * Maximum alignment for ioremap() regions.
  35. * Can be overridden by arch-specific value.
  36. */
  37. #ifndef IOREMAP_MAX_ORDER
  38. #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */
  39. #endif
  40. struct vm_struct {
  41. struct vm_struct *next;
  42. void *addr;
  43. unsigned long size;
  44. unsigned long flags;
  45. struct page **pages;
  46. #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
  47. unsigned int page_order;
  48. #endif
  49. unsigned int nr_pages;
  50. phys_addr_t phys_addr;
  51. const void *caller;
  52. };
  53. struct vmap_area {
  54. unsigned long va_start;
  55. unsigned long va_end;
  56. struct rb_node rb_node; /* address sorted rbtree */
  57. struct list_head list; /* address sorted list */
  58. /*
  59. * The following two variables can be packed, because
  60. * a vmap_area object can be either:
  61. * 1) in "free" tree (root is free_vmap_area_root)
  62. * 2) or "busy" tree (root is vmap_area_root)
  63. */
  64. union {
  65. unsigned long subtree_max_size; /* in "free" tree */
  66. struct vm_struct *vm; /* in "busy" tree */
  67. };
  68. };
  69. /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
  70. #ifndef arch_vmap_p4d_supported
  71. static inline bool arch_vmap_p4d_supported(pgprot_t prot)
  72. {
  73. return false;
  74. }
  75. #endif
  76. #ifndef arch_vmap_pud_supported
  77. static inline bool arch_vmap_pud_supported(pgprot_t prot)
  78. {
  79. return false;
  80. }
  81. #endif
  82. #ifndef arch_vmap_pmd_supported
  83. static inline bool arch_vmap_pmd_supported(pgprot_t prot)
  84. {
  85. return false;
  86. }
  87. #endif
  88. #ifndef arch_vmap_pte_range_map_size
  89. static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end,
  90. u64 pfn, unsigned int max_page_shift)
  91. {
  92. return PAGE_SIZE;
  93. }
  94. #endif
  95. #ifndef arch_vmap_pte_supported_shift
  96. static inline int arch_vmap_pte_supported_shift(unsigned long size)
  97. {
  98. return PAGE_SHIFT;
  99. }
  100. #endif
  101. #ifndef arch_vmap_pgprot_tagged
  102. static inline pgprot_t arch_vmap_pgprot_tagged(pgprot_t prot)
  103. {
  104. return prot;
  105. }
  106. #endif
  107. /*
  108. * Highlevel APIs for driver use
  109. */
  110. extern void vm_unmap_ram(const void *mem, unsigned int count);
  111. extern void *vm_map_ram(struct page **pages, unsigned int count, int node);
  112. extern void vm_unmap_aliases(void);
  113. #ifdef CONFIG_MMU
  114. extern void __init vmalloc_init(void);
  115. extern unsigned long vmalloc_nr_pages(void);
  116. #else
  117. static inline void vmalloc_init(void)
  118. {
  119. }
  120. static inline unsigned long vmalloc_nr_pages(void) { return 0; }
  121. #endif
  122. extern void *vmalloc(unsigned long size) __alloc_size(1);
  123. extern void *vzalloc(unsigned long size) __alloc_size(1);
  124. extern void *vmalloc_user(unsigned long size) __alloc_size(1);
  125. extern void *vmalloc_node(unsigned long size, int node) __alloc_size(1);
  126. extern void *vzalloc_node(unsigned long size, int node) __alloc_size(1);
  127. extern void *vmalloc_32(unsigned long size) __alloc_size(1);
  128. extern void *vmalloc_32_user(unsigned long size) __alloc_size(1);
  129. extern void *__vmalloc(unsigned long size, gfp_t gfp_mask) __alloc_size(1);
  130. extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
  131. unsigned long start, unsigned long end, gfp_t gfp_mask,
  132. pgprot_t prot, unsigned long vm_flags, int node,
  133. const void *caller) __alloc_size(1);
  134. void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
  135. int node, const void *caller) __alloc_size(1);
  136. void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __alloc_size(1);
  137. extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
  138. extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
  139. extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
  140. extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2);
  141. extern void vfree(const void *addr);
  142. extern void vfree_atomic(const void *addr);
  143. extern void *vmap(struct page **pages, unsigned int count,
  144. unsigned long flags, pgprot_t prot);
  145. void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
  146. extern void vunmap(const void *addr);
  147. extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
  148. unsigned long uaddr, void *kaddr,
  149. unsigned long pgoff, unsigned long size);
  150. extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  151. unsigned long pgoff);
  152. /*
  153. * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
  154. * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
  155. * needs to be called.
  156. */
  157. #ifndef ARCH_PAGE_TABLE_SYNC_MASK
  158. #define ARCH_PAGE_TABLE_SYNC_MASK 0
  159. #endif
  160. /*
  161. * There is no default implementation for arch_sync_kernel_mappings(). It is
  162. * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK
  163. * is 0.
  164. */
  165. void arch_sync_kernel_mappings(unsigned long start, unsigned long end);
  166. /*
  167. * Lowlevel-APIs (not for driver use!)
  168. */
  169. static inline size_t get_vm_area_size(const struct vm_struct *area)
  170. {
  171. if (!(area->flags & VM_NO_GUARD))
  172. /* return actual size without guard page */
  173. return area->size - PAGE_SIZE;
  174. else
  175. return area->size;
  176. }
  177. extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags);
  178. extern struct vm_struct *get_vm_area_caller(unsigned long size,
  179. unsigned long flags, const void *caller);
  180. extern struct vm_struct *__get_vm_area_caller(unsigned long size,
  181. unsigned long flags,
  182. unsigned long start, unsigned long end,
  183. const void *caller);
  184. void free_vm_area(struct vm_struct *area);
  185. extern struct vm_struct *remove_vm_area(const void *addr);
  186. extern struct vm_struct *find_vm_area(const void *addr);
  187. struct vmap_area *find_vmap_area(unsigned long addr);
  188. static inline bool is_vm_area_hugepages(const void *addr)
  189. {
  190. /*
  191. * This may not 100% tell if the area is mapped with > PAGE_SIZE
  192. * page table entries, if for some reason the architecture indicates
  193. * larger sizes are available but decides not to use them, nothing
  194. * prevents that. This only indicates the size of the physical page
  195. * allocated in the vmalloc layer.
  196. */
  197. #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
  198. return find_vm_area(addr)->page_order > 0;
  199. #else
  200. return false;
  201. #endif
  202. }
  203. #ifdef CONFIG_MMU
  204. void vunmap_range(unsigned long addr, unsigned long end);
  205. static inline void set_vm_flush_reset_perms(void *addr)
  206. {
  207. struct vm_struct *vm = find_vm_area(addr);
  208. if (vm)
  209. vm->flags |= VM_FLUSH_RESET_PERMS;
  210. }
  211. #else
  212. static inline void set_vm_flush_reset_perms(void *addr)
  213. {
  214. }
  215. #endif
  216. /* for /proc/kcore */
  217. extern long vread(char *buf, char *addr, unsigned long count);
  218. /*
  219. * Internals. Don't use..
  220. */
  221. extern struct list_head vmap_area_list;
  222. extern __init void vm_area_add_early(struct vm_struct *vm);
  223. extern __init void vm_area_register_early(struct vm_struct *vm, size_t align);
  224. #ifdef CONFIG_SMP
  225. # ifdef CONFIG_MMU
  226. struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
  227. const size_t *sizes, int nr_vms,
  228. size_t align);
  229. void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
  230. # else
  231. static inline struct vm_struct **
  232. pcpu_get_vm_areas(const unsigned long *offsets,
  233. const size_t *sizes, int nr_vms,
  234. size_t align)
  235. {
  236. return NULL;
  237. }
  238. static inline void
  239. pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
  240. {
  241. }
  242. # endif
  243. #endif
  244. #ifdef CONFIG_MMU
  245. #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
  246. #else
  247. #define VMALLOC_TOTAL 0UL
  248. #endif
  249. int register_vmap_purge_notifier(struct notifier_block *nb);
  250. int unregister_vmap_purge_notifier(struct notifier_block *nb);
  251. #if defined(CONFIG_MMU) && defined(CONFIG_PRINTK)
  252. bool vmalloc_dump_obj(void *object);
  253. #else
  254. static inline bool vmalloc_dump_obj(void *object) { return false; }
  255. #endif
  256. #endif /* _LINUX_VMALLOC_H */