xref: /freebsd/sys/compat/linuxkpi/common/include/linux/slab.h (revision 1f7df757017404011732196e65981d9325f7a89f)
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  #ifndef	_LINUXKPI_LINUX_SLAB_H_
30  #define	_LINUXKPI_LINUX_SLAB_H_
31  
32  #include <sys/types.h>
33  #include <sys/malloc.h>
34  #include <sys/limits.h>
35  
36  #include <linux/compat.h>
37  #include <linux/types.h>
38  #include <linux/gfp.h>
39  #include <linux/llist.h>
40  #include <linux/overflow.h>
41  
42  MALLOC_DECLARE(M_KMALLOC);
43  
44  #define	kmalloc(size, flags)		lkpi_kmalloc(size, flags)
45  #define	kvmalloc(size, flags)		kmalloc(size, flags)
46  #define	kvzalloc(size, flags)		kmalloc(size, (flags) | __GFP_ZERO)
47  #define	kvcalloc(n, size, flags)	kvmalloc_array(n, size, (flags) | __GFP_ZERO)
48  #define	kzalloc(size, flags)		kmalloc(size, (flags) | __GFP_ZERO)
49  #define	kzalloc_node(size, flags, node)	kmalloc_node(size, (flags) | __GFP_ZERO, node)
50  #define	kfree_const(ptr)		kfree(ptr)
51  #define	vzalloc(size)			__vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0)
52  #define	vfree(arg)			kfree(arg)
53  #define	kvfree(arg)			kfree(arg)
54  #define	vmalloc_node(size, node)	__vmalloc_node(size, GFP_KERNEL, node)
55  #define	vmalloc_user(size)		__vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0)
56  #define	vmalloc(size)			__vmalloc(size, GFP_KERNEL, 0)
57  
58  /*
59   * Prefix some functions with linux_ to avoid namespace conflict
60   * with the OpenSolaris code in the kernel.
61   */
62  #define	kmem_cache		linux_kmem_cache
63  #define	kmem_cache_create(...)	linux_kmem_cache_create(__VA_ARGS__)
64  #define	kmem_cache_alloc(...)	lkpi_kmem_cache_alloc(__VA_ARGS__)
65  #define	kmem_cache_zalloc(...)	lkpi_kmem_cache_zalloc(__VA_ARGS__)
66  #define	kmem_cache_free(...)	lkpi_kmem_cache_free(__VA_ARGS__)
67  #define	kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__)
68  #define	kmem_cache_shrink(x)	(0)
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  
78  #define	SLAB_HWCACHE_ALIGN	(1 << 0)
79  #define	SLAB_TYPESAFE_BY_RCU	(1 << 1)
80  #define	SLAB_RECLAIM_ACCOUNT	(1 << 2)
81  
82  #define	SLAB_DESTROY_BY_RCU \
83  	SLAB_TYPESAFE_BY_RCU
84  
85  #define	ARCH_KMALLOC_MINALIGN \
86  	__alignof(unsigned long long)
87  
88  /* drm-kmod 5.4 compat */
89  #define kfree_async(ptr)	kfree(ptr);
90  
91  #define	ZERO_SIZE_PTR		((void *)16)
92  #define ZERO_OR_NULL_PTR(x)	((x) == NULL || (x) == ZERO_SIZE_PTR)
93  
94  extern void *lkpi_kmalloc(size_t size, gfp_t flags);
95  void *lkpi___kmalloc(size_t size, gfp_t flags);
96  #define	__kmalloc(_s, _f)	lkpi___kmalloc(_s, _f)
97  
98  static inline gfp_t
99  linux_check_m_flags(gfp_t flags)
100  {
101  	const gfp_t m = M_NOWAIT | M_WAITOK;
102  
103  	/* make sure either M_NOWAIT or M_WAITOK is set */
104  	if ((flags & m) == 0)
105  		flags |= M_NOWAIT;
106  	else if ((flags & m) == m)
107  		flags &= ~M_WAITOK;
108  
109  	/* mask away LinuxKPI specific flags */
110  	return (flags & GFP_NATIVE_MASK);
111  }
112  
113  static inline void *
114  kmalloc_node(size_t size, gfp_t flags, int node)
115  {
116  	return (malloc_domainset(size, M_KMALLOC,
117  	    linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
118  }
119  
120  static inline void *
121  kcalloc(size_t n, size_t size, gfp_t flags)
122  {
123  	flags |= __GFP_ZERO;
124  	return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
125  }
126  
127  static inline void *
128  kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
129  {
130  	flags |= __GFP_ZERO;
131  	return (mallocarray_domainset(n, size, M_KMALLOC,
132  	    linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
133  }
134  
135  static inline void *
136  __vmalloc(size_t size, gfp_t flags, int other)
137  {
138  	return (malloc(size, M_KMALLOC, linux_check_m_flags(flags)));
139  }
140  
141  static inline void *
142  __vmalloc_node(size_t size, gfp_t flags, int node)
143  {
144  	return (malloc_domainset(size, M_KMALLOC,
145  	    linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
146  }
147  
148  static inline void *
149  vmalloc_32(size_t size)
150  {
151  	return (contigmalloc(size, M_KMALLOC, M_WAITOK, 0, UINT_MAX, 1, 1));
152  }
153  
154  static inline void *
155  kmalloc_array(size_t n, size_t size, gfp_t flags)
156  {
157  	return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
158  }
159  
160  static inline void *
161  kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node)
162  {
163  	return (mallocarray_domainset(n, size, M_KMALLOC,
164  	    linux_get_vm_domain_set(node), linux_check_m_flags(flags)));
165  }
166  
167  static inline void *
168  kvmalloc_array(size_t n, size_t size, gfp_t flags)
169  {
170  	return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags)));
171  }
172  
173  static inline void *
174  krealloc(void *ptr, size_t size, gfp_t flags)
175  {
176  	return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags)));
177  }
178  
179  static inline void *
180  krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags)
181  {
182  	if (WOULD_OVERFLOW(n, size)) {
183  		return NULL;
184  	}
185  
186  	return (realloc(ptr, n * size, M_KMALLOC, linux_check_m_flags(flags)));
187  }
188  
189  extern void linux_kfree_async(void *);
190  
191  static inline void
192  kfree(const void *ptr)
193  {
194  	if (ZERO_OR_NULL_PTR(ptr))
195  		return;
196  
197  	if (curthread->td_critnest != 0)
198  		linux_kfree_async(__DECONST(void *, ptr));
199  	else
200  		free(__DECONST(void *, ptr), M_KMALLOC);
201  }
202  
203  static __inline void
204  kfree_sensitive(const void *ptr)
205  {
206  	if (ZERO_OR_NULL_PTR(ptr))
207  		return;
208  
209  	zfree(__DECONST(void *, ptr), M_KMALLOC);
210  }
211  
212  static inline void *
213  kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags)
214  {
215  	void *newptr;
216  
217  	if (newsize <= oldsize)
218  		return (__DECONST(void *, ptr));
219  
220  	newptr = kvmalloc(newsize, flags);
221  	if (newptr != NULL) {
222  		memcpy(newptr, ptr, oldsize);
223  		kvfree(ptr);
224  	}
225  
226  	return (newptr);
227  }
228  
229  static inline size_t
230  ksize(const void *ptr)
231  {
232  	return (malloc_usable_size(ptr));
233  }
234  
235  static inline size_t
236  kmalloc_size_roundup(size_t size)
237  {
238  	if (unlikely(size == 0 || size == SIZE_MAX))
239  		return (size);
240  	return (malloc_size(size));
241  }
242  
243  extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
244      size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
245  extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t);
246  extern void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t);
247  extern void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *);
248  extern void linux_kmem_cache_destroy(struct linux_kmem_cache *);
249  
250  #endif					/* _LINUXKPI_LINUX_SLAB_H_ */
251