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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

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