]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/cddl/compat/opensolaris/kern/opensolaris_kmem.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / cddl / compat / opensolaris / kern / opensolaris_kmem.c
1 /*-
2  * Copyright (c) 2006-2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kmem.h>
35 #include <sys/debug.h>
36 #include <sys/mutex.h>
37
38 #include <vm/vm_page.h>
39 #include <vm/vm_object.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_map.h>
42
43 #ifdef KMEM_DEBUG
44 #include <sys/queue.h>
45 #include <sys/stack.h>
46 #endif
47
48 #ifdef _KERNEL
49 MALLOC_DEFINE(M_SOLARIS, "solaris", "Solaris");
50 #else
51 #define malloc(size, type, flags)       malloc(size)
52 #define free(addr, type)                free(addr)
53 #endif
54
55 #ifdef KMEM_DEBUG
56 struct kmem_item {
57         struct stack    stack;
58         LIST_ENTRY(kmem_item) next;
59 };
60 static LIST_HEAD(, kmem_item) kmem_items;
61 static struct mtx kmem_items_mtx;
62 MTX_SYSINIT(kmem_items_mtx, &kmem_items_mtx, "kmem_items", MTX_DEF);
63 #endif  /* KMEM_DEBUG */
64
65 #include <sys/vmem.h>
66
67 void *
68 zfs_kmem_alloc(size_t size, int kmflags)
69 {
70         void *p;
71 #ifdef KMEM_DEBUG
72         struct kmem_item *i;
73
74         size += sizeof(struct kmem_item);
75 #endif
76         p = malloc(size, M_SOLARIS, kmflags);
77 #ifndef _KERNEL
78         if (kmflags & KM_SLEEP)
79                 assert(p != NULL);
80 #endif
81 #ifdef KMEM_DEBUG
82         if (p != NULL) {
83                 i = p;
84                 p = (u_char *)p + sizeof(struct kmem_item);
85                 stack_save(&i->stack);
86                 mtx_lock(&kmem_items_mtx);
87                 LIST_INSERT_HEAD(&kmem_items, i, next);
88                 mtx_unlock(&kmem_items_mtx);
89         }
90 #endif
91         return (p);
92 }
93
94 void
95 zfs_kmem_free(void *buf, size_t size __unused)
96 {
97 #ifdef KMEM_DEBUG
98         if (buf == NULL) {
99                 printf("%s: attempt to free NULL\n", __func__);
100                 return;
101         }
102         struct kmem_item *i;
103
104         buf = (u_char *)buf - sizeof(struct kmem_item);
105         mtx_lock(&kmem_items_mtx);
106         LIST_FOREACH(i, &kmem_items, next) {
107                 if (i == buf)
108                         break;
109         }
110         ASSERT(i != NULL);
111         LIST_REMOVE(i, next);
112         mtx_unlock(&kmem_items_mtx);
113 #endif
114         free(buf, M_SOLARIS);
115 }
116
117 static uint64_t kmem_size_val;
118
119 static void
120 kmem_size_init(void *unused __unused)
121 {
122
123         kmem_size_val = (uint64_t)cnt.v_page_count * PAGE_SIZE;
124         if (kmem_size_val > vm_kmem_size)
125                 kmem_size_val = vm_kmem_size;
126 }
127 SYSINIT(kmem_size_init, SI_SUB_KMEM, SI_ORDER_ANY, kmem_size_init, NULL);
128
129 uint64_t
130 kmem_size(void)
131 {
132
133         return (kmem_size_val);
134 }
135
136 uint64_t
137 kmem_used(void)
138 {
139
140         return (vmem_size(kmem_arena, VMEM_ALLOC));
141 }
142
143 static int
144 kmem_std_constructor(void *mem, int size __unused, void *private, int flags)
145 {
146         struct kmem_cache *cache = private;
147
148         return (cache->kc_constructor(mem, cache->kc_private, flags));
149 }
150
151 static void
152 kmem_std_destructor(void *mem, int size __unused, void *private)
153 {
154         struct kmem_cache *cache = private;
155
156         cache->kc_destructor(mem, cache->kc_private);
157 }
158
159 kmem_cache_t *
160 kmem_cache_create(char *name, size_t bufsize, size_t align,
161     int (*constructor)(void *, void *, int), void (*destructor)(void *, void *),
162     void (*reclaim)(void *) __unused, void *private, vmem_t *vmp, int cflags)
163 {
164         kmem_cache_t *cache;
165
166         ASSERT(vmp == NULL);
167
168         cache = kmem_alloc(sizeof(*cache), KM_SLEEP);
169         strlcpy(cache->kc_name, name, sizeof(cache->kc_name));
170         cache->kc_constructor = constructor;
171         cache->kc_destructor = destructor;
172         cache->kc_private = private;
173 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
174         cache->kc_zone = uma_zcreate(cache->kc_name, bufsize,
175             constructor != NULL ? kmem_std_constructor : NULL,
176             destructor != NULL ? kmem_std_destructor : NULL,
177             NULL, NULL, align > 0 ? align - 1 : 0, cflags);
178 #else
179         cache->kc_size = bufsize;
180 #endif
181
182         return (cache);
183 }
184
185 void
186 kmem_cache_destroy(kmem_cache_t *cache)
187 {
188 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
189         uma_zdestroy(cache->kc_zone);
190 #endif
191         kmem_free(cache, sizeof(*cache));
192 }
193
194 void *
195 kmem_cache_alloc(kmem_cache_t *cache, int flags)
196 {
197 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
198         return (uma_zalloc_arg(cache->kc_zone, cache, flags));
199 #else
200         void *p;
201
202         p = kmem_alloc(cache->kc_size, flags);
203         if (p != NULL && cache->kc_constructor != NULL)
204                 kmem_std_constructor(p, cache->kc_size, cache, flags);
205         return (p);
206 #endif
207 }
208
209 void
210 kmem_cache_free(kmem_cache_t *cache, void *buf)
211 {
212 #if defined(_KERNEL) && !defined(KMEM_DEBUG)
213         uma_zfree_arg(cache->kc_zone, buf, cache);
214 #else
215         if (cache->kc_destructor != NULL)
216                 kmem_std_destructor(buf, cache->kc_size, cache);
217         kmem_free(buf, cache->kc_size);
218 #endif
219 }
220
221 #ifdef _KERNEL
222 void
223 kmem_cache_reap_now(kmem_cache_t *cache)
224 {
225 #ifndef KMEM_DEBUG
226         zone_drain(cache->kc_zone);
227 #endif
228 }
229
230 void
231 kmem_reap(void)
232 {
233         uma_reclaim();
234 }
235 #else
236 void
237 kmem_cache_reap_now(kmem_cache_t *cache __unused)
238 {
239 }
240
241 void
242 kmem_reap(void)
243 {
244 }
245 #endif
246
247 int
248 kmem_debugging(void)
249 {
250         return (0);
251 }
252
253 void *
254 calloc(size_t n, size_t s)
255 {
256         return (kmem_zalloc(n * s, KM_NOSLEEP));
257 }
258
259 #ifdef KMEM_DEBUG
260 void kmem_show(void *);
261 void
262 kmem_show(void *dummy __unused)
263 {
264         struct kmem_item *i;
265
266         mtx_lock(&kmem_items_mtx);
267         if (LIST_EMPTY(&kmem_items))
268                 printf("KMEM_DEBUG: No leaked elements.\n");
269         else {
270                 printf("KMEM_DEBUG: Leaked elements:\n\n");
271                 LIST_FOREACH(i, &kmem_items, next) {
272                         printf("address=%p\n", i);
273                         stack_print_ddb(&i->stack);
274                         printf("\n");
275                 }
276         }
277         mtx_unlock(&kmem_items_mtx);
278 }
279
280 SYSUNINIT(sol_kmem, SI_SUB_CPU, SI_ORDER_FIRST, kmem_show, NULL);
281 #endif  /* KMEM_DEBUG */