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

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

Declare malloc() etc in standard <stdlib.h> rather than <mm/slab.h>

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