source: mainline/kernel/generic/src/mm/slab.c@ 05913fe7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 05913fe7 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

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