1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2021 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 #ifndef _LINUXKPI_LINUX_SLAB_H_ 32 #define _LINUXKPI_LINUX_SLAB_H_ 33 34 #include <sys/types.h> 35 #include <sys/malloc.h> 36 #include <sys/limits.h> 37 38 #include <linux/compat.h> 39 #include <linux/types.h> 40 #include <linux/gfp.h> 41 #include <linux/llist.h> 42 #include <linux/overflow.h> 43 44 MALLOC_DECLARE(M_KMALLOC); 45 46 #define kvmalloc(size, flags) kmalloc(size, flags) 47 #define kvzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 48 #define kvcalloc(n, size, flags) kvmalloc_array(n, size, (flags) | __GFP_ZERO) 49 #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 50 #define kzalloc_node(size, flags, node) kmalloc_node(size, (flags) | __GFP_ZERO, node) 51 #define kfree_const(ptr) kfree(ptr) 52 #define vzalloc(size) __vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0) 53 #define vfree(arg) kfree(arg) 54 #define kvfree(arg) kfree(arg) 55 #define vmalloc_node(size, node) __vmalloc_node(size, GFP_KERNEL, node) 56 #define vmalloc_user(size) __vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0) 57 #define vmalloc(size) __vmalloc(size, GFP_KERNEL, 0) 58 #define __kmalloc(...) kmalloc(__VA_ARGS__) 59 60 /* 61 * Prefix some functions with linux_ to avoid namespace conflict 62 * with the OpenSolaris code in the kernel. 63 */ 64 #define kmem_cache linux_kmem_cache 65 #define kmem_cache_create(...) linux_kmem_cache_create(__VA_ARGS__) 66 #define kmem_cache_alloc(...) lkpi_kmem_cache_alloc(__VA_ARGS__) 67 #define kmem_cache_zalloc(...) lkpi_kmem_cache_zalloc(__VA_ARGS__) 68 #define kmem_cache_free(...) lkpi_kmem_cache_free(__VA_ARGS__) 69 #define kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__) 70 #define kmem_cache_shrink(x) (0) 71 72 #define KMEM_CACHE(__struct, flags) \ 73 linux_kmem_cache_create(#__struct, sizeof(struct __struct), \ 74 __alignof(struct __struct), (flags), NULL) 75 76 typedef void linux_kmem_ctor_t (void *); 77 78 struct linux_kmem_cache; 79 80 #define SLAB_HWCACHE_ALIGN (1 << 0) 81 #define SLAB_TYPESAFE_BY_RCU (1 << 1) 82 #define SLAB_RECLAIM_ACCOUNT (1 << 2) 83 84 #define SLAB_DESTROY_BY_RCU \ 85 SLAB_TYPESAFE_BY_RCU 86 87 #define ARCH_KMALLOC_MINALIGN \ 88 __alignof(unsigned long long) 89 90 /* drm-kmod 5.4 compat */ 91 #define kfree_async(ptr) kfree(ptr); 92 93 #define ZERO_OR_NULL_PTR(x) ((x) == NULL) 94 95 static inline gfp_t 96 linux_check_m_flags(gfp_t flags) 97 { 98 const gfp_t m = M_NOWAIT | M_WAITOK; 99 100 /* make sure either M_NOWAIT or M_WAITOK is set */ 101 if ((flags & m) == 0) 102 flags |= M_NOWAIT; 103 else if ((flags & m) == m) 104 flags &= ~M_WAITOK; 105 106 /* mask away LinuxKPI specific flags */ 107 return (flags & GFP_NATIVE_MASK); 108 } 109 110 static inline void * 111 kmalloc(size_t size, gfp_t flags) 112 { 113 return (malloc(MAX(size, sizeof(struct llist_node)), M_KMALLOC, 114 linux_check_m_flags(flags))); 115 } 116 117 static inline void * 118 kmalloc_node(size_t size, gfp_t flags, int node) 119 { 120 return (malloc_domainset(size, M_KMALLOC, 121 linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 122 } 123 124 static inline void * 125 kcalloc(size_t n, size_t size, gfp_t flags) 126 { 127 flags |= __GFP_ZERO; 128 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 129 } 130 131 static inline void * 132 kcalloc_node(size_t n, size_t size, gfp_t flags, int node) 133 { 134 flags |= __GFP_ZERO; 135 return (mallocarray_domainset(n, size, M_KMALLOC, 136 linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 137 } 138 139 static inline void * 140 __vmalloc(size_t size, gfp_t flags, int other) 141 { 142 return (malloc(size, M_KMALLOC, linux_check_m_flags(flags))); 143 } 144 145 static inline void * 146 __vmalloc_node(size_t size, gfp_t flags, int node) 147 { 148 return (malloc_domainset(size, M_KMALLOC, 149 linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 150 } 151 152 static inline void * 153 vmalloc_32(size_t size) 154 { 155 return (contigmalloc(size, M_KMALLOC, M_WAITOK, 0, UINT_MAX, 1, 1)); 156 } 157 158 static inline void * 159 kmalloc_array(size_t n, size_t size, gfp_t flags) 160 { 161 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 162 } 163 164 static inline void * 165 kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node) 166 { 167 return (mallocarray_domainset(n, size, M_KMALLOC, 168 linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 169 } 170 171 static inline void * 172 kvmalloc_array(size_t n, size_t size, gfp_t flags) 173 { 174 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 175 } 176 177 static inline void * 178 krealloc(void *ptr, size_t size, gfp_t flags) 179 { 180 return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags))); 181 } 182 183 static inline void * 184 krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags) 185 { 186 if (WOULD_OVERFLOW(n, size)) { 187 return NULL; 188 } 189 190 return (realloc(ptr, n * size, M_KMALLOC, linux_check_m_flags(flags))); 191 } 192 193 extern void linux_kfree_async(void *); 194 195 static inline void 196 kfree(const void *ptr) 197 { 198 if (curthread->td_critnest != 0) 199 linux_kfree_async(__DECONST(void *, ptr)); 200 else 201 free(__DECONST(void *, ptr), M_KMALLOC); 202 } 203 204 static __inline void 205 kfree_sensitive(const void *ptr) 206 { 207 zfree(__DECONST(void *, ptr), M_KMALLOC); 208 } 209 210 static inline size_t 211 ksize(const void *ptr) 212 { 213 return (malloc_usable_size(ptr)); 214 } 215 216 extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name, 217 size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor); 218 extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t); 219 extern void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t); 220 extern void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *); 221 extern void linux_kmem_cache_destroy(struct linux_kmem_cache *); 222 223 #endif /* _LINUXKPI_LINUX_SLAB_H_ */ 224