Rev 1702 | Rev 1760 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
759 | palkovsky | 1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1757 | jermar | 29 | /** @addtogroup genericmm |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1248 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1248 | jermar | 35 | * @brief Slab allocator. |
769 | palkovsky | 36 | * |
1248 | jermar | 37 | * The slab allocator is closely modelled after OpenSolaris slab allocator. |
38 | * @see http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/ |
||
39 | * |
||
769 | palkovsky | 40 | * with the following exceptions: |
1248 | jermar | 41 | * @li empty slabs are deallocated immediately |
769 | palkovsky | 42 | * (in Linux they are kept in linked list, in Solaris ???) |
1248 | jermar | 43 | * @li empty magazines are deallocated when not needed |
769 | palkovsky | 44 | * (in Solaris they are held in linked list in slab cache) |
45 | * |
||
1248 | jermar | 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 |
||
1144 | jermar | 49 | * supported, but we would need to adjust allocation strategy) |
769 | palkovsky | 50 | * |
1248 | jermar | 51 | * The slab allocator supports per-CPU caches ('magazines') to facilitate |
769 | palkovsky | 52 | * good SMP scaling. |
53 | * |
||
54 | * When a new object is being allocated, it is first checked, if it is |
||
1554 | jermar | 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. |
||
769 | palkovsky | 58 | * |
1554 | jermar | 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, |
||
1248 | jermar | 61 | * the object is deallocated into slab). If the magazine is full, it is |
1554 | jermar | 62 | * put into cpu-shared list of magazines and a new one is allocated. |
769 | palkovsky | 63 | * |
1554 | jermar | 64 | * The CPU-bound magazine is actually a pair of magazines in order to avoid |
769 | palkovsky | 65 | * thrashing when somebody is allocating/deallocating 1 item at the magazine |
66 | * size boundary. LIFO order is enforced, which should avoid fragmentation |
||
67 | * as much as possible. |
||
68 | * |
||
1554 | jermar | 69 | * Every cache contains list of full slabs and list of partially full slabs. |
1248 | jermar | 70 | * Empty slabs are immediately freed (thrashing will be avoided because |
769 | palkovsky | 71 | * of magazines). |
72 | * |
||
1248 | jermar | 73 | * The slab information structure is kept inside the data area, if possible. |
769 | palkovsky | 74 | * The cache can be marked that it should not use magazines. This is used |
1248 | jermar | 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). |
||
769 | palkovsky | 77 | * |
1554 | jermar | 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(). |
||
769 | palkovsky | 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 | * |
||
1757 | jermar | 86 | * @todo |
1248 | jermar | 87 | * For better CPU-scaling the magazine allocation strategy should |
775 | palkovsky | 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 | * |
||
1757 | jermar | 96 | * @todo |
97 | * it might be good to add granularity of locks even to slab level, |
||
98 | * we could then try_spinlock over all partial slabs and thus improve |
||
99 | * scalability even on slab level |
||
769 | palkovsky | 100 | */ |
101 | |||
759 | palkovsky | 102 | #include <synch/spinlock.h> |
103 | #include <mm/slab.h> |
||
788 | jermar | 104 | #include <adt/list.h> |
759 | palkovsky | 105 | #include <memstr.h> |
106 | #include <align.h> |
||
762 | palkovsky | 107 | #include <mm/frame.h> |
759 | palkovsky | 108 | #include <config.h> |
109 | #include <print.h> |
||
110 | #include <arch.h> |
||
111 | #include <panic.h> |
||
762 | palkovsky | 112 | #include <debug.h> |
771 | palkovsky | 113 | #include <bitops.h> |
759 | palkovsky | 114 | |
115 | SPINLOCK_INITIALIZE(slab_cache_lock); |
||
769 | palkovsky | 116 | static LIST_INITIALIZE(slab_cache_list); |
759 | palkovsky | 117 | |
769 | palkovsky | 118 | /** Magazine cache */ |
119 | static slab_cache_t mag_cache; |
||
120 | /** Cache for cache descriptors */ |
||
121 | static slab_cache_t slab_cache_cache; |
||
122 | /** Cache for external slab descriptors |
||
123 | * This time we want per-cpu cache, so do not make it static |
||
1248 | jermar | 124 | * - using slab for internal slab structures will not deadlock, |
769 | palkovsky | 125 | * as all slab structures are 'small' - control structures of |
126 | * their caches do not require further allocation |
||
127 | */ |
||
128 | static slab_cache_t *slab_extern_cache; |
||
771 | palkovsky | 129 | /** Caches for malloc */ |
130 | static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1]; |
||
131 | char *malloc_names[] = { |
||
791 | palkovsky | 132 | "malloc-16","malloc-32","malloc-64","malloc-128", |
771 | palkovsky | 133 | "malloc-256","malloc-512","malloc-1K","malloc-2K", |
134 | "malloc-4K","malloc-8K","malloc-16K","malloc-32K", |
||
1428 | palkovsky | 135 | "malloc-64K","malloc-128K","malloc-256K" |
771 | palkovsky | 136 | }; |
762 | palkovsky | 137 | |
769 | palkovsky | 138 | /** Slab descriptor */ |
762 | palkovsky | 139 | typedef struct { |
140 | slab_cache_t *cache; /**< Pointer to parent cache */ |
||
141 | link_t link; /* List of full/partial slabs */ |
||
142 | void *start; /**< Start address of first available item */ |
||
143 | count_t available; /**< Count of available items in this slab */ |
||
144 | index_t nextavail; /**< The index of next available item */ |
||
145 | }slab_t; |
||
146 | |||
791 | palkovsky | 147 | #ifdef CONFIG_DEBUG |
148 | static int _slab_initialized = 0; |
||
149 | #endif |
||
150 | |||
759 | palkovsky | 151 | /**************************************/ |
1248 | jermar | 152 | /* Slab allocation functions */ |
759 | palkovsky | 153 | |
762 | palkovsky | 154 | /** |
155 | * Allocate frames for slab space and initialize |
||
156 | * |
||
157 | */ |
||
158 | static slab_t * slab_space_alloc(slab_cache_t *cache, int flags) |
||
159 | { |
||
160 | void *data; |
||
161 | slab_t *slab; |
||
162 | size_t fsize; |
||
163 | int i; |
||
164 | int status; |
||
814 | palkovsky | 165 | pfn_t pfn; |
166 | int zone=0; |
||
167 | |||
168 | pfn = frame_alloc_rc_zone(cache->order, FRAME_KA | flags, &status, &zone); |
||
169 | data = (void *) PA2KA(PFN2ADDR(pfn)); |
||
764 | palkovsky | 170 | if (status != FRAME_OK) { |
762 | palkovsky | 171 | return NULL; |
764 | palkovsky | 172 | } |
768 | palkovsky | 173 | if (! (cache->flags & SLAB_CACHE_SLINSIDE)) { |
769 | palkovsky | 174 | slab = slab_alloc(slab_extern_cache, flags); |
762 | palkovsky | 175 | if (!slab) { |
814 | palkovsky | 176 | frame_free(ADDR2PFN(KA2PA(data))); |
762 | palkovsky | 177 | return NULL; |
178 | } |
||
179 | } else { |
||
180 | fsize = (PAGE_SIZE << cache->order); |
||
181 | slab = data + fsize - sizeof(*slab); |
||
182 | } |
||
1288 | jermar | 183 | |
762 | palkovsky | 184 | /* Fill in slab structures */ |
814 | palkovsky | 185 | for (i=0; i < (1 << cache->order); i++) |
186 | frame_set_parent(pfn+i, slab, zone); |
||
762 | palkovsky | 187 | |
188 | slab->start = data; |
||
189 | slab->available = cache->objects; |
||
190 | slab->nextavail = 0; |
||
767 | palkovsky | 191 | slab->cache = cache; |
762 | palkovsky | 192 | |
193 | for (i=0; i<cache->objects;i++) |
||
194 | *((int *) (slab->start + i*cache->size)) = i+1; |
||
764 | palkovsky | 195 | |
196 | atomic_inc(&cache->allocated_slabs); |
||
762 | palkovsky | 197 | return slab; |
198 | } |
||
199 | |||
759 | palkovsky | 200 | /** |
1248 | jermar | 201 | * Deallocate space associated with slab |
762 | palkovsky | 202 | * |
203 | * @return number of freed frames |
||
204 | */ |
||
205 | static count_t slab_space_free(slab_cache_t *cache, slab_t *slab) |
||
206 | { |
||
814 | palkovsky | 207 | frame_free(ADDR2PFN(KA2PA(slab->start))); |
768 | palkovsky | 208 | if (! (cache->flags & SLAB_CACHE_SLINSIDE)) |
769 | palkovsky | 209 | slab_free(slab_extern_cache, slab); |
764 | palkovsky | 210 | |
211 | atomic_dec(&cache->allocated_slabs); |
||
212 | |||
762 | palkovsky | 213 | return 1 << cache->order; |
214 | } |
||
215 | |||
216 | /** Map object to slab structure */ |
||
217 | static slab_t * obj2slab(void *obj) |
||
218 | { |
||
814 | palkovsky | 219 | return (slab_t *)frame_get_parent(ADDR2PFN(KA2PA(obj)), 0); |
762 | palkovsky | 220 | } |
221 | |||
222 | /**************************************/ |
||
1248 | jermar | 223 | /* Slab functions */ |
762 | palkovsky | 224 | |
225 | |||
226 | /** |
||
759 | palkovsky | 227 | * Return object to slab and call a destructor |
228 | * |
||
762 | palkovsky | 229 | * @param slab If the caller knows directly slab of the object, otherwise NULL |
230 | * |
||
759 | palkovsky | 231 | * @return Number of freed pages |
232 | */ |
||
762 | palkovsky | 233 | static count_t slab_obj_destroy(slab_cache_t *cache, void *obj, |
234 | slab_t *slab) |
||
759 | palkovsky | 235 | { |
787 | palkovsky | 236 | int freed = 0; |
237 | |||
762 | palkovsky | 238 | if (!slab) |
239 | slab = obj2slab(obj); |
||
240 | |||
767 | palkovsky | 241 | ASSERT(slab->cache == cache); |
242 | |||
787 | palkovsky | 243 | if (cache->destructor) |
244 | freed = cache->destructor(obj); |
||
245 | |||
776 | palkovsky | 246 | spinlock_lock(&cache->slablock); |
789 | palkovsky | 247 | ASSERT(slab->available < cache->objects); |
776 | palkovsky | 248 | |
762 | palkovsky | 249 | *((int *)obj) = slab->nextavail; |
250 | slab->nextavail = (obj - slab->start)/cache->size; |
||
251 | slab->available++; |
||
252 | |||
253 | /* Move it to correct list */ |
||
254 | if (slab->available == cache->objects) { |
||
255 | /* Free associated memory */ |
||
256 | list_remove(&slab->link); |
||
782 | palkovsky | 257 | spinlock_unlock(&cache->slablock); |
258 | |||
787 | palkovsky | 259 | return freed + slab_space_free(cache, slab); |
782 | palkovsky | 260 | |
780 | palkovsky | 261 | } else if (slab->available == 1) { |
262 | /* It was in full, move to partial */ |
||
263 | list_remove(&slab->link); |
||
264 | list_prepend(&slab->link, &cache->partial_slabs); |
||
762 | palkovsky | 265 | } |
783 | palkovsky | 266 | spinlock_unlock(&cache->slablock); |
787 | palkovsky | 267 | return freed; |
759 | palkovsky | 268 | } |
269 | |||
270 | /** |
||
271 | * Take new object from slab or create new if needed |
||
272 | * |
||
273 | * @return Object address or null |
||
274 | */ |
||
275 | static void * slab_obj_create(slab_cache_t *cache, int flags) |
||
276 | { |
||
762 | palkovsky | 277 | slab_t *slab; |
278 | void *obj; |
||
279 | |||
776 | palkovsky | 280 | spinlock_lock(&cache->slablock); |
281 | |||
762 | palkovsky | 282 | if (list_empty(&cache->partial_slabs)) { |
283 | /* Allow recursion and reclaiming |
||
1248 | jermar | 284 | * - this should work, as the slab control structures |
1288 | jermar | 285 | * are small and do not need to allocate with anything |
286 | * other than frame_alloc when they are allocating, |
||
762 | palkovsky | 287 | * that's why we should get recursion at most 1-level deep |
288 | */ |
||
776 | palkovsky | 289 | spinlock_unlock(&cache->slablock); |
762 | palkovsky | 290 | slab = slab_space_alloc(cache, flags); |
780 | palkovsky | 291 | if (!slab) |
292 | return NULL; |
||
776 | palkovsky | 293 | spinlock_lock(&cache->slablock); |
762 | palkovsky | 294 | } else { |
295 | slab = list_get_instance(cache->partial_slabs.next, |
||
296 | slab_t, |
||
297 | link); |
||
298 | list_remove(&slab->link); |
||
299 | } |
||
300 | obj = slab->start + slab->nextavail * cache->size; |
||
301 | slab->nextavail = *((int *)obj); |
||
302 | slab->available--; |
||
787 | palkovsky | 303 | |
762 | palkovsky | 304 | if (! slab->available) |
764 | palkovsky | 305 | list_prepend(&slab->link, &cache->full_slabs); |
762 | palkovsky | 306 | else |
764 | palkovsky | 307 | list_prepend(&slab->link, &cache->partial_slabs); |
776 | palkovsky | 308 | |
309 | spinlock_unlock(&cache->slablock); |
||
787 | palkovsky | 310 | |
311 | if (cache->constructor && cache->constructor(obj, flags)) { |
||
312 | /* Bad, bad, construction failed */ |
||
313 | slab_obj_destroy(cache, obj, slab); |
||
314 | return NULL; |
||
315 | } |
||
762 | palkovsky | 316 | return obj; |
759 | palkovsky | 317 | } |
318 | |||
319 | /**************************************/ |
||
320 | /* CPU-Cache slab functions */ |
||
321 | |||
322 | /** |
||
781 | palkovsky | 323 | * Finds a full magazine in cache, takes it from list |
324 | * and returns it |
||
325 | * |
||
326 | * @param first If true, return first, else last mag |
||
327 | */ |
||
328 | static slab_magazine_t * get_mag_from_cache(slab_cache_t *cache, |
||
329 | int first) |
||
330 | { |
||
331 | slab_magazine_t *mag = NULL; |
||
332 | link_t *cur; |
||
333 | |||
334 | spinlock_lock(&cache->maglock); |
||
335 | if (!list_empty(&cache->magazines)) { |
||
336 | if (first) |
||
337 | cur = cache->magazines.next; |
||
338 | else |
||
339 | cur = cache->magazines.prev; |
||
340 | mag = list_get_instance(cur, slab_magazine_t, link); |
||
341 | list_remove(&mag->link); |
||
342 | atomic_dec(&cache->magazine_counter); |
||
343 | } |
||
344 | spinlock_unlock(&cache->maglock); |
||
345 | return mag; |
||
346 | } |
||
347 | |||
348 | /** Prepend magazine to magazine list in cache */ |
||
349 | static void put_mag_to_cache(slab_cache_t *cache, slab_magazine_t *mag) |
||
350 | { |
||
351 | spinlock_lock(&cache->maglock); |
||
352 | |||
353 | list_prepend(&mag->link, &cache->magazines); |
||
354 | atomic_inc(&cache->magazine_counter); |
||
355 | |||
356 | spinlock_unlock(&cache->maglock); |
||
357 | } |
||
358 | |||
359 | /** |
||
759 | palkovsky | 360 | * Free all objects in magazine and free memory associated with magazine |
361 | * |
||
362 | * @return Number of freed pages |
||
363 | */ |
||
364 | static count_t magazine_destroy(slab_cache_t *cache, |
||
365 | slab_magazine_t *mag) |
||
366 | { |
||
367 | int i; |
||
368 | count_t frames = 0; |
||
369 | |||
767 | palkovsky | 370 | for (i=0;i < mag->busy; i++) { |
762 | palkovsky | 371 | frames += slab_obj_destroy(cache, mag->objs[i], NULL); |
767 | palkovsky | 372 | atomic_dec(&cache->cached_objs); |
373 | } |
||
759 | palkovsky | 374 | |
375 | slab_free(&mag_cache, mag); |
||
376 | |||
377 | return frames; |
||
378 | } |
||
379 | |||
380 | /** |
||
769 | palkovsky | 381 | * Find full magazine, set it as current and return it |
382 | * |
||
383 | * Assume cpu_magazine lock is held |
||
384 | */ |
||
385 | static slab_magazine_t * get_full_current_mag(slab_cache_t *cache) |
||
386 | { |
||
387 | slab_magazine_t *cmag, *lastmag, *newmag; |
||
388 | |||
389 | cmag = cache->mag_cache[CPU->id].current; |
||
390 | lastmag = cache->mag_cache[CPU->id].last; |
||
391 | if (cmag) { /* First try local CPU magazines */ |
||
392 | if (cmag->busy) |
||
393 | return cmag; |
||
394 | |||
395 | if (lastmag && lastmag->busy) { |
||
396 | cache->mag_cache[CPU->id].current = lastmag; |
||
397 | cache->mag_cache[CPU->id].last = cmag; |
||
398 | return lastmag; |
||
399 | } |
||
400 | } |
||
401 | /* Local magazines are empty, import one from magazine list */ |
||
781 | palkovsky | 402 | newmag = get_mag_from_cache(cache, 1); |
403 | if (!newmag) |
||
769 | palkovsky | 404 | return NULL; |
405 | |||
406 | if (lastmag) |
||
781 | palkovsky | 407 | magazine_destroy(cache, lastmag); |
408 | |||
769 | palkovsky | 409 | cache->mag_cache[CPU->id].last = cmag; |
410 | cache->mag_cache[CPU->id].current = newmag; |
||
411 | return newmag; |
||
412 | } |
||
413 | |||
414 | /** |
||
759 | palkovsky | 415 | * Try to find object in CPU-cache magazines |
416 | * |
||
417 | * @return Pointer to object or NULL if not available |
||
418 | */ |
||
419 | static void * magazine_obj_get(slab_cache_t *cache) |
||
420 | { |
||
421 | slab_magazine_t *mag; |
||
767 | palkovsky | 422 | void *obj; |
759 | palkovsky | 423 | |
772 | palkovsky | 424 | if (!CPU) |
425 | return NULL; |
||
426 | |||
759 | palkovsky | 427 | spinlock_lock(&cache->mag_cache[CPU->id].lock); |
428 | |||
769 | palkovsky | 429 | mag = get_full_current_mag(cache); |
430 | if (!mag) { |
||
431 | spinlock_unlock(&cache->mag_cache[CPU->id].lock); |
||
432 | return NULL; |
||
759 | palkovsky | 433 | } |
767 | palkovsky | 434 | obj = mag->objs[--mag->busy]; |
759 | palkovsky | 435 | spinlock_unlock(&cache->mag_cache[CPU->id].lock); |
767 | palkovsky | 436 | atomic_dec(&cache->cached_objs); |
437 | |||
438 | return obj; |
||
759 | palkovsky | 439 | } |
440 | |||
441 | /** |
||
768 | palkovsky | 442 | * Assure that the current magazine is empty, return pointer to it, or NULL if |
769 | palkovsky | 443 | * no empty magazine is available and cannot be allocated |
759 | palkovsky | 444 | * |
773 | palkovsky | 445 | * Assume mag_cache[CPU->id].lock is held |
446 | * |
||
759 | palkovsky | 447 | * We have 2 magazines bound to processor. |
448 | * First try the current. |
||
449 | * If full, try the last. |
||
450 | * If full, put to magazines list. |
||
451 | * allocate new, exchange last & current |
||
452 | * |
||
768 | palkovsky | 453 | */ |
454 | static slab_magazine_t * make_empty_current_mag(slab_cache_t *cache) |
||
455 | { |
||
456 | slab_magazine_t *cmag,*lastmag,*newmag; |
||
457 | |||
458 | cmag = cache->mag_cache[CPU->id].current; |
||
459 | lastmag = cache->mag_cache[CPU->id].last; |
||
460 | |||
461 | if (cmag) { |
||
462 | if (cmag->busy < cmag->size) |
||
463 | return cmag; |
||
464 | if (lastmag && lastmag->busy < lastmag->size) { |
||
465 | cache->mag_cache[CPU->id].last = cmag; |
||
466 | cache->mag_cache[CPU->id].current = lastmag; |
||
467 | return lastmag; |
||
468 | } |
||
469 | } |
||
470 | /* current | last are full | nonexistent, allocate new */ |
||
471 | /* We do not want to sleep just because of caching */ |
||
472 | /* Especially we do not want reclaiming to start, as |
||
473 | * this would deadlock */ |
||
474 | newmag = slab_alloc(&mag_cache, FRAME_ATOMIC | FRAME_NO_RECLAIM); |
||
475 | if (!newmag) |
||
476 | return NULL; |
||
477 | newmag->size = SLAB_MAG_SIZE; |
||
478 | newmag->busy = 0; |
||
479 | |||
480 | /* Flush last to magazine list */ |
||
781 | palkovsky | 481 | if (lastmag) |
482 | put_mag_to_cache(cache, lastmag); |
||
483 | |||
768 | palkovsky | 484 | /* Move current as last, save new as current */ |
485 | cache->mag_cache[CPU->id].last = cmag; |
||
486 | cache->mag_cache[CPU->id].current = newmag; |
||
487 | |||
488 | return newmag; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Put object into CPU-cache magazine |
||
493 | * |
||
759 | palkovsky | 494 | * @return 0 - success, -1 - could not get memory |
495 | */ |
||
496 | static int magazine_obj_put(slab_cache_t *cache, void *obj) |
||
497 | { |
||
498 | slab_magazine_t *mag; |
||
499 | |||
772 | palkovsky | 500 | if (!CPU) |
501 | return -1; |
||
502 | |||
759 | palkovsky | 503 | spinlock_lock(&cache->mag_cache[CPU->id].lock); |
768 | palkovsky | 504 | |
505 | mag = make_empty_current_mag(cache); |
||
769 | palkovsky | 506 | if (!mag) { |
507 | spinlock_unlock(&cache->mag_cache[CPU->id].lock); |
||
508 | return -1; |
||
509 | } |
||
759 | palkovsky | 510 | |
511 | mag->objs[mag->busy++] = obj; |
||
512 | |||
513 | spinlock_unlock(&cache->mag_cache[CPU->id].lock); |
||
767 | palkovsky | 514 | atomic_inc(&cache->cached_objs); |
759 | palkovsky | 515 | return 0; |
516 | } |
||
517 | |||
518 | |||
519 | /**************************************/ |
||
1248 | jermar | 520 | /* Slab cache functions */ |
759 | palkovsky | 521 | |
762 | palkovsky | 522 | /** Return number of objects that fit in certain cache size */ |
523 | static int comp_objects(slab_cache_t *cache) |
||
524 | { |
||
525 | if (cache->flags & SLAB_CACHE_SLINSIDE) |
||
526 | return ((PAGE_SIZE << cache->order) - sizeof(slab_t)) / cache->size; |
||
527 | else |
||
528 | return (PAGE_SIZE << cache->order) / cache->size; |
||
529 | } |
||
530 | |||
531 | /** Return wasted space in slab */ |
||
532 | static int badness(slab_cache_t *cache) |
||
533 | { |
||
534 | int objects; |
||
535 | int ssize; |
||
536 | |||
537 | objects = comp_objects(cache); |
||
538 | ssize = PAGE_SIZE << cache->order; |
||
539 | if (cache->flags & SLAB_CACHE_SLINSIDE) |
||
540 | ssize -= sizeof(slab_t); |
||
541 | return ssize - objects*cache->size; |
||
542 | } |
||
543 | |||
789 | palkovsky | 544 | /** |
545 | * Initialize mag_cache structure in slab cache |
||
546 | */ |
||
547 | static void make_magcache(slab_cache_t *cache) |
||
548 | { |
||
549 | int i; |
||
791 | palkovsky | 550 | |
551 | ASSERT(_slab_initialized >= 2); |
||
789 | palkovsky | 552 | |
822 | palkovsky | 553 | cache->mag_cache = malloc(sizeof(slab_mag_cache_t)*config.cpu_count,0); |
789 | palkovsky | 554 | for (i=0; i < config.cpu_count; i++) { |
555 | memsetb((__address)&cache->mag_cache[i], |
||
556 | sizeof(cache->mag_cache[i]), 0); |
||
557 | spinlock_initialize(&cache->mag_cache[i].lock, |
||
558 | "slab_maglock_cpu"); |
||
559 | } |
||
560 | } |
||
561 | |||
759 | palkovsky | 562 | /** Initialize allocated memory as a slab cache */ |
563 | static void |
||
564 | _slab_cache_create(slab_cache_t *cache, |
||
565 | char *name, |
||
566 | size_t size, |
||
567 | size_t align, |
||
568 | int (*constructor)(void *obj, int kmflag), |
||
787 | palkovsky | 569 | int (*destructor)(void *obj), |
759 | palkovsky | 570 | int flags) |
571 | { |
||
771 | palkovsky | 572 | int pages; |
783 | palkovsky | 573 | ipl_t ipl; |
759 | palkovsky | 574 | |
575 | memsetb((__address)cache, sizeof(*cache), 0); |
||
576 | cache->name = name; |
||
577 | |||
766 | palkovsky | 578 | if (align < sizeof(__native)) |
579 | align = sizeof(__native); |
||
580 | size = ALIGN_UP(size, align); |
||
581 | |||
762 | palkovsky | 582 | cache->size = size; |
759 | palkovsky | 583 | |
584 | cache->constructor = constructor; |
||
585 | cache->destructor = destructor; |
||
586 | cache->flags = flags; |
||
587 | |||
588 | list_initialize(&cache->full_slabs); |
||
589 | list_initialize(&cache->partial_slabs); |
||
590 | list_initialize(&cache->magazines); |
||
776 | palkovsky | 591 | spinlock_initialize(&cache->slablock, "slab_lock"); |
592 | spinlock_initialize(&cache->maglock, "slab_maglock"); |
||
789 | palkovsky | 593 | if (! (cache->flags & SLAB_CACHE_NOMAGAZINE)) |
594 | make_magcache(cache); |
||
759 | palkovsky | 595 | |
596 | /* Compute slab sizes, object counts in slabs etc. */ |
||
597 | if (cache->size < SLAB_INSIDE_SIZE) |
||
598 | cache->flags |= SLAB_CACHE_SLINSIDE; |
||
599 | |||
762 | palkovsky | 600 | /* Minimum slab order */ |
1682 | palkovsky | 601 | pages = SIZE2FRAMES(cache->size); |
1677 | palkovsky | 602 | /* We need the 2^order >= pages */ |
603 | if (pages == 1) |
||
604 | cache->order = 0; |
||
605 | else |
||
606 | cache->order = fnzb(pages-1)+1; |
||
766 | palkovsky | 607 | |
762 | palkovsky | 608 | while (badness(cache) > SLAB_MAX_BADNESS(cache)) { |
609 | cache->order += 1; |
||
610 | } |
||
611 | cache->objects = comp_objects(cache); |
||
766 | palkovsky | 612 | /* If info fits in, put it inside */ |
613 | if (badness(cache) > sizeof(slab_t)) |
||
614 | cache->flags |= SLAB_CACHE_SLINSIDE; |
||
762 | palkovsky | 615 | |
783 | palkovsky | 616 | /* Add cache to cache list */ |
617 | ipl = interrupts_disable(); |
||
759 | palkovsky | 618 | spinlock_lock(&slab_cache_lock); |
619 | |||
620 | list_append(&cache->link, &slab_cache_list); |
||
621 | |||
622 | spinlock_unlock(&slab_cache_lock); |
||
783 | palkovsky | 623 | interrupts_restore(ipl); |
759 | palkovsky | 624 | } |
625 | |||
626 | /** Create slab cache */ |
||
627 | slab_cache_t * slab_cache_create(char *name, |
||
628 | size_t size, |
||
629 | size_t align, |
||
630 | int (*constructor)(void *obj, int kmflag), |
||
787 | palkovsky | 631 | int (*destructor)(void *obj), |
759 | palkovsky | 632 | int flags) |
633 | { |
||
634 | slab_cache_t *cache; |
||
635 | |||
769 | palkovsky | 636 | cache = slab_alloc(&slab_cache_cache, 0); |
759 | palkovsky | 637 | _slab_cache_create(cache, name, size, align, constructor, destructor, |
638 | flags); |
||
639 | return cache; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Reclaim space occupied by objects that are already free |
||
644 | * |
||
645 | * @param flags If contains SLAB_RECLAIM_ALL, do aggressive freeing |
||
646 | * @return Number of freed pages |
||
647 | */ |
||
648 | static count_t _slab_reclaim(slab_cache_t *cache, int flags) |
||
649 | { |
||
650 | int i; |
||
651 | slab_magazine_t *mag; |
||
652 | count_t frames = 0; |
||
781 | palkovsky | 653 | int magcount; |
759 | palkovsky | 654 | |
655 | if (cache->flags & SLAB_CACHE_NOMAGAZINE) |
||
656 | return 0; /* Nothing to do */ |
||
781 | palkovsky | 657 | |
658 | /* We count up to original magazine count to avoid |
||
659 | * endless loop |
||
660 | */ |
||
661 | magcount = atomic_get(&cache->magazine_counter); |
||
662 | while (magcount-- && (mag=get_mag_from_cache(cache,0))) { |
||
663 | frames += magazine_destroy(cache,mag); |
||
664 | if (!(flags & SLAB_RECLAIM_ALL) && frames) |
||
665 | break; |
||
769 | palkovsky | 666 | } |
759 | palkovsky | 667 | |
668 | if (flags & SLAB_RECLAIM_ALL) { |
||
781 | palkovsky | 669 | /* Free cpu-bound magazines */ |
759 | palkovsky | 670 | /* Destroy CPU magazines */ |
671 | for (i=0; i<config.cpu_count; i++) { |
||
781 | palkovsky | 672 | spinlock_lock(&cache->mag_cache[i].lock); |
673 | |||
759 | palkovsky | 674 | mag = cache->mag_cache[i].current; |
675 | if (mag) |
||
676 | frames += magazine_destroy(cache, mag); |
||
677 | cache->mag_cache[i].current = NULL; |
||
678 | |||
679 | mag = cache->mag_cache[i].last; |
||
680 | if (mag) |
||
681 | frames += magazine_destroy(cache, mag); |
||
682 | cache->mag_cache[i].last = NULL; |
||
781 | palkovsky | 683 | |
684 | spinlock_unlock(&cache->mag_cache[i].lock); |
||
759 | palkovsky | 685 | } |
686 | } |
||
767 | palkovsky | 687 | |
759 | palkovsky | 688 | return frames; |
689 | } |
||
690 | |||
691 | /** Check that there are no slabs and remove cache from system */ |
||
692 | void slab_cache_destroy(slab_cache_t *cache) |
||
693 | { |
||
781 | palkovsky | 694 | ipl_t ipl; |
695 | |||
696 | /* First remove cache from link, so that we don't need |
||
697 | * to disable interrupts later |
||
698 | */ |
||
699 | |||
700 | ipl = interrupts_disable(); |
||
701 | spinlock_lock(&slab_cache_lock); |
||
702 | |||
703 | list_remove(&cache->link); |
||
704 | |||
705 | spinlock_unlock(&slab_cache_lock); |
||
706 | interrupts_restore(ipl); |
||
707 | |||
759 | palkovsky | 708 | /* Do not lock anything, we assume the software is correct and |
709 | * does not touch the cache when it decides to destroy it */ |
||
710 | |||
711 | /* Destroy all magazines */ |
||
712 | _slab_reclaim(cache, SLAB_RECLAIM_ALL); |
||
713 | |||
714 | /* All slabs must be empty */ |
||
715 | if (!list_empty(&cache->full_slabs) \ |
||
716 | || !list_empty(&cache->partial_slabs)) |
||
717 | panic("Destroying cache that is not empty."); |
||
718 | |||
789 | palkovsky | 719 | if (!(cache->flags & SLAB_CACHE_NOMAGAZINE)) |
822 | palkovsky | 720 | free(cache->mag_cache); |
769 | palkovsky | 721 | slab_free(&slab_cache_cache, cache); |
759 | palkovsky | 722 | } |
723 | |||
724 | /** Allocate new object from cache - if no flags given, always returns |
||
725 | memory */ |
||
726 | void * slab_alloc(slab_cache_t *cache, int flags) |
||
727 | { |
||
728 | ipl_t ipl; |
||
729 | void *result = NULL; |
||
773 | palkovsky | 730 | |
759 | palkovsky | 731 | /* Disable interrupts to avoid deadlocks with interrupt handlers */ |
732 | ipl = interrupts_disable(); |
||
771 | palkovsky | 733 | |
814 | palkovsky | 734 | if (!(cache->flags & SLAB_CACHE_NOMAGAZINE)) { |
759 | palkovsky | 735 | result = magazine_obj_get(cache); |
814 | palkovsky | 736 | } |
776 | palkovsky | 737 | if (!result) |
759 | palkovsky | 738 | result = slab_obj_create(cache, flags); |
739 | |||
769 | palkovsky | 740 | interrupts_restore(ipl); |
741 | |||
764 | palkovsky | 742 | if (result) |
743 | atomic_inc(&cache->allocated_objs); |
||
744 | |||
759 | palkovsky | 745 | return result; |
746 | } |
||
747 | |||
771 | palkovsky | 748 | /** Return object to cache, use slab if known */ |
749 | static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab) |
||
759 | palkovsky | 750 | { |
751 | ipl_t ipl; |
||
752 | |||
753 | ipl = interrupts_disable(); |
||
754 | |||
762 | palkovsky | 755 | if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \ |
756 | || magazine_obj_put(cache, obj)) { |
||
776 | palkovsky | 757 | |
771 | palkovsky | 758 | slab_obj_destroy(cache, obj, slab); |
776 | palkovsky | 759 | |
759 | palkovsky | 760 | } |
769 | palkovsky | 761 | interrupts_restore(ipl); |
764 | palkovsky | 762 | atomic_dec(&cache->allocated_objs); |
759 | palkovsky | 763 | } |
764 | |||
771 | palkovsky | 765 | /** Return slab object to cache */ |
766 | void slab_free(slab_cache_t *cache, void *obj) |
||
767 | { |
||
768 | _slab_free(cache,obj,NULL); |
||
769 | } |
||
770 | |||
759 | palkovsky | 771 | /* Go through all caches and reclaim what is possible */ |
772 | count_t slab_reclaim(int flags) |
||
773 | { |
||
774 | slab_cache_t *cache; |
||
775 | link_t *cur; |
||
776 | count_t frames = 0; |
||
777 | |||
778 | spinlock_lock(&slab_cache_lock); |
||
779 | |||
776 | palkovsky | 780 | /* TODO: Add assert, that interrupts are disabled, otherwise |
781 | * memory allocation from interrupts can deadlock. |
||
782 | */ |
||
783 | |||
759 | palkovsky | 784 | for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) { |
785 | cache = list_get_instance(cur, slab_cache_t, link); |
||
786 | frames += _slab_reclaim(cache, flags); |
||
787 | } |
||
788 | |||
789 | spinlock_unlock(&slab_cache_lock); |
||
790 | |||
791 | return frames; |
||
792 | } |
||
793 | |||
794 | |||
795 | /* Print list of slabs */ |
||
796 | void slab_print_list(void) |
||
797 | { |
||
798 | slab_cache_t *cache; |
||
799 | link_t *cur; |
||
783 | palkovsky | 800 | ipl_t ipl; |
801 | |||
802 | ipl = interrupts_disable(); |
||
759 | palkovsky | 803 | spinlock_lock(&slab_cache_lock); |
1248 | jermar | 804 | printf("slab name\t Osize\t Pages\t Obj/pg\t Slabs\t Cached\tAllocobjs\tCtl\n"); |
759 | palkovsky | 805 | for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) { |
806 | cache = list_get_instance(cur, slab_cache_t, link); |
||
1224 | cejka | 807 | printf("%s\t%7zd\t%7zd\t%7zd\t%7zd\t%7zd\t%7zd\t\t%s\n", cache->name, cache->size, |
766 | palkovsky | 808 | (1 << cache->order), cache->objects, |
767 | palkovsky | 809 | atomic_get(&cache->allocated_slabs), |
810 | atomic_get(&cache->cached_objs), |
||
766 | palkovsky | 811 | atomic_get(&cache->allocated_objs), |
812 | cache->flags & SLAB_CACHE_SLINSIDE ? "In" : "Out"); |
||
759 | palkovsky | 813 | } |
814 | spinlock_unlock(&slab_cache_lock); |
||
783 | palkovsky | 815 | interrupts_restore(ipl); |
759 | palkovsky | 816 | } |
817 | |||
818 | void slab_cache_init(void) |
||
819 | { |
||
771 | palkovsky | 820 | int i, size; |
821 | |||
759 | palkovsky | 822 | /* Initialize magazine cache */ |
823 | _slab_cache_create(&mag_cache, |
||
824 | "slab_magazine", |
||
825 | sizeof(slab_magazine_t)+SLAB_MAG_SIZE*sizeof(void*), |
||
826 | sizeof(__address), |
||
827 | NULL, NULL, |
||
769 | palkovsky | 828 | SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE); |
829 | /* Initialize slab_cache cache */ |
||
830 | _slab_cache_create(&slab_cache_cache, |
||
831 | "slab_cache", |
||
789 | palkovsky | 832 | sizeof(slab_cache_cache), |
769 | palkovsky | 833 | sizeof(__address), |
834 | NULL, NULL, |
||
835 | SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE); |
||
836 | /* Initialize external slab cache */ |
||
837 | slab_extern_cache = slab_cache_create("slab_extern", |
||
838 | sizeof(slab_t), |
||
839 | 0, NULL, NULL, |
||
789 | palkovsky | 840 | SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED); |
759 | palkovsky | 841 | |
842 | /* Initialize structures for malloc */ |
||
771 | palkovsky | 843 | for (i=0, size=(1<<SLAB_MIN_MALLOC_W); |
844 | i < (SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1); |
||
845 | i++, size <<= 1) { |
||
846 | malloc_caches[i] = slab_cache_create(malloc_names[i], |
||
847 | size, 0, |
||
789 | palkovsky | 848 | NULL,NULL, SLAB_CACHE_MAGDEFERRED); |
771 | palkovsky | 849 | } |
778 | palkovsky | 850 | #ifdef CONFIG_DEBUG |
851 | _slab_initialized = 1; |
||
852 | #endif |
||
759 | palkovsky | 853 | } |
771 | palkovsky | 854 | |
789 | palkovsky | 855 | /** Enable cpu_cache |
856 | * |
||
857 | * Kernel calls this function, when it knows the real number of |
||
858 | * processors. |
||
859 | * Allocate slab for cpucache and enable it on all existing |
||
860 | * slabs that are SLAB_CACHE_MAGDEFERRED |
||
861 | */ |
||
862 | void slab_enable_cpucache(void) |
||
863 | { |
||
864 | link_t *cur; |
||
865 | slab_cache_t *s; |
||
866 | |||
791 | palkovsky | 867 | #ifdef CONFIG_DEBUG |
868 | _slab_initialized = 2; |
||
869 | #endif |
||
870 | |||
789 | palkovsky | 871 | spinlock_lock(&slab_cache_lock); |
872 | |||
873 | for (cur=slab_cache_list.next; cur != &slab_cache_list;cur=cur->next){ |
||
874 | s = list_get_instance(cur, slab_cache_t, link); |
||
875 | if ((s->flags & SLAB_CACHE_MAGDEFERRED) != SLAB_CACHE_MAGDEFERRED) |
||
876 | continue; |
||
877 | make_magcache(s); |
||
878 | s->flags &= ~SLAB_CACHE_MAGDEFERRED; |
||
879 | } |
||
880 | |||
881 | spinlock_unlock(&slab_cache_lock); |
||
882 | } |
||
883 | |||
771 | palkovsky | 884 | /**************************************/ |
885 | /* kalloc/kfree functions */ |
||
822 | palkovsky | 886 | void * malloc(unsigned int size, int flags) |
771 | palkovsky | 887 | { |
888 | int idx; |
||
778 | palkovsky | 889 | |
890 | ASSERT(_slab_initialized); |
||
1288 | jermar | 891 | ASSERT(size && size <= (1 << SLAB_MAX_MALLOC_W)); |
771 | palkovsky | 892 | |
893 | if (size < (1 << SLAB_MIN_MALLOC_W)) |
||
894 | size = (1 << SLAB_MIN_MALLOC_W); |
||
895 | |||
896 | idx = fnzb(size-1) - SLAB_MIN_MALLOC_W + 1; |
||
897 | |||
898 | return slab_alloc(malloc_caches[idx], flags); |
||
899 | } |
||
900 | |||
822 | palkovsky | 901 | void free(void *obj) |
771 | palkovsky | 902 | { |
781 | palkovsky | 903 | slab_t *slab; |
904 | |||
905 | if (!obj) return; |
||
906 | |||
907 | slab = obj2slab(obj); |
||
771 | palkovsky | 908 | _slab_free(slab->cache, obj, slab); |
909 | } |
||
1702 | cejka | 910 | |
1757 | jermar | 911 | /** @} |
1702 | cejka | 912 | */ |