2 * A Remote Heap. Remote means that we don't touch the memory that the
3 * heap points to. Normal heap implementations use the memory they manage
4 * to place their list. We cannot do that because the memory we manage may
5 * have special properties, for example it is uncachable or of different
8 * Author: Pantelis Antoniou <panto@intracom.gr>
10 * 2004 (c) INTRACOM S.A. Greece. This file is licensed under
11 * the terms of the GNU General Public License version 2. This program
12 * is licensed "as is" without any warranty of any kind, whether express
15 #include <linux/types.h>
16 #include <linux/errno.h>
18 #include <linux/slab.h>
20 #include <asm/rheap.h>
23 * Fixup a list_head, needed when copying lists. If the pointers fall
24 * between s and e, apply the delta. This assumes that
25 * sizeof(struct list_head *) == sizeof(unsigned long *).
27 static inline void fixup(unsigned long s, unsigned long e, int d,
32 pp = (unsigned long *)&l->next;
33 if (*pp >= s && *pp < e)
36 pp = (unsigned long *)&l->prev;
37 if (*pp >= s && *pp < e)
41 /* Grow the allocated blocks */
42 static int grow(rh_info_t * info, int max_blocks)
44 rh_block_t *block, *blk;
47 unsigned long blks, blke;
49 if (max_blocks <= info->max_blocks)
52 new_blocks = max_blocks - info->max_blocks;
54 block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
58 if (info->max_blocks > 0) {
60 /* copy old block area */
61 memcpy(block, info->block,
62 sizeof(rh_block_t) * info->max_blocks);
64 delta = (char *)block - (char *)info->block;
66 /* and fixup list pointers */
67 blks = (unsigned long)info->block;
68 blke = (unsigned long)(info->block + info->max_blocks);
70 for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
71 fixup(blks, blke, delta, &blk->list);
73 fixup(blks, blke, delta, &info->empty_list);
74 fixup(blks, blke, delta, &info->free_list);
75 fixup(blks, blke, delta, &info->taken_list);
77 /* free the old allocated memory */
78 if ((info->flags & RHIF_STATIC_BLOCK) == 0)
83 info->empty_slots += new_blocks;
84 info->max_blocks = max_blocks;
85 info->flags &= ~RHIF_STATIC_BLOCK;
87 /* add all new blocks to the free list */
88 for (i = 0, blk = block + info->max_blocks; i < new_blocks; i++, blk++)
89 list_add(&blk->list, &info->empty_list);
95 * Assure at least the required amount of empty slots. If this function
96 * causes a grow in the block area then all pointers kept to the block
99 static int assure_empty(rh_info_t * info, int slots)
103 /* This function is not meant to be used to grow uncontrollably */
108 if (info->empty_slots >= slots)
111 /* Next 16 sized block */
112 max_blocks = ((info->max_blocks + slots) + 15) & ~15;
114 return grow(info, max_blocks);
117 static rh_block_t *get_slot(rh_info_t * info)
121 /* If no more free slots, and failure to extend. */
122 /* XXX: You should have called assure_empty before */
123 if (info->empty_slots == 0) {
124 printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
128 /* Get empty slot to use */
129 blk = list_entry(info->empty_list.next, rh_block_t, list);
130 list_del_init(&blk->list);
141 static inline void release_slot(rh_info_t * info, rh_block_t * blk)
143 list_add(&blk->list, &info->empty_list);
147 static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
154 unsigned long s, e, bs, be;
157 /* We assume that they are aligned properly */
159 s = (unsigned long)blkn->start;
162 /* Find the blocks immediately before and after the given one
168 list_for_each(l, &info->free_list) {
169 blk = list_entry(l, rh_block_t, list);
171 bs = (unsigned long)blk->start;
174 if (next == NULL && s >= bs)
183 /* If both are not null, break now */
184 if (before != NULL && after != NULL)
188 /* Now check if they are really adjacent */
189 if (before != NULL && s != (unsigned long)before->start + before->size)
192 if (after != NULL && e != (unsigned long)after->start)
195 /* No coalescing; list insert and return */
196 if (before == NULL && after == NULL) {
199 list_add(&blkn->list, &next->list);
201 list_add(&blkn->list, &info->free_list);
206 /* We don't need it anymore */
207 release_slot(info, blkn);
209 /* Grow the before block */
210 if (before != NULL && after == NULL) {
211 before->size += size;
215 /* Grow the after block backwards */
216 if (before == NULL && after != NULL) {
217 after->start = (int8_t *)after->start - size;
222 /* Grow the before block, and release the after block */
223 before->size += size + after->size;
224 list_del(&after->list);
225 release_slot(info, after);
228 static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
233 /* Find the block immediately before the given one (if any) */
234 list_for_each(l, &info->taken_list) {
235 blk = list_entry(l, rh_block_t, list);
236 if (blk->start > blkn->start) {
237 list_add_tail(&blkn->list, &blk->list);
242 list_add_tail(&blkn->list, &info->taken_list);
246 * Create a remote heap dynamically. Note that no memory for the blocks
247 * are allocated. It will upon the first allocation
249 rh_info_t *rh_create(unsigned int alignment)
253 /* Alignment must be a power of two */
254 if ((alignment & (alignment - 1)) != 0)
255 return ERR_PTR(-EINVAL);
257 info = kmalloc(sizeof(*info), GFP_KERNEL);
259 return ERR_PTR(-ENOMEM);
261 info->alignment = alignment;
263 /* Initially everything as empty */
265 info->max_blocks = 0;
266 info->empty_slots = 0;
269 INIT_LIST_HEAD(&info->empty_list);
270 INIT_LIST_HEAD(&info->free_list);
271 INIT_LIST_HEAD(&info->taken_list);
277 * Destroy a dynamically created remote heap. Deallocate only if the areas
280 void rh_destroy(rh_info_t * info)
282 if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
285 if ((info->flags & RHIF_STATIC_INFO) == 0)
290 * Initialize in place a remote heap info block. This is needed to support
291 * operation very early in the startup of the kernel, when it is not yet safe
294 void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
300 /* Alignment must be a power of two */
301 if ((alignment & (alignment - 1)) != 0)
304 info->alignment = alignment;
306 /* Initially everything as empty */
308 info->max_blocks = max_blocks;
309 info->empty_slots = max_blocks;
310 info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
312 INIT_LIST_HEAD(&info->empty_list);
313 INIT_LIST_HEAD(&info->free_list);
314 INIT_LIST_HEAD(&info->taken_list);
316 /* Add all new blocks to the free list */
317 for (i = 0, blk = block; i < max_blocks; i++, blk++)
318 list_add(&blk->list, &info->empty_list);
321 /* Attach a free memory region, coalesces regions if adjuscent */
322 int rh_attach_region(rh_info_t * info, void *start, int size)
325 unsigned long s, e, m;
328 /* The region must be aligned */
329 s = (unsigned long)start;
331 m = info->alignment - 1;
339 /* Take final values */
343 /* Grow the blocks, if needed */
344 r = assure_empty(info, 1);
348 blk = get_slot(info);
353 attach_free_block(info, blk);
358 /* Detatch given address range, splits free block if needed. */
359 void *rh_detach_region(rh_info_t * info, void *start, int size)
362 rh_block_t *blk, *newblk;
363 unsigned long s, e, m, bs, be;
367 return ERR_PTR(-EINVAL);
369 /* The region must be aligned */
370 s = (unsigned long)start;
372 m = info->alignment - 1;
380 if (assure_empty(info, 1) < 0)
381 return ERR_PTR(-ENOMEM);
384 list_for_each(l, &info->free_list) {
385 blk = list_entry(l, rh_block_t, list);
386 /* The range must lie entirely inside one free block */
387 bs = (unsigned long)blk->start;
388 be = (unsigned long)blk->start + blk->size;
389 if (s >= bs && e <= be)
395 return ERR_PTR(-ENOMEM);
398 if (bs == s && be == e) {
399 /* Delete from free list, release slot */
400 list_del(&blk->list);
401 release_slot(info, blk);
405 /* blk still in free list, with updated start and/or size */
406 if (bs == s || be == e) {
408 blk->start = (int8_t *)blk->start + size;
412 /* The front free fragment */
415 /* the back free fragment */
416 newblk = get_slot(info);
417 newblk->start = (void *)e;
418 newblk->size = be - e;
420 list_add(&newblk->list, &blk->list);
426 void *rh_alloc(rh_info_t * info, int size, const char *owner)
435 return ERR_PTR(-EINVAL);
437 /* Align to configured alignment */
438 size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
440 if (assure_empty(info, 1) < 0)
441 return ERR_PTR(-ENOMEM);
444 list_for_each(l, &info->free_list) {
445 blk = list_entry(l, rh_block_t, list);
446 if (size <= blk->size)
452 return ERR_PTR(-ENOMEM);
455 if (blk->size == size) {
456 /* Move from free list to taken list */
457 list_del(&blk->list);
461 attach_taken_block(info, blk);
466 newblk = get_slot(info);
467 newblk->start = blk->start;
469 newblk->owner = owner;
471 /* blk still in free list, with updated start, size */
472 blk->start = (int8_t *)blk->start + size;
475 start = newblk->start;
477 attach_taken_block(info, newblk);
482 /* allocate at precisely the given address */
483 void *rh_alloc_fixed(rh_info_t * info, void *start, int size, const char *owner)
486 rh_block_t *blk, *newblk1, *newblk2;
487 unsigned long s, e, m, bs, be;
491 return ERR_PTR(-EINVAL);
493 /* The region must be aligned */
494 s = (unsigned long)start;
496 m = info->alignment - 1;
504 if (assure_empty(info, 2) < 0)
505 return ERR_PTR(-ENOMEM);
508 list_for_each(l, &info->free_list) {
509 blk = list_entry(l, rh_block_t, list);
510 /* The range must lie entirely inside one free block */
511 bs = (unsigned long)blk->start;
512 be = (unsigned long)blk->start + blk->size;
513 if (s >= bs && e <= be)
518 return ERR_PTR(-ENOMEM);
521 if (bs == s && be == e) {
522 /* Move from free list to taken list */
523 list_del(&blk->list);
527 attach_taken_block(info, blk);
533 /* blk still in free list, with updated start and/or size */
534 if (bs == s || be == e) {
536 blk->start = (int8_t *)blk->start + size;
540 /* The front free fragment */
543 /* The back free fragment */
544 newblk2 = get_slot(info);
545 newblk2->start = (void *)e;
546 newblk2->size = be - e;
548 list_add(&newblk2->list, &blk->list);
551 newblk1 = get_slot(info);
552 newblk1->start = (void *)s;
553 newblk1->size = e - s;
554 newblk1->owner = owner;
556 start = newblk1->start;
557 attach_taken_block(info, newblk1);
562 int rh_free(rh_info_t * info, void *start)
564 rh_block_t *blk, *blk2;
568 /* Linear search for block */
570 list_for_each(l, &info->taken_list) {
571 blk2 = list_entry(l, rh_block_t, list);
572 if (start < blk2->start)
577 if (blk == NULL || start > (blk->start + blk->size))
580 /* Remove from taken list */
581 list_del(&blk->list);
583 /* Get size of freed block */
585 attach_free_block(info, blk);
590 int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
600 h = &info->free_list;
604 h = &info->taken_list;
611 /* Linear search for block */
613 list_for_each(l, h) {
614 blk = list_entry(l, rh_block_t, list);
615 if (stats != NULL && nr < max_stats) {
616 stats->start = blk->start;
617 stats->size = blk->size;
618 stats->owner = blk->owner;
627 int rh_set_owner(rh_info_t * info, void *start, const char *owner)
629 rh_block_t *blk, *blk2;
633 /* Linear search for block */
635 list_for_each(l, &info->taken_list) {
636 blk2 = list_entry(l, rh_block_t, list);
637 if (start < blk2->start)
642 if (blk == NULL || start > (blk->start + blk->size))
651 void rh_dump(rh_info_t * info)
653 static rh_stats_t st[32]; /* XXX maximum 32 blocks */
657 maxnr = sizeof(st) / sizeof(st[0]);
660 "info @0x%p (%d slots empty / %d max)\n",
661 info, info->empty_slots, info->max_blocks);
663 printk(KERN_INFO " Free:\n");
664 nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
667 for (i = 0; i < nr; i++)
670 st[i].start, (int8_t *) st[i].start + st[i].size,
672 printk(KERN_INFO "\n");
674 printk(KERN_INFO " Taken:\n");
675 nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
678 for (i = 0; i < nr; i++)
680 " 0x%p-0x%p (%u) %s\n",
681 st[i].start, (int8_t *) st[i].start + st[i].size,
682 st[i].size, st[i].owner != NULL ? st[i].owner : "");
683 printk(KERN_INFO "\n");
686 void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
689 "blk @0x%p: 0x%p-0x%p (%u)\n",
690 blk, blk->start, (int8_t *) blk->start + blk->size, blk->size);