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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ab6f2507 was ab6f2507, checked in by Jakub Jermar <jakub@…>, 14 years ago

Make sure that slab_reclaim() executes with interrupts disabled.
There is actually no need to explicitly disable interrupts for
slab_reclaim() in the calling function when it can do it itself.

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