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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36f0738 was 7f11dc6, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Numerous minor error code tweaks in kernel.

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