vmalloc.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. struct vm_area_struct; /* vma defining user mapping in mm_types.h */
  12. struct notifier_block; /* in notifier.h */
  13. /* bits in flags of vmalloc's vm_struct below */
  14. #define VM_IOREMAP 0x00000001 /* ioremap() and friends */
  15. #define VM_ALLOC 0x00000002 /* vmalloc() */
  16. #define VM_MAP 0x00000004 /* vmap()ed pages */
  17. #define VM_USERMAP 0x00000008 /* suitable for remap_vmalloc_range */
  18. #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */
  19. #define VM_NO_GUARD 0x00000040 /* don't add guard page */
  20. #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */
  21. /* bits [20..32] reserved for arch specific ioremap internals */
  22. /*
  23. * Maximum alignment for ioremap() regions.
  24. * Can be overriden by arch-specific value.
  25. */
  26. #ifndef IOREMAP_MAX_ORDER
  27. #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */
  28. #endif
  29. struct vm_struct {
  30. struct vm_struct *next;
  31. void *addr;
  32. unsigned long size;
  33. unsigned long flags;
  34. struct page **pages;
  35. unsigned int nr_pages;
  36. phys_addr_t phys_addr;
  37. const void *caller;
  38. };
  39. struct vmap_area {
  40. unsigned long va_start;
  41. unsigned long va_end;
  42. unsigned long flags;
  43. struct rb_node rb_node; /* address sorted rbtree */
  44. struct list_head list; /* address sorted list */
  45. struct llist_node purge_list; /* "lazy purge" list */
  46. struct vm_struct *vm;
  47. struct rcu_head rcu_head;
  48. };
  49. /*
  50. * Highlevel APIs for driver use
  51. */
  52. extern void vm_unmap_ram(const void *mem, unsigned int count);
  53. extern void *vm_map_ram(struct page **pages, unsigned int count,
  54. int node, pgprot_t prot);
  55. extern void vm_unmap_aliases(void);
  56. #ifdef CONFIG_MMU
  57. extern void __init vmalloc_init(void);
  58. #else
  59. static inline void vmalloc_init(void)
  60. {
  61. }
  62. #endif
  63. extern void *vmalloc(unsigned long size);
  64. extern void *vzalloc(unsigned long size);
  65. extern void *vmalloc_user(unsigned long size);
  66. extern void *vmalloc_node(unsigned long size, int node);
  67. extern void *vzalloc_node(unsigned long size, int node);
  68. extern void *vmalloc_exec(unsigned long size);
  69. extern void *vmalloc_32(unsigned long size);
  70. extern void *vmalloc_32_user(unsigned long size);
  71. extern void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot);
  72. extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
  73. unsigned long start, unsigned long end, gfp_t gfp_mask,
  74. pgprot_t prot, unsigned long vm_flags, int node,
  75. const void *caller);
  76. #ifndef CONFIG_MMU
  77. extern void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags);
  78. static inline void *__vmalloc_node_flags_caller(unsigned long size, int node,
  79. gfp_t flags, void *caller)
  80. {
  81. return __vmalloc_node_flags(size, node, flags);
  82. }
  83. #else
  84. extern void *__vmalloc_node_flags_caller(unsigned long size,
  85. int node, gfp_t flags, void *caller);
  86. #endif
  87. extern void vfree(const void *addr);
  88. extern void vfree_atomic(const void *addr);
  89. extern void *vmap(struct page **pages, unsigned int count,
  90. unsigned long flags, pgprot_t prot);
  91. extern void vunmap(const void *addr);
  92. extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
  93. unsigned long uaddr, void *kaddr,
  94. unsigned long size);
  95. extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  96. unsigned long pgoff);
  97. void vmalloc_sync_all(void);
  98. /*
  99. * Lowlevel-APIs (not for driver use!)
  100. */
  101. static inline size_t get_vm_area_size(const struct vm_struct *area)
  102. {
  103. if (!(area->flags & VM_NO_GUARD))
  104. /* return actual size without guard page */
  105. return area->size - PAGE_SIZE;
  106. else
  107. return area->size;
  108. }
  109. extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags);
  110. extern struct vm_struct *get_vm_area_caller(unsigned long size,
  111. unsigned long flags, const void *caller);
  112. extern struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
  113. unsigned long start, unsigned long end);
  114. extern struct vm_struct *__get_vm_area_caller(unsigned long size,
  115. unsigned long flags,
  116. unsigned long start, unsigned long end,
  117. const void *caller);
  118. extern struct vm_struct *remove_vm_area(const void *addr);
  119. extern struct vm_struct *find_vm_area(const void *addr);
  120. extern int map_vm_area(struct vm_struct *area, pgprot_t prot,
  121. struct page **pages);
  122. #ifdef CONFIG_MMU
  123. extern int map_kernel_range_noflush(unsigned long start, unsigned long size,
  124. pgprot_t prot, struct page **pages);
  125. extern void unmap_kernel_range_noflush(unsigned long addr, unsigned long size);
  126. extern void unmap_kernel_range(unsigned long addr, unsigned long size);
  127. #else
  128. static inline int
  129. map_kernel_range_noflush(unsigned long start, unsigned long size,
  130. pgprot_t prot, struct page **pages)
  131. {
  132. return size >> PAGE_SHIFT;
  133. }
  134. static inline void
  135. unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
  136. {
  137. }
  138. static inline void
  139. unmap_kernel_range(unsigned long addr, unsigned long size)
  140. {
  141. }
  142. #endif
  143. /* Allocate/destroy a 'vmalloc' VM area. */
  144. extern struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes);
  145. extern void free_vm_area(struct vm_struct *area);
  146. /* for /dev/kmem */
  147. extern long vread(char *buf, char *addr, unsigned long count);
  148. extern long vwrite(char *buf, char *addr, unsigned long count);
  149. /*
  150. * Internals. Dont't use..
  151. */
  152. extern struct list_head vmap_area_list;
  153. extern __init void vm_area_add_early(struct vm_struct *vm);
  154. extern __init void vm_area_register_early(struct vm_struct *vm, size_t align);
  155. #ifdef CONFIG_SMP
  156. # ifdef CONFIG_MMU
  157. struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
  158. const size_t *sizes, int nr_vms,
  159. size_t align);
  160. void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
  161. # else
  162. static inline struct vm_struct **
  163. pcpu_get_vm_areas(const unsigned long *offsets,
  164. const size_t *sizes, int nr_vms,
  165. size_t align)
  166. {
  167. return NULL;
  168. }
  169. static inline void
  170. pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
  171. {
  172. }
  173. # endif
  174. #endif
  175. #ifdef CONFIG_MMU
  176. #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
  177. #else
  178. #define VMALLOC_TOTAL 0UL
  179. #endif
  180. int register_vmap_purge_notifier(struct notifier_block *nb);
  181. int unregister_vmap_purge_notifier(struct notifier_block *nb);
  182. #endif /* _LINUX_VMALLOC_H */