Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_VMALLOC_H
0003 #define _LINUX_VMALLOC_H
0004 
0005 #include <linux/spinlock.h>
0006 #include <linux/init.h>
0007 #include <linux/list.h>
0008 #include <linux/llist.h>
0009 #include <asm/page.h>       /* pgprot_t */
0010 #include <linux/rbtree.h>
0011 #include <linux/overflow.h>
0012 
0013 #include <asm/vmalloc.h>
0014 
0015 struct vm_area_struct;      /* vma defining user mapping in mm_types.h */
0016 struct notifier_block;      /* in notifier.h */
0017 
0018 /* bits in flags of vmalloc's vm_struct below */
0019 #define VM_IOREMAP      0x00000001  /* ioremap() and friends */
0020 #define VM_ALLOC        0x00000002  /* vmalloc() */
0021 #define VM_MAP          0x00000004  /* vmap()ed pages */
0022 #define VM_USERMAP      0x00000008  /* suitable for remap_vmalloc_range */
0023 #define VM_DMA_COHERENT     0x00000010  /* dma_alloc_coherent */
0024 #define VM_UNINITIALIZED    0x00000020  /* vm_struct is not fully initialized */
0025 #define VM_NO_GUARD     0x00000040      /* ***DANGEROUS*** don't add guard page */
0026 #define VM_KASAN        0x00000080      /* has allocated kasan shadow memory */
0027 #define VM_FLUSH_RESET_PERMS    0x00000100  /* reset direct map and flush TLB on unmap, can't be freed in atomic context */
0028 #define VM_MAP_PUT_PAGES    0x00000200  /* put pages and free array in vfree */
0029 #define VM_ALLOW_HUGE_VMAP  0x00000400      /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */
0030 
0031 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
0032     !defined(CONFIG_KASAN_VMALLOC)
0033 #define VM_DEFER_KMEMLEAK   0x00000800  /* defer kmemleak object creation */
0034 #else
0035 #define VM_DEFER_KMEMLEAK   0
0036 #endif
0037 
0038 /* bits [20..32] reserved for arch specific ioremap internals */
0039 
0040 /*
0041  * Maximum alignment for ioremap() regions.
0042  * Can be overridden by arch-specific value.
0043  */
0044 #ifndef IOREMAP_MAX_ORDER
0045 #define IOREMAP_MAX_ORDER   (7 + PAGE_SHIFT)    /* 128 pages */
0046 #endif
0047 
0048 struct vm_struct {
0049     struct vm_struct    *next;
0050     void            *addr;
0051     unsigned long       size;
0052     unsigned long       flags;
0053     struct page     **pages;
0054 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
0055     unsigned int        page_order;
0056 #endif
0057     unsigned int        nr_pages;
0058     phys_addr_t     phys_addr;
0059     const void      *caller;
0060 };
0061 
0062 struct vmap_area {
0063     unsigned long va_start;
0064     unsigned long va_end;
0065 
0066     struct rb_node rb_node;         /* address sorted rbtree */
0067     struct list_head list;          /* address sorted list */
0068 
0069     /*
0070      * The following two variables can be packed, because
0071      * a vmap_area object can be either:
0072      *    1) in "free" tree (root is free_vmap_area_root)
0073      *    2) or "busy" tree (root is vmap_area_root)
0074      */
0075     union {
0076         unsigned long subtree_max_size; /* in "free" tree */
0077         struct vm_struct *vm;           /* in "busy" tree */
0078     };
0079 };
0080 
0081 /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
0082 #ifndef arch_vmap_p4d_supported
0083 static inline bool arch_vmap_p4d_supported(pgprot_t prot)
0084 {
0085     return false;
0086 }
0087 #endif
0088 
0089 #ifndef arch_vmap_pud_supported
0090 static inline bool arch_vmap_pud_supported(pgprot_t prot)
0091 {
0092     return false;
0093 }
0094 #endif
0095 
0096 #ifndef arch_vmap_pmd_supported
0097 static inline bool arch_vmap_pmd_supported(pgprot_t prot)
0098 {
0099     return false;
0100 }
0101 #endif
0102 
0103 #ifndef arch_vmap_pte_range_map_size
0104 static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end,
0105                              u64 pfn, unsigned int max_page_shift)
0106 {
0107     return PAGE_SIZE;
0108 }
0109 #endif
0110 
0111 #ifndef arch_vmap_pte_supported_shift
0112 static inline int arch_vmap_pte_supported_shift(unsigned long size)
0113 {
0114     return PAGE_SHIFT;
0115 }
0116 #endif
0117 
0118 #ifndef arch_vmap_pgprot_tagged
0119 static inline pgprot_t arch_vmap_pgprot_tagged(pgprot_t prot)
0120 {
0121     return prot;
0122 }
0123 #endif
0124 
0125 /*
0126  *  Highlevel APIs for driver use
0127  */
0128 extern void vm_unmap_ram(const void *mem, unsigned int count);
0129 extern void *vm_map_ram(struct page **pages, unsigned int count, int node);
0130 extern void vm_unmap_aliases(void);
0131 
0132 #ifdef CONFIG_MMU
0133 extern void __init vmalloc_init(void);
0134 extern unsigned long vmalloc_nr_pages(void);
0135 #else
0136 static inline void vmalloc_init(void)
0137 {
0138 }
0139 static inline unsigned long vmalloc_nr_pages(void) { return 0; }
0140 #endif
0141 
0142 extern void *vmalloc(unsigned long size) __alloc_size(1);
0143 extern void *vzalloc(unsigned long size) __alloc_size(1);
0144 extern void *vmalloc_user(unsigned long size) __alloc_size(1);
0145 extern void *vmalloc_node(unsigned long size, int node) __alloc_size(1);
0146 extern void *vzalloc_node(unsigned long size, int node) __alloc_size(1);
0147 extern void *vmalloc_32(unsigned long size) __alloc_size(1);
0148 extern void *vmalloc_32_user(unsigned long size) __alloc_size(1);
0149 extern void *__vmalloc(unsigned long size, gfp_t gfp_mask) __alloc_size(1);
0150 extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
0151             unsigned long start, unsigned long end, gfp_t gfp_mask,
0152             pgprot_t prot, unsigned long vm_flags, int node,
0153             const void *caller) __alloc_size(1);
0154 void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
0155         int node, const void *caller) __alloc_size(1);
0156 void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __alloc_size(1);
0157 
0158 extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
0159 extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2);
0160 extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
0161 extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2);
0162 
0163 extern void vfree(const void *addr);
0164 extern void vfree_atomic(const void *addr);
0165 
0166 extern void *vmap(struct page **pages, unsigned int count,
0167             unsigned long flags, pgprot_t prot);
0168 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
0169 extern void vunmap(const void *addr);
0170 
0171 extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
0172                        unsigned long uaddr, void *kaddr,
0173                        unsigned long pgoff, unsigned long size);
0174 
0175 extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
0176                             unsigned long pgoff);
0177 
0178 /*
0179  * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
0180  * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
0181  * needs to be called.
0182  */
0183 #ifndef ARCH_PAGE_TABLE_SYNC_MASK
0184 #define ARCH_PAGE_TABLE_SYNC_MASK 0
0185 #endif
0186 
0187 /*
0188  * There is no default implementation for arch_sync_kernel_mappings(). It is
0189  * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK
0190  * is 0.
0191  */
0192 void arch_sync_kernel_mappings(unsigned long start, unsigned long end);
0193 
0194 /*
0195  *  Lowlevel-APIs (not for driver use!)
0196  */
0197 
0198 static inline size_t get_vm_area_size(const struct vm_struct *area)
0199 {
0200     if (!(area->flags & VM_NO_GUARD))
0201         /* return actual size without guard page */
0202         return area->size - PAGE_SIZE;
0203     else
0204         return area->size;
0205 
0206 }
0207 
0208 extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags);
0209 extern struct vm_struct *get_vm_area_caller(unsigned long size,
0210                     unsigned long flags, const void *caller);
0211 extern struct vm_struct *__get_vm_area_caller(unsigned long size,
0212                     unsigned long flags,
0213                     unsigned long start, unsigned long end,
0214                     const void *caller);
0215 void free_vm_area(struct vm_struct *area);
0216 extern struct vm_struct *remove_vm_area(const void *addr);
0217 extern struct vm_struct *find_vm_area(const void *addr);
0218 struct vmap_area *find_vmap_area(unsigned long addr);
0219 
0220 static inline bool is_vm_area_hugepages(const void *addr)
0221 {
0222     /*
0223      * This may not 100% tell if the area is mapped with > PAGE_SIZE
0224      * page table entries, if for some reason the architecture indicates
0225      * larger sizes are available but decides not to use them, nothing
0226      * prevents that. This only indicates the size of the physical page
0227      * allocated in the vmalloc layer.
0228      */
0229 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
0230     return find_vm_area(addr)->page_order > 0;
0231 #else
0232     return false;
0233 #endif
0234 }
0235 
0236 #ifdef CONFIG_MMU
0237 void vunmap_range(unsigned long addr, unsigned long end);
0238 static inline void set_vm_flush_reset_perms(void *addr)
0239 {
0240     struct vm_struct *vm = find_vm_area(addr);
0241 
0242     if (vm)
0243         vm->flags |= VM_FLUSH_RESET_PERMS;
0244 }
0245 
0246 #else
0247 static inline void set_vm_flush_reset_perms(void *addr)
0248 {
0249 }
0250 #endif
0251 
0252 /* for /proc/kcore */
0253 extern long vread(char *buf, char *addr, unsigned long count);
0254 
0255 /*
0256  *  Internals.  Don't use..
0257  */
0258 extern struct list_head vmap_area_list;
0259 extern __init void vm_area_add_early(struct vm_struct *vm);
0260 extern __init void vm_area_register_early(struct vm_struct *vm, size_t align);
0261 
0262 #ifdef CONFIG_SMP
0263 # ifdef CONFIG_MMU
0264 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
0265                      const size_t *sizes, int nr_vms,
0266                      size_t align);
0267 
0268 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
0269 # else
0270 static inline struct vm_struct **
0271 pcpu_get_vm_areas(const unsigned long *offsets,
0272         const size_t *sizes, int nr_vms,
0273         size_t align)
0274 {
0275     return NULL;
0276 }
0277 
0278 static inline void
0279 pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
0280 {
0281 }
0282 # endif
0283 #endif
0284 
0285 #ifdef CONFIG_MMU
0286 #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
0287 #else
0288 #define VMALLOC_TOTAL 0UL
0289 #endif
0290 
0291 int register_vmap_purge_notifier(struct notifier_block *nb);
0292 int unregister_vmap_purge_notifier(struct notifier_block *nb);
0293 
0294 #if defined(CONFIG_MMU) && defined(CONFIG_PRINTK)
0295 bool vmalloc_dump_obj(void *object);
0296 #else
0297 static inline bool vmalloc_dump_obj(void *object) { return false; }
0298 #endif
0299 
0300 #endif /* _LINUX_VMALLOC_H */