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: stable/12/sys/compat/linuxkpi/common/include/linux/slab.h 369508 2021-03-23 11:48:48Z hselasky $
30 */
31 #ifndef _LINUX_SLAB_H_
32 #define _LINUX_SLAB_H_
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/limits.h>
38 #include <vm/uma.h>
39
40 #include <linux/compat.h>
41 #include <linux/types.h>
42 #include <linux/gfp.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(...) linux_kmem_cache_alloc(__VA_ARGS__)
67 #define kmem_cache_free(...) linux_kmem_cache_free(__VA_ARGS__)
68 #define kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__)
69
70 #define KMEM_CACHE(__struct, flags) \
71 linux_kmem_cache_create(#__struct, sizeof(struct __struct), \
72 __alignof(struct __struct), (flags), NULL)
73
74 typedef void linux_kmem_ctor_t (void *);
75
76 struct linux_kmem_cache {
77 uma_zone_t cache_zone;
78 linux_kmem_ctor_t *cache_ctor;
79 unsigned cache_flags;
80 unsigned cache_size;
81 };
82
83 #define SLAB_HWCACHE_ALIGN (1 << 0)
84 #define SLAB_TYPESAFE_BY_RCU (1 << 1)
85 #define SLAB_RECLAIM_ACCOUNT (1 << 2)
86
87 #define SLAB_DESTROY_BY_RCU \
88 SLAB_TYPESAFE_BY_RCU
89
90 #define ARCH_KMALLOC_MINALIGN \
91 __alignof(unsigned long long)
92
93 static inline gfp_t
linux_check_m_flags(gfp_t flags)94 linux_check_m_flags(gfp_t flags)
95 {
96 const gfp_t m = M_NOWAIT | M_WAITOK;
97
98 /* make sure either M_NOWAIT or M_WAITOK is set */
99 if ((flags & m) == 0)
100 flags |= M_NOWAIT;
101 else if ((flags & m) == m)
102 flags &= ~M_WAITOK;
103
104 /* mask away LinuxKPI specific flags */
105 return (flags & GFP_NATIVE_MASK);
106 }
107
108 static inline void *
kmalloc(size_t size,gfp_t flags)109 kmalloc(size_t size, gfp_t flags)
110 {
111 return (malloc(size, M_KMALLOC, linux_check_m_flags(flags)));
112 }
113
114 static inline void *
kmalloc_node(size_t size,gfp_t flags,int node)115 kmalloc_node(size_t size, gfp_t flags, int node)
116 {
117 return (malloc_domainset(size, M_KMALLOC,
118 linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
119 }
120
121 static inline void *
kcalloc(size_t n,size_t size,gfp_t flags)122 kcalloc(size_t n, size_t size, gfp_t flags)
123 {
124 flags |= __GFP_ZERO;
125 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
126 }
127
128 static inline void *
kcalloc_node(size_t n,size_t size,gfp_t flags,int node)129 kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
130 {
131 flags |= __GFP_ZERO;
132 return (mallocarray_domainset(n, size, M_KMALLOC,
133 linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
134 }
135
136 static inline void *
__vmalloc(size_t size,gfp_t flags,int other)137 __vmalloc(size_t size, gfp_t flags, int other)
138 {
139 return (malloc(size, M_KMALLOC, linux_check_m_flags(flags)));
140 }
141
142 static inline void *
__vmalloc_node(size_t size,gfp_t flags,int node)143 __vmalloc_node(size_t size, gfp_t flags, int node)
144 {
145 return (malloc_domainset(size, M_KMALLOC,
146 linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
147 }
148
149 static inline void *
vmalloc_32(size_t size)150 vmalloc_32(size_t size)
151 {
152 return (contigmalloc(size, M_KMALLOC, M_WAITOK, 0, UINT_MAX, 1, 1));
153 }
154
155 static inline void *
kmalloc_array(size_t n,size_t size,gfp_t flags)156 kmalloc_array(size_t n, size_t size, gfp_t flags)
157 {
158 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
159 }
160
161 static inline void *
kmalloc_array_node(size_t n,size_t size,gfp_t flags,int node)162 kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node)
163 {
164 return (mallocarray_domainset(n, size, M_KMALLOC,
165 linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
166 }
167
168 static inline void *
kvmalloc_array(size_t n,size_t size,gfp_t flags)169 kvmalloc_array(size_t n, size_t size, gfp_t flags)
170 {
171 return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
172 }
173
174 static inline void *
krealloc(void * ptr,size_t size,gfp_t flags)175 krealloc(void *ptr, size_t size, gfp_t flags)
176 {
177 return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags)));
178 }
179
180 static inline void
kfree(const void * ptr)181 kfree(const void *ptr)
182 {
183 free(__DECONST(void *, ptr), M_KMALLOC);
184 }
185
186 static inline size_t
ksize(const void * ptr)187 ksize(const void *ptr)
188 {
189 return (malloc_usable_size(ptr));
190 }
191
192 extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
193 size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
194
195 static inline void *
linux_kmem_cache_alloc(struct linux_kmem_cache * c,gfp_t flags)196 linux_kmem_cache_alloc(struct linux_kmem_cache *c, gfp_t flags)
197 {
198 return (uma_zalloc_arg(c->cache_zone, c,
199 linux_check_m_flags(flags)));
200 }
201
202 static inline void *
kmem_cache_zalloc(struct linux_kmem_cache * c,gfp_t flags)203 kmem_cache_zalloc(struct linux_kmem_cache *c, gfp_t flags)
204 {
205 return (uma_zalloc_arg(c->cache_zone, c,
206 linux_check_m_flags(flags | M_ZERO)));
207 }
208
209 extern void linux_kmem_cache_free_rcu(struct linux_kmem_cache *, void *);
210
211 static inline void
linux_kmem_cache_free(struct linux_kmem_cache * c,void * m)212 linux_kmem_cache_free(struct linux_kmem_cache *c, void *m)
213 {
214 if (unlikely(c->cache_flags & SLAB_TYPESAFE_BY_RCU))
215 linux_kmem_cache_free_rcu(c, m);
216 else
217 uma_zfree(c->cache_zone, m);
218 }
219
220 extern void linux_kmem_cache_destroy(struct linux_kmem_cache *);
221
222 #endif /* _LINUX_SLAB_H_ */
223