source: mainline/kernel/generic/src/lib/ra.c@ 81b9d3e

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

Let the resource allocator treat 0 as a valid resource

  • Property mode set to 100644
File size: 10.6 KB
Line 
1/*
2 * Copyright (c) 2011 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup generic
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Resource allocator.
36 *
37 * This is a generic resource allocator, loosely based on the ideas presented
38 * in chapter 4 of the following paper and further simplified:
39 *
40 * Bonwick J., Adams J.: Magazines and Vmem: Extending the Slab Allocator to
41 * Many CPUs and Arbitrary Resources, USENIX 2001
42 *
43 */
44
45#include <assert.h>
46#include <lib/ra.h>
47#include <typedefs.h>
48#include <mm/slab.h>
49#include <bitops.h>
50#include <panic.h>
51#include <adt/list.h>
52#include <adt/hash_table.h>
53#include <align.h>
54#include <macros.h>
55#include <synch/spinlock.h>
56
57static slab_cache_t *ra_segment_cache;
58
59#define USED_BUCKETS 1024
60
61static size_t used_hash(sysarg_t *key)
62{
63 return ((*key >> 2) & (USED_BUCKETS - 1));
64}
65
66static bool used_compare(sysarg_t *key, size_t keys, link_t *item)
67{
68 ra_segment_t *seg;
69
70 seg = hash_table_get_instance(item, ra_segment_t, fu_link);
71 return seg->base == *key;
72}
73
74static hash_table_operations_t used_ops = {
75 .hash = used_hash,
76 .compare = used_compare,
77 .remove_callback = NULL,
78};
79
80/** Calculate the segment size. */
81static size_t ra_segment_size_get(ra_segment_t *seg)
82{
83 ra_segment_t *nextseg;
84
85 nextseg = list_get_instance(seg->segment_link.next, ra_segment_t,
86 segment_link);
87 return nextseg->base - seg->base;
88}
89
90static ra_segment_t *ra_segment_create(uintptr_t base)
91{
92 ra_segment_t *seg;
93
94 seg = slab_alloc(ra_segment_cache, FRAME_ATOMIC);
95 if (!seg)
96 return NULL;
97
98 link_initialize(&seg->segment_link);
99 link_initialize(&seg->fu_link);
100
101 seg->base = base;
102 seg->flags = 0;
103
104 return seg;
105}
106
107static void ra_segment_destroy(ra_segment_t *seg)
108{
109 slab_free(ra_segment_cache, seg);
110}
111
112static ra_span_t *ra_span_create(uintptr_t base, size_t size)
113{
114 ra_span_t *span;
115 ra_segment_t *seg, *lastseg;
116 unsigned int i;
117
118 span = (ra_span_t *) malloc(sizeof(ra_span_t), FRAME_ATOMIC);
119 if (!span)
120 return NULL;
121
122 span->max_order = fnzb(size);
123 span->base = base;
124 span->size = size;
125
126 span->free = (list_t *) malloc((span->max_order + 1) * sizeof(list_t),
127 FRAME_ATOMIC);
128 if (!span->free) {
129 free(span);
130 return NULL;
131 }
132
133 /*
134 * Create a segment to represent the entire size of the span.
135 */
136 seg = ra_segment_create(base);
137 if (!seg) {
138 free(span->free);
139 free(span);
140 return NULL;
141 }
142 seg->flags = RA_SEGMENT_FREE;
143
144 /*
145 * The last segment will be used as a sentinel at the end of the
146 * segment list so that it is possible to calculate the size for
147 * all other segments. It will not be placed in any free list or
148 * in the used segment hash and adjacent segments will not be
149 * coalesced with it.
150 */
151 lastseg = ra_segment_create(base + size);
152 if (!lastseg) {
153 ra_segment_destroy(seg);
154 free(span->free);
155 free(span);
156 return NULL;
157 }
158
159 link_initialize(&span->span_link);
160 list_initialize(&span->segments);
161
162 hash_table_create(&span->used, USED_BUCKETS, 1, &used_ops);
163
164 for (i = 0; i <= span->max_order; i++)
165 list_initialize(&span->free[i]);
166
167 /* Insert the first segment into the list of segments. */
168 list_append(&seg->segment_link, &span->segments);
169 /* Insert the last segment into the list of segments. */
170 list_append(&lastseg->segment_link, &span->segments);
171
172 /* Insert the first segment into the respective free list. */
173 list_append(&seg->fu_link, &span->free[span->max_order]);
174
175 return span;
176}
177
178/** Create an empty arena. */
179ra_arena_t *ra_arena_create(void)
180{
181 ra_arena_t *arena;
182
183 arena = (ra_arena_t *) malloc(sizeof(ra_arena_t), FRAME_ATOMIC);
184 if (!arena)
185 return NULL;
186
187 irq_spinlock_initialize(&arena->lock, "arena_lock");
188 list_initialize(&arena->spans);
189
190 return arena;
191}
192
193/** Add a span to arena. */
194bool ra_span_add(ra_arena_t *arena, uintptr_t base, size_t size)
195{
196 ra_span_t *span;
197
198 span = ra_span_create(base, size);
199 if (!span)
200 return false;
201
202 /* TODO: check for overlaps */
203 irq_spinlock_lock(&arena->lock, true);
204 list_append(&span->span_link, &arena->spans);
205 irq_spinlock_unlock(&arena->lock, true);
206 return true;
207}
208
209static bool
210ra_span_alloc(ra_span_t *span, size_t size, size_t align, uintptr_t *base)
211{
212 /*
213 * We need to add the maximum of align - 1 to be able to compensate for
214 * the worst case unaligned segment.
215 */
216 size_t needed = size + align - 1;
217 size_t order = ispwr2(needed) ? fnzb(needed) : fnzb(needed) + 1;
218 ra_segment_t *pred = NULL;
219 ra_segment_t *succ = NULL;
220
221 /*
222 * Find the free list of the smallest order which can satisfy this
223 * request.
224 */
225 for (; order <= span->max_order; order++) {
226 ra_segment_t *seg;
227 uintptr_t newbase;
228
229 if (list_empty(&span->free[order]))
230 continue;
231
232 /* Take the first segment from the free list. */
233 seg = list_get_instance(list_first(&span->free[order]),
234 ra_segment_t, fu_link);
235
236 assert(seg->flags & RA_SEGMENT_FREE);
237
238 /*
239 * See if we need to allocate new segments for the chopped-off
240 * parts of this segment.
241 */
242 if (!IS_ALIGNED(seg->base, align)) {
243 pred = ra_segment_create(seg->base);
244 if (!pred) {
245 /*
246 * Fail as we are unable to split the segment.
247 */
248 break;
249 }
250 pred->flags |= RA_SEGMENT_FREE;
251 }
252 newbase = ALIGN_UP(seg->base, align);
253 if (newbase + size != seg->base + ra_segment_size_get(seg)) {
254 assert(newbase + (size - 1) < seg->base +
255 (ra_segment_size_get(seg) - 1));
256 succ = ra_segment_create(newbase + size);
257 if (!succ) {
258 if (pred)
259 ra_segment_destroy(pred);
260 /*
261 * Fail as we are unable to split the segment.
262 */
263 break;
264 }
265 succ->flags |= RA_SEGMENT_FREE;
266 }
267
268
269 /* Put unneeded parts back. */
270 if (pred) {
271 size_t pred_order;
272
273 list_insert_before(&pred->segment_link,
274 &seg->segment_link);
275 pred_order = fnzb(ra_segment_size_get(pred));
276 list_append(&pred->fu_link, &span->free[pred_order]);
277 }
278 if (succ) {
279 size_t succ_order;
280
281 list_insert_after(&succ->segment_link,
282 &seg->segment_link);
283 succ_order = fnzb(ra_segment_size_get(succ));
284 list_append(&succ->fu_link, &span->free[succ_order]);
285 }
286
287 /* Now remove the found segment from the free list. */
288 list_remove(&seg->fu_link);
289 seg->base = newbase;
290 seg->flags &= ~RA_SEGMENT_FREE;
291
292 /* Hash-in the segment into the used hash. */
293 sysarg_t key = seg->base;
294 hash_table_insert(&span->used, &key, &seg->fu_link);
295
296 *base = newbase;
297 return true;
298 }
299
300 return false;
301}
302
303static void ra_span_free(ra_span_t *span, size_t base, size_t size)
304{
305 sysarg_t key = base;
306 link_t *link;
307 ra_segment_t *seg;
308 ra_segment_t *pred;
309 ra_segment_t *succ;
310 size_t order;
311
312 /*
313 * Locate the segment in the used hash table.
314 */
315 link = hash_table_find(&span->used, &key);
316 if (!link) {
317 panic("Freeing segment which is not known to be used (base=%"
318 PRIxn ", size=%" PRIdn ").", base, size);
319 }
320 seg = hash_table_get_instance(link, ra_segment_t, fu_link);
321
322 /*
323 * Hash out the segment.
324 */
325 hash_table_remove(&span->used, &key, 1);
326
327 assert(!(seg->flags & RA_SEGMENT_FREE));
328 assert(seg->base == base);
329 assert(ra_segment_size_get(seg) == size);
330
331 /*
332 * Check whether the segment can be coalesced with its left neighbor.
333 */
334 if (list_first(&span->segments) != &seg->segment_link) {
335 pred = hash_table_get_instance(seg->segment_link.prev,
336 ra_segment_t, segment_link);
337
338 assert(pred->base < seg->base);
339
340 if (pred->flags & RA_SEGMENT_FREE) {
341 /*
342 * The segment can be coalesced with its predecessor.
343 * Remove the predecessor from the free and segment
344 * lists, rebase the segment and throw the predecessor
345 * away.
346 */
347 list_remove(&pred->fu_link);
348 list_remove(&pred->segment_link);
349 seg->base = pred->base;
350 ra_segment_destroy(pred);
351 }
352 }
353
354 /*
355 * Check whether the segment can be coalesced with its right neighbor.
356 */
357 succ = hash_table_get_instance(seg->segment_link.next, ra_segment_t,
358 segment_link);
359 assert(succ->base > seg->base);
360 if (succ->flags & RA_SEGMENT_FREE) {
361 /*
362 * The segment can be coalesced with its successor.
363 * Remove the successor from the free and segment lists
364 * and throw it away.
365 */
366 list_remove(&succ->fu_link);
367 list_remove(&succ->segment_link);
368 ra_segment_destroy(succ);
369 }
370
371 /* Put the segment on the appropriate free list. */
372 seg->flags |= RA_SEGMENT_FREE;
373 order = fnzb(ra_segment_size_get(seg));
374 list_append(&seg->fu_link, &span->free[order]);
375}
376
377/** Allocate resources from arena. */
378bool
379ra_alloc(ra_arena_t *arena, size_t size, size_t alignment, uintptr_t *base)
380{
381 bool success = false;
382
383 assert(size >= 1);
384 assert(alignment >= 1);
385 assert(ispwr2(alignment));
386
387 irq_spinlock_lock(&arena->lock, true);
388 list_foreach(arena->spans, span_link, ra_span_t, span) {
389 success = ra_span_alloc(span, size, alignment, base);
390 if (success)
391 break;
392 }
393 irq_spinlock_unlock(&arena->lock, true);
394
395 return success;
396}
397
398/* Return resources to arena. */
399void ra_free(ra_arena_t *arena, uintptr_t base, size_t size)
400{
401 irq_spinlock_lock(&arena->lock, true);
402 list_foreach(arena->spans, span_link, ra_span_t, span) {
403 if (iswithin(span->base, span->size, base, size)) {
404 ra_span_free(span, base, size);
405 irq_spinlock_unlock(&arena->lock, true);
406 return;
407 }
408 }
409 irq_spinlock_unlock(&arena->lock, true);
410
411 panic("Freeing to wrong arena (base=%" PRIxn ", size=%" PRIdn ").",
412 base, size);
413}
414
415void ra_init(void)
416{
417 ra_segment_cache = slab_cache_create("ra_segment_t",
418 sizeof(ra_segment_t), 0, NULL, NULL, SLAB_CACHE_MAGDEFERRED);
419}
420
421/** @}
422 */
Note: See TracBrowser for help on using the repository browser.