xref: /freebsd/sys/compat/linuxkpi/common/include/linux/slab.h (revision b08fc26cbdd00df6852e71e1be58fa9cc92019f0)
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-2017 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	_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 <vm/uma.h>
38 
39 #include <linux/types.h>
40 #include <linux/gfp.h>
41 
42 MALLOC_DECLARE(M_KMALLOC);
43 
44 #define	kvmalloc(size)			kmalloc(size, 0)
45 #define	kzalloc(size, flags)		kmalloc(size, (flags) | __GFP_ZERO)
46 #define	kzalloc_node(size, flags, node)	kmalloc(size, (flags) | __GFP_ZERO)
47 #define	kfree_const(ptr)		kfree(ptr)
48 #define	kcalloc(n, size, flags)	        kmalloc((n) * (size), (flags) | __GFP_ZERO)
49 #define	vzalloc(size)			__vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0)
50 #define	vfree(arg)			kfree(arg)
51 #define	kvfree(arg)			kfree(arg)
52 #define	vmalloc_node(size, node)        __vmalloc(size, GFP_KERNEL, 0)
53 #define	vmalloc_user(size)              __vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0)
54 #define	vmalloc(size)                   __vmalloc(size, GFP_KERNEL, 0)
55 #define	__kmalloc(...)			kmalloc(__VA_ARGS__)
56 #define	kmalloc_node(chunk, flags, n)	kmalloc(chunk, flags)
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(...)	linux_kmem_cache_alloc(__VA_ARGS__)
65 #define	kmem_cache_free(...) 	linux_kmem_cache_free(__VA_ARGS__)
66 #define	kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__)
67 
68 typedef void linux_kmem_ctor_t (void *);
69 
70 struct linux_kmem_cache {
71 	uma_zone_t cache_zone;
72 	linux_kmem_ctor_t *cache_ctor;
73 	unsigned cache_flags;
74 	unsigned cache_size;
75 };
76 
77 #define	SLAB_HWCACHE_ALIGN	(1 << 0)
78 #define	SLAB_DESTROY_BY_RCU     (1 << 1)
79 #define	SLAB_RECLAIM_ACCOUNT	(1 << 2)
80 
81 static inline gfp_t
82 linux_check_m_flags(gfp_t flags)
83 {
84 	const gfp_t m = M_NOWAIT | M_WAITOK;
85 
86 	/* make sure either M_NOWAIT or M_WAITOK is set */
87 	if ((flags & m) == 0)
88 		flags |= M_NOWAIT;
89 	else if ((flags & m) == m)
90 		flags &= ~M_WAITOK;
91 
92 	/* mask away LinuxKPI specific flags */
93 	return (flags & GFP_NATIVE_MASK);
94 }
95 
96 static inline void *
97 kmalloc(size_t size, gfp_t flags)
98 {
99 	return (malloc(size, M_KMALLOC, linux_check_m_flags(flags)));
100 }
101 
102 static inline void *
103 __vmalloc(size_t size, gfp_t flags, int other)
104 {
105 	return (malloc(size, M_KMALLOC, linux_check_m_flags(flags)));
106 }
107 
108 static inline void *
109 kmalloc_array(size_t n, size_t size, gfp_t flags)
110 {
111 	if (size != 0 && n > (SIZE_MAX / size))
112 		return (NULL);
113 	return (malloc(n * size, M_KMALLOC, linux_check_m_flags(flags)));
114 }
115 
116 static inline void *
117 krealloc(void *ptr, size_t size, gfp_t flags)
118 {
119 	return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags)));
120 }
121 
122 static inline void
123 kfree(const void *ptr)
124 {
125 	free(__DECONST(void *, ptr), M_KMALLOC);
126 }
127 
128 extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
129     size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
130 
131 static inline void *
132 linux_kmem_cache_alloc(struct linux_kmem_cache *c, gfp_t flags)
133 {
134 	return (uma_zalloc_arg(c->cache_zone, c,
135 	    linux_check_m_flags(flags)));
136 }
137 
138 static inline void *
139 kmem_cache_zalloc(struct linux_kmem_cache *c, gfp_t flags)
140 {
141 	return (uma_zalloc_arg(c->cache_zone, c,
142 	    linux_check_m_flags(flags | M_ZERO)));
143 }
144 
145 extern void linux_kmem_cache_free_rcu(struct linux_kmem_cache *, void *);
146 
147 static inline void
148 linux_kmem_cache_free(struct linux_kmem_cache *c, void *m)
149 {
150 	if (unlikely(c->cache_flags & SLAB_DESTROY_BY_RCU))
151 		linux_kmem_cache_free_rcu(c, m);
152 	else
153 		uma_zfree(c->cache_zone, m);
154 }
155 
156 extern void linux_kmem_cache_destroy(struct linux_kmem_cache *);
157 
158 #endif					/* _LINUX_SLAB_H_ */
159