1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericmm
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Slab allocator.
|
---|
36 | *
|
---|
37 | * The slab allocator is closely modelled after OpenSolaris slab allocator.
|
---|
38 | * @see http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/
|
---|
39 | *
|
---|
40 | * with the following exceptions:
|
---|
41 | * @li empty slabs are deallocated immediately
|
---|
42 | * (in Linux they are kept in linked list, in Solaris ???)
|
---|
43 | * @li empty magazines are deallocated when not needed
|
---|
44 | * (in Solaris they are held in linked list in slab cache)
|
---|
45 | *
|
---|
46 | * Following features are not currently supported but would be easy to do:
|
---|
47 | * @li cache coloring
|
---|
48 | * @li dynamic magazine growing (different magazine sizes are already
|
---|
49 | * supported, but we would need to adjust allocation strategy)
|
---|
50 | *
|
---|
51 | * The slab allocator supports per-CPU caches ('magazines') to facilitate
|
---|
52 | * good SMP scaling.
|
---|
53 | *
|
---|
54 | * When a new object is being allocated, it is first checked, if it is
|
---|
55 | * available in a CPU-bound magazine. If it is not found there, it is
|
---|
56 | * allocated from a CPU-shared slab - if a partially full one is found,
|
---|
57 | * it is used, otherwise a new one is allocated.
|
---|
58 | *
|
---|
59 | * When an object is being deallocated, it is put to a CPU-bound magazine.
|
---|
60 | * If there is no such magazine, a new one is allocated (if this fails,
|
---|
61 | * the object is deallocated into slab). If the magazine is full, it is
|
---|
62 | * put into cpu-shared list of magazines and a new one is allocated.
|
---|
63 | *
|
---|
64 | * The CPU-bound magazine is actually a pair of magazines in order to avoid
|
---|
65 | * thrashing when somebody is allocating/deallocating 1 item at the magazine
|
---|
66 | * size boundary. LIFO order is enforced, which should avoid fragmentation
|
---|
67 | * as much as possible.
|
---|
68 | *
|
---|
69 | * Every cache contains list of full slabs and list of partially full slabs.
|
---|
70 | * Empty slabs are immediately freed (thrashing will be avoided because
|
---|
71 | * of magazines).
|
---|
72 | *
|
---|
73 | * The slab information structure is kept inside the data area, if possible.
|
---|
74 | * The cache can be marked that it should not use magazines. This is used
|
---|
75 | * only for slab related caches to avoid deadlocks and infinite recursion
|
---|
76 | * (the slab allocator uses itself for allocating all it's control structures).
|
---|
77 | *
|
---|
78 | * The slab allocator allocates a lot of space and does not free it. When
|
---|
79 | * the frame allocator fails to allocate a frame, it calls slab_reclaim().
|
---|
80 | * It tries 'light reclaim' first, then brutal reclaim. The light reclaim
|
---|
81 | * releases slabs from cpu-shared magazine-list, until at least 1 slab
|
---|
82 | * is deallocated in each cache (this algorithm should probably change).
|
---|
83 | * The brutal reclaim removes all cached objects, even from CPU-bound
|
---|
84 | * magazines.
|
---|
85 | *
|
---|
86 | * @todo
|
---|
87 | * For better CPU-scaling the magazine allocation strategy should
|
---|
88 | * be extended. Currently, if the cache does not have magazine, it asks
|
---|
89 | * for non-cpu cached magazine cache to provide one. It might be feasible
|
---|
90 | * to add cpu-cached magazine cache (which would allocate it's magazines
|
---|
91 | * from non-cpu-cached mag. cache). This would provide a nice per-cpu
|
---|
92 | * buffer. The other possibility is to use the per-cache
|
---|
93 | * 'empty-magazine-list', which decreases competing for 1 per-system
|
---|
94 | * magazine cache.
|
---|
95 | *
|
---|
96 | * @todo
|
---|
97 | * It might be good to add granularity of locks even to slab level,
|
---|
98 | * we could then try_spinlock over all partial slabs and thus improve
|
---|
99 | * scalability even on slab level.
|
---|
100 | *
|
---|
101 | */
|
---|
102 |
|
---|
103 | #include <synch/spinlock.h>
|
---|
104 | #include <mm/slab.h>
|
---|
105 | #include <adt/list.h>
|
---|
106 | #include <memstr.h>
|
---|
107 | #include <align.h>
|
---|
108 | #include <mm/frame.h>
|
---|
109 | #include <config.h>
|
---|
110 | #include <print.h>
|
---|
111 | #include <arch.h>
|
---|
112 | #include <panic.h>
|
---|
113 | #include <debug.h>
|
---|
114 | #include <bitops.h>
|
---|
115 | #include <macros.h>
|
---|
116 |
|
---|
117 | IRQ_SPINLOCK_STATIC_INITIALIZE(slab_cache_lock);
|
---|
118 | static LIST_INITIALIZE(slab_cache_list);
|
---|
119 |
|
---|
120 | /** Magazine cache */
|
---|
121 | static slab_cache_t mag_cache;
|
---|
122 |
|
---|
123 | /** Cache for cache descriptors */
|
---|
124 | static slab_cache_t slab_cache_cache;
|
---|
125 |
|
---|
126 | /** Cache for external slab descriptors
|
---|
127 | * This time we want per-cpu cache, so do not make it static
|
---|
128 | * - using slab for internal slab structures will not deadlock,
|
---|
129 | * as all slab structures are 'small' - control structures of
|
---|
130 | * their caches do not require further allocation
|
---|
131 | */
|
---|
132 | static slab_cache_t *slab_extern_cache;
|
---|
133 |
|
---|
134 | /** Caches for malloc */
|
---|
135 | static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1];
|
---|
136 |
|
---|
137 | static const char *malloc_names[] = {
|
---|
138 | "malloc-16",
|
---|
139 | "malloc-32",
|
---|
140 | "malloc-64",
|
---|
141 | "malloc-128",
|
---|
142 | "malloc-256",
|
---|
143 | "malloc-512",
|
---|
144 | "malloc-1K",
|
---|
145 | "malloc-2K",
|
---|
146 | "malloc-4K",
|
---|
147 | "malloc-8K",
|
---|
148 | "malloc-16K",
|
---|
149 | "malloc-32K",
|
---|
150 | "malloc-64K",
|
---|
151 | "malloc-128K",
|
---|
152 | "malloc-256K",
|
---|
153 | "malloc-512K",
|
---|
154 | "malloc-1M",
|
---|
155 | "malloc-2M",
|
---|
156 | "malloc-4M"
|
---|
157 | };
|
---|
158 |
|
---|
159 | /** Slab descriptor */
|
---|
160 | typedef struct {
|
---|
161 | slab_cache_t *cache; /**< Pointer to parent cache. */
|
---|
162 | link_t link; /**< List of full/partial slabs. */
|
---|
163 | void *start; /**< Start address of first available item. */
|
---|
164 | size_t available; /**< Count of available items in this slab. */
|
---|
165 | size_t nextavail; /**< The index of next available item. */
|
---|
166 | } slab_t;
|
---|
167 |
|
---|
168 | #ifdef CONFIG_DEBUG
|
---|
169 | static unsigned int _slab_initialized = 0;
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | /**************************************/
|
---|
173 | /* Slab allocation functions */
|
---|
174 | /**************************************/
|
---|
175 |
|
---|
176 | /** Allocate frames for slab space and initialize
|
---|
177 | *
|
---|
178 | */
|
---|
179 | NO_TRACE static slab_t *slab_space_alloc(slab_cache_t *cache,
|
---|
180 | unsigned int flags)
|
---|
181 | {
|
---|
182 | size_t zone = 0;
|
---|
183 |
|
---|
184 | void *data = frame_alloc_generic(cache->order, FRAME_KA | flags, &zone);
|
---|
185 | if (!data) {
|
---|
186 | return NULL;
|
---|
187 | }
|
---|
188 |
|
---|
189 | slab_t *slab;
|
---|
190 | size_t fsize;
|
---|
191 |
|
---|
192 | if (!(cache->flags & SLAB_CACHE_SLINSIDE)) {
|
---|
193 | slab = slab_alloc(slab_extern_cache, flags);
|
---|
194 | if (!slab) {
|
---|
195 | frame_free(KA2PA(data));
|
---|
196 | return NULL;
|
---|
197 | }
|
---|
198 | } else {
|
---|
199 | fsize = (PAGE_SIZE << cache->order);
|
---|
200 | slab = data + fsize - sizeof(*slab);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /* Fill in slab structures */
|
---|
204 | size_t i;
|
---|
205 | for (i = 0; i < ((size_t) 1 << cache->order); i++)
|
---|
206 | frame_set_parent(ADDR2PFN(KA2PA(data)) + i, slab, zone);
|
---|
207 |
|
---|
208 | slab->start = data;
|
---|
209 | slab->available = cache->objects;
|
---|
210 | slab->nextavail = 0;
|
---|
211 | slab->cache = cache;
|
---|
212 |
|
---|
213 | for (i = 0; i < cache->objects; i++)
|
---|
214 | *((size_t *) (slab->start + i * cache->size)) = i + 1;
|
---|
215 |
|
---|
216 | atomic_inc(&cache->allocated_slabs);
|
---|
217 | return slab;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /** Deallocate space associated with slab
|
---|
221 | *
|
---|
222 | * @return number of freed frames
|
---|
223 | *
|
---|
224 | */
|
---|
225 | NO_TRACE static size_t slab_space_free(slab_cache_t *cache, slab_t *slab)
|
---|
226 | {
|
---|
227 | frame_free(KA2PA(slab->start));
|
---|
228 | if (!(cache->flags & SLAB_CACHE_SLINSIDE))
|
---|
229 | slab_free(slab_extern_cache, slab);
|
---|
230 |
|
---|
231 | atomic_dec(&cache->allocated_slabs);
|
---|
232 |
|
---|
233 | return (1 << cache->order);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** Map object to slab structure */
|
---|
237 | NO_TRACE static slab_t *obj2slab(void *obj)
|
---|
238 | {
|
---|
239 | return (slab_t *) frame_get_parent(ADDR2PFN(KA2PA(obj)), 0);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /******************/
|
---|
243 | /* Slab functions */
|
---|
244 | /******************/
|
---|
245 |
|
---|
246 | /** Return object to slab and call a destructor
|
---|
247 | *
|
---|
248 | * @param slab If the caller knows directly slab of the object, otherwise NULL
|
---|
249 | *
|
---|
250 | * @return Number of freed pages
|
---|
251 | *
|
---|
252 | */
|
---|
253 | NO_TRACE static size_t slab_obj_destroy(slab_cache_t *cache, void *obj,
|
---|
254 | slab_t *slab)
|
---|
255 | {
|
---|
256 | if (!slab)
|
---|
257 | slab = obj2slab(obj);
|
---|
258 |
|
---|
259 | ASSERT(slab->cache == cache);
|
---|
260 |
|
---|
261 | size_t freed = 0;
|
---|
262 |
|
---|
263 | if (cache->destructor)
|
---|
264 | freed = cache->destructor(obj);
|
---|
265 |
|
---|
266 | spinlock_lock(&cache->slablock);
|
---|
267 | ASSERT(slab->available < cache->objects);
|
---|
268 |
|
---|
269 | *((size_t *) obj) = slab->nextavail;
|
---|
270 | slab->nextavail = (obj - slab->start) / cache->size;
|
---|
271 | slab->available++;
|
---|
272 |
|
---|
273 | /* Move it to correct list */
|
---|
274 | if (slab->available == cache->objects) {
|
---|
275 | /* Free associated memory */
|
---|
276 | list_remove(&slab->link);
|
---|
277 | spinlock_unlock(&cache->slablock);
|
---|
278 |
|
---|
279 | return freed + slab_space_free(cache, slab);
|
---|
280 | } else if (slab->available == 1) {
|
---|
281 | /* It was in full, move to partial */
|
---|
282 | list_remove(&slab->link);
|
---|
283 | list_prepend(&slab->link, &cache->partial_slabs);
|
---|
284 | }
|
---|
285 |
|
---|
286 | spinlock_unlock(&cache->slablock);
|
---|
287 | return freed;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /** Take new object from slab or create new if needed
|
---|
291 | *
|
---|
292 | * @return Object address or null
|
---|
293 | *
|
---|
294 | */
|
---|
295 | NO_TRACE static void *slab_obj_create(slab_cache_t *cache, unsigned int flags)
|
---|
296 | {
|
---|
297 | spinlock_lock(&cache->slablock);
|
---|
298 |
|
---|
299 | slab_t *slab;
|
---|
300 |
|
---|
301 | if (list_empty(&cache->partial_slabs)) {
|
---|
302 | /*
|
---|
303 | * Allow recursion and reclaiming
|
---|
304 | * - this should work, as the slab control structures
|
---|
305 | * are small and do not need to allocate with anything
|
---|
306 | * other than frame_alloc when they are allocating,
|
---|
307 | * that's why we should get recursion at most 1-level deep
|
---|
308 | *
|
---|
309 | */
|
---|
310 | spinlock_unlock(&cache->slablock);
|
---|
311 | slab = slab_space_alloc(cache, flags);
|
---|
312 | if (!slab)
|
---|
313 | return NULL;
|
---|
314 |
|
---|
315 | spinlock_lock(&cache->slablock);
|
---|
316 | } else {
|
---|
317 | slab = list_get_instance(list_first(&cache->partial_slabs),
|
---|
318 | slab_t, link);
|
---|
319 | list_remove(&slab->link);
|
---|
320 | }
|
---|
321 |
|
---|
322 | void *obj = slab->start + slab->nextavail * cache->size;
|
---|
323 | slab->nextavail = *((size_t *) obj);
|
---|
324 | slab->available--;
|
---|
325 |
|
---|
326 | if (!slab->available)
|
---|
327 | list_prepend(&slab->link, &cache->full_slabs);
|
---|
328 | else
|
---|
329 | list_prepend(&slab->link, &cache->partial_slabs);
|
---|
330 |
|
---|
331 | spinlock_unlock(&cache->slablock);
|
---|
332 |
|
---|
333 | if ((cache->constructor) && (cache->constructor(obj, flags))) {
|
---|
334 | /* Bad, bad, construction failed */
|
---|
335 | slab_obj_destroy(cache, obj, slab);
|
---|
336 | return NULL;
|
---|
337 | }
|
---|
338 |
|
---|
339 | return obj;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /****************************/
|
---|
343 | /* CPU-Cache slab functions */
|
---|
344 | /****************************/
|
---|
345 |
|
---|
346 | /** Find a full magazine in cache, take it from list and return it
|
---|
347 | *
|
---|
348 | * @param first If true, return first, else last mag.
|
---|
349 | *
|
---|
350 | */
|
---|
351 | NO_TRACE static slab_magazine_t *get_mag_from_cache(slab_cache_t *cache,
|
---|
352 | bool first)
|
---|
353 | {
|
---|
354 | slab_magazine_t *mag = NULL;
|
---|
355 | link_t *cur;
|
---|
356 |
|
---|
357 | spinlock_lock(&cache->maglock);
|
---|
358 | if (!list_empty(&cache->magazines)) {
|
---|
359 | if (first)
|
---|
360 | cur = list_first(&cache->magazines);
|
---|
361 | else
|
---|
362 | cur = list_last(&cache->magazines);
|
---|
363 |
|
---|
364 | mag = list_get_instance(cur, slab_magazine_t, link);
|
---|
365 | list_remove(&mag->link);
|
---|
366 | atomic_dec(&cache->magazine_counter);
|
---|
367 | }
|
---|
368 | spinlock_unlock(&cache->maglock);
|
---|
369 |
|
---|
370 | return mag;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /** Prepend magazine to magazine list in cache
|
---|
374 | *
|
---|
375 | */
|
---|
376 | NO_TRACE static void put_mag_to_cache(slab_cache_t *cache,
|
---|
377 | slab_magazine_t *mag)
|
---|
378 | {
|
---|
379 | spinlock_lock(&cache->maglock);
|
---|
380 |
|
---|
381 | list_prepend(&mag->link, &cache->magazines);
|
---|
382 | atomic_inc(&cache->magazine_counter);
|
---|
383 |
|
---|
384 | spinlock_unlock(&cache->maglock);
|
---|
385 | }
|
---|
386 |
|
---|
387 | /** Free all objects in magazine and free memory associated with magazine
|
---|
388 | *
|
---|
389 | * @return Number of freed pages
|
---|
390 | *
|
---|
391 | */
|
---|
392 | NO_TRACE static size_t magazine_destroy(slab_cache_t *cache,
|
---|
393 | slab_magazine_t *mag)
|
---|
394 | {
|
---|
395 | size_t i;
|
---|
396 | size_t frames = 0;
|
---|
397 |
|
---|
398 | for (i = 0; i < mag->busy; i++) {
|
---|
399 | frames += slab_obj_destroy(cache, mag->objs[i], NULL);
|
---|
400 | atomic_dec(&cache->cached_objs);
|
---|
401 | }
|
---|
402 |
|
---|
403 | slab_free(&mag_cache, mag);
|
---|
404 |
|
---|
405 | return frames;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /** Find full magazine, set it as current and return it
|
---|
409 | *
|
---|
410 | */
|
---|
411 | NO_TRACE static slab_magazine_t *get_full_current_mag(slab_cache_t *cache)
|
---|
412 | {
|
---|
413 | slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
|
---|
414 | slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
|
---|
415 |
|
---|
416 | ASSERT(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
|
---|
417 |
|
---|
418 | if (cmag) { /* First try local CPU magazines */
|
---|
419 | if (cmag->busy)
|
---|
420 | return cmag;
|
---|
421 |
|
---|
422 | if ((lastmag) && (lastmag->busy)) {
|
---|
423 | cache->mag_cache[CPU->id].current = lastmag;
|
---|
424 | cache->mag_cache[CPU->id].last = cmag;
|
---|
425 | return lastmag;
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | /* Local magazines are empty, import one from magazine list */
|
---|
430 | slab_magazine_t *newmag = get_mag_from_cache(cache, 1);
|
---|
431 | if (!newmag)
|
---|
432 | return NULL;
|
---|
433 |
|
---|
434 | if (lastmag)
|
---|
435 | magazine_destroy(cache, lastmag);
|
---|
436 |
|
---|
437 | cache->mag_cache[CPU->id].last = cmag;
|
---|
438 | cache->mag_cache[CPU->id].current = newmag;
|
---|
439 |
|
---|
440 | return newmag;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /** Try to find object in CPU-cache magazines
|
---|
444 | *
|
---|
445 | * @return Pointer to object or NULL if not available
|
---|
446 | *
|
---|
447 | */
|
---|
448 | NO_TRACE static void *magazine_obj_get(slab_cache_t *cache)
|
---|
449 | {
|
---|
450 | if (!CPU)
|
---|
451 | return NULL;
|
---|
452 |
|
---|
453 | irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
|
---|
454 |
|
---|
455 | slab_magazine_t *mag = get_full_current_mag(cache);
|
---|
456 | if (!mag) {
|
---|
457 | irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
|
---|
458 | return NULL;
|
---|
459 | }
|
---|
460 |
|
---|
461 | void *obj = mag->objs[--mag->busy];
|
---|
462 | irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
|
---|
463 |
|
---|
464 | atomic_dec(&cache->cached_objs);
|
---|
465 |
|
---|
466 | return obj;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /** Assure that the current magazine is empty, return pointer to it,
|
---|
470 | * or NULL if no empty magazine is available and cannot be allocated
|
---|
471 | *
|
---|
472 | * We have 2 magazines bound to processor.
|
---|
473 | * First try the current.
|
---|
474 | * If full, try the last.
|
---|
475 | * If full, put to magazines list.
|
---|
476 | *
|
---|
477 | */
|
---|
478 | NO_TRACE static slab_magazine_t *make_empty_current_mag(slab_cache_t *cache)
|
---|
479 | {
|
---|
480 | slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
|
---|
481 | slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
|
---|
482 |
|
---|
483 | ASSERT(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
|
---|
484 |
|
---|
485 | if (cmag) {
|
---|
486 | if (cmag->busy < cmag->size)
|
---|
487 | return cmag;
|
---|
488 |
|
---|
489 | if ((lastmag) && (lastmag->busy < lastmag->size)) {
|
---|
490 | cache->mag_cache[CPU->id].last = cmag;
|
---|
491 | cache->mag_cache[CPU->id].current = lastmag;
|
---|
492 | return lastmag;
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* current | last are full | nonexistent, allocate new */
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * We do not want to sleep just because of caching,
|
---|
500 | * especially we do not want reclaiming to start, as
|
---|
501 | * this would deadlock.
|
---|
502 | *
|
---|
503 | */
|
---|
504 | slab_magazine_t *newmag = slab_alloc(&mag_cache,
|
---|
505 | FRAME_ATOMIC | FRAME_NO_RECLAIM);
|
---|
506 | if (!newmag)
|
---|
507 | return NULL;
|
---|
508 |
|
---|
509 | newmag->size = SLAB_MAG_SIZE;
|
---|
510 | newmag->busy = 0;
|
---|
511 |
|
---|
512 | /* Flush last to magazine list */
|
---|
513 | if (lastmag)
|
---|
514 | put_mag_to_cache(cache, lastmag);
|
---|
515 |
|
---|
516 | /* Move current as last, save new as current */
|
---|
517 | cache->mag_cache[CPU->id].last = cmag;
|
---|
518 | cache->mag_cache[CPU->id].current = newmag;
|
---|
519 |
|
---|
520 | return newmag;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /** Put object into CPU-cache magazine
|
---|
524 | *
|
---|
525 | * @return 0 on success, -1 on no memory
|
---|
526 | *
|
---|
527 | */
|
---|
528 | NO_TRACE static int magazine_obj_put(slab_cache_t *cache, void *obj)
|
---|
529 | {
|
---|
530 | if (!CPU)
|
---|
531 | return -1;
|
---|
532 |
|
---|
533 | irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
|
---|
534 |
|
---|
535 | slab_magazine_t *mag = make_empty_current_mag(cache);
|
---|
536 | if (!mag) {
|
---|
537 | irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
|
---|
538 | return -1;
|
---|
539 | }
|
---|
540 |
|
---|
541 | mag->objs[mag->busy++] = obj;
|
---|
542 |
|
---|
543 | irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
|
---|
544 |
|
---|
545 | atomic_inc(&cache->cached_objs);
|
---|
546 |
|
---|
547 | return 0;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /************************/
|
---|
551 | /* Slab cache functions */
|
---|
552 | /************************/
|
---|
553 |
|
---|
554 | /** Return number of objects that fit in certain cache size
|
---|
555 | *
|
---|
556 | */
|
---|
557 | NO_TRACE static size_t comp_objects(slab_cache_t *cache)
|
---|
558 | {
|
---|
559 | if (cache->flags & SLAB_CACHE_SLINSIDE)
|
---|
560 | return ((PAGE_SIZE << cache->order)
|
---|
561 | - sizeof(slab_t)) / cache->size;
|
---|
562 | else
|
---|
563 | return (PAGE_SIZE << cache->order) / cache->size;
|
---|
564 | }
|
---|
565 |
|
---|
566 | /** Return wasted space in slab
|
---|
567 | *
|
---|
568 | */
|
---|
569 | NO_TRACE static size_t badness(slab_cache_t *cache)
|
---|
570 | {
|
---|
571 | size_t objects = comp_objects(cache);
|
---|
572 | size_t ssize = PAGE_SIZE << cache->order;
|
---|
573 |
|
---|
574 | if (cache->flags & SLAB_CACHE_SLINSIDE)
|
---|
575 | ssize -= sizeof(slab_t);
|
---|
576 |
|
---|
577 | return ssize - objects * cache->size;
|
---|
578 | }
|
---|
579 |
|
---|
580 | /** Initialize mag_cache structure in slab cache
|
---|
581 | *
|
---|
582 | */
|
---|
583 | NO_TRACE static bool make_magcache(slab_cache_t *cache)
|
---|
584 | {
|
---|
585 | ASSERT(_slab_initialized >= 2);
|
---|
586 |
|
---|
587 | cache->mag_cache = malloc(sizeof(slab_mag_cache_t) * config.cpu_count,
|
---|
588 | FRAME_ATOMIC);
|
---|
589 | if (!cache->mag_cache)
|
---|
590 | return false;
|
---|
591 |
|
---|
592 | size_t i;
|
---|
593 | for (i = 0; i < config.cpu_count; i++) {
|
---|
594 | memsetb(&cache->mag_cache[i], sizeof(cache->mag_cache[i]), 0);
|
---|
595 | irq_spinlock_initialize(&cache->mag_cache[i].lock,
|
---|
596 | "slab.cache.mag_cache[].lock");
|
---|
597 | }
|
---|
598 |
|
---|
599 | return true;
|
---|
600 | }
|
---|
601 |
|
---|
602 | /** Initialize allocated memory as a slab cache
|
---|
603 | *
|
---|
604 | */
|
---|
605 | NO_TRACE static void _slab_cache_create(slab_cache_t *cache, const char *name,
|
---|
606 | size_t size, size_t align, int (*constructor)(void *obj,
|
---|
607 | unsigned int kmflag), size_t (*destructor)(void *obj), unsigned int flags)
|
---|
608 | {
|
---|
609 | memsetb(cache, sizeof(*cache), 0);
|
---|
610 | cache->name = name;
|
---|
611 |
|
---|
612 | if (align < sizeof(sysarg_t))
|
---|
613 | align = sizeof(sysarg_t);
|
---|
614 |
|
---|
615 | size = ALIGN_UP(size, align);
|
---|
616 |
|
---|
617 | cache->size = size;
|
---|
618 | cache->constructor = constructor;
|
---|
619 | cache->destructor = destructor;
|
---|
620 | cache->flags = flags;
|
---|
621 |
|
---|
622 | list_initialize(&cache->full_slabs);
|
---|
623 | list_initialize(&cache->partial_slabs);
|
---|
624 | list_initialize(&cache->magazines);
|
---|
625 |
|
---|
626 | spinlock_initialize(&cache->slablock, "slab.cache.slablock");
|
---|
627 | spinlock_initialize(&cache->maglock, "slab.cache.maglock");
|
---|
628 |
|
---|
629 | if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
|
---|
630 | (void) make_magcache(cache);
|
---|
631 |
|
---|
632 | /* Compute slab sizes, object counts in slabs etc. */
|
---|
633 | if (cache->size < SLAB_INSIDE_SIZE)
|
---|
634 | cache->flags |= SLAB_CACHE_SLINSIDE;
|
---|
635 |
|
---|
636 | /* Minimum slab order */
|
---|
637 | size_t pages = SIZE2FRAMES(cache->size);
|
---|
638 |
|
---|
639 | /* We need the 2^order >= pages */
|
---|
640 | if (pages == 1)
|
---|
641 | cache->order = 0;
|
---|
642 | else
|
---|
643 | cache->order = fnzb(pages - 1) + 1;
|
---|
644 |
|
---|
645 | while (badness(cache) > SLAB_MAX_BADNESS(cache))
|
---|
646 | cache->order += 1;
|
---|
647 |
|
---|
648 | cache->objects = comp_objects(cache);
|
---|
649 |
|
---|
650 | /* If info fits in, put it inside */
|
---|
651 | if (badness(cache) > sizeof(slab_t))
|
---|
652 | cache->flags |= SLAB_CACHE_SLINSIDE;
|
---|
653 |
|
---|
654 | /* Add cache to cache list */
|
---|
655 | irq_spinlock_lock(&slab_cache_lock, true);
|
---|
656 | list_append(&cache->link, &slab_cache_list);
|
---|
657 | irq_spinlock_unlock(&slab_cache_lock, true);
|
---|
658 | }
|
---|
659 |
|
---|
660 | /** Create slab cache
|
---|
661 | *
|
---|
662 | */
|
---|
663 | slab_cache_t *slab_cache_create(const char *name, size_t size, size_t align,
|
---|
664 | int (*constructor)(void *obj, unsigned int kmflag),
|
---|
665 | size_t (*destructor)(void *obj), unsigned int flags)
|
---|
666 | {
|
---|
667 | slab_cache_t *cache = slab_alloc(&slab_cache_cache, 0);
|
---|
668 | _slab_cache_create(cache, name, size, align, constructor, destructor,
|
---|
669 | flags);
|
---|
670 |
|
---|
671 | return cache;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /** Reclaim space occupied by objects that are already free
|
---|
675 | *
|
---|
676 | * @param flags If contains SLAB_RECLAIM_ALL, do aggressive freeing
|
---|
677 | *
|
---|
678 | * @return Number of freed pages
|
---|
679 | *
|
---|
680 | */
|
---|
681 | NO_TRACE static size_t _slab_reclaim(slab_cache_t *cache, unsigned int flags)
|
---|
682 | {
|
---|
683 | if (cache->flags & SLAB_CACHE_NOMAGAZINE)
|
---|
684 | return 0; /* Nothing to do */
|
---|
685 |
|
---|
686 | /*
|
---|
687 | * We count up to original magazine count to avoid
|
---|
688 | * endless loop
|
---|
689 | */
|
---|
690 | atomic_count_t magcount = atomic_get(&cache->magazine_counter);
|
---|
691 |
|
---|
692 | slab_magazine_t *mag;
|
---|
693 | size_t frames = 0;
|
---|
694 |
|
---|
695 | while ((magcount--) && (mag = get_mag_from_cache(cache, 0))) {
|
---|
696 | frames += magazine_destroy(cache, mag);
|
---|
697 | if ((!(flags & SLAB_RECLAIM_ALL)) && (frames))
|
---|
698 | break;
|
---|
699 | }
|
---|
700 |
|
---|
701 | if (flags & SLAB_RECLAIM_ALL) {
|
---|
702 | /* Free cpu-bound magazines */
|
---|
703 | /* Destroy CPU magazines */
|
---|
704 | size_t i;
|
---|
705 | for (i = 0; i < config.cpu_count; i++) {
|
---|
706 | irq_spinlock_lock(&cache->mag_cache[i].lock, true);
|
---|
707 |
|
---|
708 | mag = cache->mag_cache[i].current;
|
---|
709 | if (mag)
|
---|
710 | frames += magazine_destroy(cache, mag);
|
---|
711 | cache->mag_cache[i].current = NULL;
|
---|
712 |
|
---|
713 | mag = cache->mag_cache[i].last;
|
---|
714 | if (mag)
|
---|
715 | frames += magazine_destroy(cache, mag);
|
---|
716 | cache->mag_cache[i].last = NULL;
|
---|
717 |
|
---|
718 | irq_spinlock_unlock(&cache->mag_cache[i].lock, true);
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | return frames;
|
---|
723 | }
|
---|
724 |
|
---|
725 | /** Check that there are no slabs and remove cache from system
|
---|
726 | *
|
---|
727 | */
|
---|
728 | void slab_cache_destroy(slab_cache_t *cache)
|
---|
729 | {
|
---|
730 | /*
|
---|
731 | * First remove cache from link, so that we don't need
|
---|
732 | * to disable interrupts later
|
---|
733 | *
|
---|
734 | */
|
---|
735 | irq_spinlock_lock(&slab_cache_lock, true);
|
---|
736 | list_remove(&cache->link);
|
---|
737 | irq_spinlock_unlock(&slab_cache_lock, true);
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * Do not lock anything, we assume the software is correct and
|
---|
741 | * does not touch the cache when it decides to destroy it
|
---|
742 | *
|
---|
743 | */
|
---|
744 |
|
---|
745 | /* Destroy all magazines */
|
---|
746 | _slab_reclaim(cache, SLAB_RECLAIM_ALL);
|
---|
747 |
|
---|
748 | /* All slabs must be empty */
|
---|
749 | if ((!list_empty(&cache->full_slabs)) ||
|
---|
750 | (!list_empty(&cache->partial_slabs)))
|
---|
751 | panic("Destroying cache that is not empty.");
|
---|
752 |
|
---|
753 | if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
|
---|
754 | free(cache->mag_cache);
|
---|
755 |
|
---|
756 | slab_free(&slab_cache_cache, cache);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /** Allocate new object from cache - if no flags given, always returns memory
|
---|
760 | *
|
---|
761 | */
|
---|
762 | void *slab_alloc(slab_cache_t *cache, unsigned int flags)
|
---|
763 | {
|
---|
764 | /* Disable interrupts to avoid deadlocks with interrupt handlers */
|
---|
765 | ipl_t ipl = interrupts_disable();
|
---|
766 |
|
---|
767 | void *result = NULL;
|
---|
768 |
|
---|
769 | if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
|
---|
770 | result = magazine_obj_get(cache);
|
---|
771 |
|
---|
772 | if (!result)
|
---|
773 | result = slab_obj_create(cache, flags);
|
---|
774 |
|
---|
775 | interrupts_restore(ipl);
|
---|
776 |
|
---|
777 | if (result)
|
---|
778 | atomic_inc(&cache->allocated_objs);
|
---|
779 |
|
---|
780 | return result;
|
---|
781 | }
|
---|
782 |
|
---|
783 | /** Return object to cache, use slab if known
|
---|
784 | *
|
---|
785 | */
|
---|
786 | NO_TRACE static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab)
|
---|
787 | {
|
---|
788 | ipl_t ipl = interrupts_disable();
|
---|
789 |
|
---|
790 | if ((cache->flags & SLAB_CACHE_NOMAGAZINE) ||
|
---|
791 | (magazine_obj_put(cache, obj)))
|
---|
792 | slab_obj_destroy(cache, obj, slab);
|
---|
793 |
|
---|
794 | interrupts_restore(ipl);
|
---|
795 | atomic_dec(&cache->allocated_objs);
|
---|
796 | }
|
---|
797 |
|
---|
798 | /** Return slab object to cache
|
---|
799 | *
|
---|
800 | */
|
---|
801 | void slab_free(slab_cache_t *cache, void *obj)
|
---|
802 | {
|
---|
803 | _slab_free(cache, obj, NULL);
|
---|
804 | }
|
---|
805 |
|
---|
806 | /** Go through all caches and reclaim what is possible */
|
---|
807 | size_t slab_reclaim(unsigned int flags)
|
---|
808 | {
|
---|
809 | irq_spinlock_lock(&slab_cache_lock, true);
|
---|
810 |
|
---|
811 | size_t frames = 0;
|
---|
812 | list_foreach(slab_cache_list, cur) {
|
---|
813 | slab_cache_t *cache = list_get_instance(cur, slab_cache_t, link);
|
---|
814 | frames += _slab_reclaim(cache, flags);
|
---|
815 | }
|
---|
816 |
|
---|
817 | irq_spinlock_unlock(&slab_cache_lock, true);
|
---|
818 |
|
---|
819 | return frames;
|
---|
820 | }
|
---|
821 |
|
---|
822 | /* Print list of slabs
|
---|
823 | *
|
---|
824 | */
|
---|
825 | void slab_print_list(void)
|
---|
826 | {
|
---|
827 | printf("[slab name ] [size ] [pages ] [obj/pg] [slabs ]"
|
---|
828 | " [cached] [alloc ] [ctl]\n");
|
---|
829 |
|
---|
830 | size_t skip = 0;
|
---|
831 | while (true) {
|
---|
832 | /*
|
---|
833 | * We must not hold the slab_cache_lock spinlock when printing
|
---|
834 | * the statistics. Otherwise we can easily deadlock if the print
|
---|
835 | * needs to allocate memory.
|
---|
836 | *
|
---|
837 | * Therefore, we walk through the slab cache list, skipping some
|
---|
838 | * amount of already processed caches during each iteration and
|
---|
839 | * gathering statistics about the first unprocessed cache. For
|
---|
840 | * the sake of printing the statistics, we realese the
|
---|
841 | * slab_cache_lock and reacquire it afterwards. Then the walk
|
---|
842 | * starts again.
|
---|
843 | *
|
---|
844 | * This limits both the efficiency and also accuracy of the
|
---|
845 | * obtained statistics. The efficiency is decreased because the
|
---|
846 | * time complexity of the algorithm is quadratic instead of
|
---|
847 | * linear. The accuracy is impacted because we drop the lock
|
---|
848 | * after processing one cache. If there is someone else
|
---|
849 | * manipulating the cache list, we might omit an arbitrary
|
---|
850 | * number of caches or process one cache multiple times.
|
---|
851 | * However, we don't bleed for this algorithm for it is only
|
---|
852 | * statistics.
|
---|
853 | */
|
---|
854 |
|
---|
855 | irq_spinlock_lock(&slab_cache_lock, true);
|
---|
856 |
|
---|
857 | link_t *cur;
|
---|
858 | size_t i;
|
---|
859 | for (i = 0, cur = slab_cache_list.head.next;
|
---|
860 | (i < skip) && (cur != &slab_cache_list.head);
|
---|
861 | i++, cur = cur->next);
|
---|
862 |
|
---|
863 | if (cur == &slab_cache_list.head) {
|
---|
864 | irq_spinlock_unlock(&slab_cache_lock, true);
|
---|
865 | break;
|
---|
866 | }
|
---|
867 |
|
---|
868 | skip++;
|
---|
869 |
|
---|
870 | slab_cache_t *cache = list_get_instance(cur, slab_cache_t, link);
|
---|
871 |
|
---|
872 | const char *name = cache->name;
|
---|
873 | uint8_t order = cache->order;
|
---|
874 | size_t size = cache->size;
|
---|
875 | size_t objects = cache->objects;
|
---|
876 | long allocated_slabs = atomic_get(&cache->allocated_slabs);
|
---|
877 | long cached_objs = atomic_get(&cache->cached_objs);
|
---|
878 | long allocated_objs = atomic_get(&cache->allocated_objs);
|
---|
879 | unsigned int flags = cache->flags;
|
---|
880 |
|
---|
881 | irq_spinlock_unlock(&slab_cache_lock, true);
|
---|
882 |
|
---|
883 | printf("%-18s %8zu %8u %8zu %8ld %8ld %8ld %-5s\n",
|
---|
884 | name, size, (1 << order), objects, allocated_slabs,
|
---|
885 | cached_objs, allocated_objs,
|
---|
886 | flags & SLAB_CACHE_SLINSIDE ? "in" : "out");
|
---|
887 | }
|
---|
888 | }
|
---|
889 |
|
---|
890 | void slab_cache_init(void)
|
---|
891 | {
|
---|
892 | /* Initialize magazine cache */
|
---|
893 | _slab_cache_create(&mag_cache, "slab_magazine",
|
---|
894 | sizeof(slab_magazine_t) + SLAB_MAG_SIZE * sizeof(void*),
|
---|
895 | sizeof(uintptr_t), NULL, NULL, SLAB_CACHE_NOMAGAZINE |
|
---|
896 | SLAB_CACHE_SLINSIDE);
|
---|
897 |
|
---|
898 | /* Initialize slab_cache cache */
|
---|
899 | _slab_cache_create(&slab_cache_cache, "slab_cache",
|
---|
900 | sizeof(slab_cache_cache), sizeof(uintptr_t), NULL, NULL,
|
---|
901 | SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
|
---|
902 |
|
---|
903 | /* Initialize external slab cache */
|
---|
904 | slab_extern_cache = slab_cache_create("slab_extern", sizeof(slab_t), 0,
|
---|
905 | NULL, NULL, SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED);
|
---|
906 |
|
---|
907 | /* Initialize structures for malloc */
|
---|
908 | size_t i;
|
---|
909 | size_t size;
|
---|
910 |
|
---|
911 | for (i = 0, size = (1 << SLAB_MIN_MALLOC_W);
|
---|
912 | i < (SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1);
|
---|
913 | i++, size <<= 1) {
|
---|
914 | malloc_caches[i] = slab_cache_create(malloc_names[i], size, 0,
|
---|
915 | NULL, NULL, SLAB_CACHE_MAGDEFERRED);
|
---|
916 | }
|
---|
917 |
|
---|
918 | #ifdef CONFIG_DEBUG
|
---|
919 | _slab_initialized = 1;
|
---|
920 | #endif
|
---|
921 | }
|
---|
922 |
|
---|
923 | /** Enable cpu_cache
|
---|
924 | *
|
---|
925 | * Kernel calls this function, when it knows the real number of
|
---|
926 | * processors. Allocate slab for cpucache and enable it on all
|
---|
927 | * existing slabs that are SLAB_CACHE_MAGDEFERRED
|
---|
928 | *
|
---|
929 | */
|
---|
930 | void slab_enable_cpucache(void)
|
---|
931 | {
|
---|
932 | #ifdef CONFIG_DEBUG
|
---|
933 | _slab_initialized = 2;
|
---|
934 | #endif
|
---|
935 |
|
---|
936 | irq_spinlock_lock(&slab_cache_lock, false);
|
---|
937 |
|
---|
938 | list_foreach(slab_cache_list, cur) {
|
---|
939 | slab_cache_t *slab = list_get_instance(cur, slab_cache_t, link);
|
---|
940 | if ((slab->flags & SLAB_CACHE_MAGDEFERRED) !=
|
---|
941 | SLAB_CACHE_MAGDEFERRED)
|
---|
942 | continue;
|
---|
943 |
|
---|
944 | (void) make_magcache(slab);
|
---|
945 | slab->flags &= ~SLAB_CACHE_MAGDEFERRED;
|
---|
946 | }
|
---|
947 |
|
---|
948 | irq_spinlock_unlock(&slab_cache_lock, false);
|
---|
949 | }
|
---|
950 |
|
---|
951 | void *malloc(size_t size, unsigned int flags)
|
---|
952 | {
|
---|
953 | ASSERT(_slab_initialized);
|
---|
954 | ASSERT(size <= (1 << SLAB_MAX_MALLOC_W));
|
---|
955 |
|
---|
956 | if (size < (1 << SLAB_MIN_MALLOC_W))
|
---|
957 | size = (1 << SLAB_MIN_MALLOC_W);
|
---|
958 |
|
---|
959 | uint8_t idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
|
---|
960 |
|
---|
961 | return slab_alloc(malloc_caches[idx], flags);
|
---|
962 | }
|
---|
963 |
|
---|
964 | void *realloc(void *ptr, size_t size, unsigned int flags)
|
---|
965 | {
|
---|
966 | ASSERT(_slab_initialized);
|
---|
967 | ASSERT(size <= (1 << SLAB_MAX_MALLOC_W));
|
---|
968 |
|
---|
969 | void *new_ptr;
|
---|
970 |
|
---|
971 | if (size > 0) {
|
---|
972 | if (size < (1 << SLAB_MIN_MALLOC_W))
|
---|
973 | size = (1 << SLAB_MIN_MALLOC_W);
|
---|
974 | uint8_t idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
|
---|
975 |
|
---|
976 | new_ptr = slab_alloc(malloc_caches[idx], flags);
|
---|
977 | } else
|
---|
978 | new_ptr = NULL;
|
---|
979 |
|
---|
980 | if ((new_ptr != NULL) && (ptr != NULL)) {
|
---|
981 | slab_t *slab = obj2slab(ptr);
|
---|
982 | memcpy(new_ptr, ptr, min(size, slab->cache->size));
|
---|
983 | }
|
---|
984 |
|
---|
985 | if (ptr != NULL)
|
---|
986 | free(ptr);
|
---|
987 |
|
---|
988 | return new_ptr;
|
---|
989 | }
|
---|
990 |
|
---|
991 | void free(void *ptr)
|
---|
992 | {
|
---|
993 | if (!ptr)
|
---|
994 | return;
|
---|
995 |
|
---|
996 | slab_t *slab = obj2slab(ptr);
|
---|
997 | _slab_free(slab->cache, ptr, slab);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /** @}
|
---|
1001 | */
|
---|