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>
17 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 #include <asm/rheap.h>
24 * Fixup a list_head, needed when copying lists. If the pointers fall
25 * between s and e, apply the delta. This assumes that
26 * sizeof(struct list_head *) == sizeof(unsigned long *).
28 static inline void fixup(unsigned long s, unsigned long e, int d,
33 pp = (unsigned long *)&l->next;
34 if (*pp >= s && *pp < e)
37 pp = (unsigned long *)&l->prev;
38 if (*pp >= s && *pp < e)
42 /* Grow the allocated blocks */
43 static int grow(rh_info_t * info, int max_blocks)
45 rh_block_t *block, *blk;
48 unsigned long blks, blke;
50 if (max_blocks <= info->max_blocks)
53 new_blocks = max_blocks - info->max_blocks;
55 block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
59 if (info->max_blocks > 0) {
61 /* copy old block area */
62 memcpy(block, info->block,
63 sizeof(rh_block_t) * info->max_blocks);
65 delta = (char *)block - (char *)info->block;
67 /* and fixup list pointers */
68 blks = (unsigned long)info->block;
69 blke = (unsigned long)(info->block + info->max_blocks);
71 for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
72 fixup(blks, blke, delta, &blk->list);
74 fixup(blks, blke, delta, &info->empty_list);
75 fixup(blks, blke, delta, &info->free_list);
76 fixup(blks, blke, delta, &info->taken_list);
78 /* free the old allocated memory */
79 if ((info->flags & RHIF_STATIC_BLOCK) == 0)
84 info->empty_slots += new_blocks;
85 info->max_blocks = max_blocks;
86 info->flags &= ~RHIF_STATIC_BLOCK;
88 /* add all new blocks to the free list */
89 blk = block + info->max_blocks - new_blocks;
90 for (i = 0; i < new_blocks; i++, blk++)
91 list_add(&blk->list, &info->empty_list);
97 * Assure at least the required amount of empty slots. If this function
98 * causes a grow in the block area then all pointers kept to the block
101 static int assure_empty(rh_info_t * info, int slots)
105 /* This function is not meant to be used to grow uncontrollably */
110 if (info->empty_slots >= slots)
113 /* Next 16 sized block */
114 max_blocks = ((info->max_blocks + slots) + 15) & ~15;
116 return grow(info, max_blocks);
119 static rh_block_t *get_slot(rh_info_t * info)
123 /* If no more free slots, and failure to extend. */
124 /* XXX: You should have called assure_empty before */
125 if (info->empty_slots == 0) {
126 printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
130 /* Get empty slot to use */
131 blk = list_entry(info->empty_list.next, rh_block_t, list);
132 list_del_init(&blk->list);
143 static inline void release_slot(rh_info_t * info, rh_block_t * blk)
145 list_add(&blk->list, &info->empty_list);
149 static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
156 unsigned long s, e, bs, be;
159 /* We assume that they are aligned properly */
164 /* Find the blocks immediately before and after the given one
170 list_for_each(l, &info->free_list) {
171 blk = list_entry(l, rh_block_t, list);
176 if (next == NULL && s >= bs)
185 /* If both are not null, break now */
186 if (before != NULL && after != NULL)
190 /* Now check if they are really adjacent */
191 if (before && s != (before->start + before->size))
194 if (after && e != after->start)
197 /* No coalescing; list insert and return */
198 if (before == NULL && after == NULL) {
201 list_add(&blkn->list, &next->list);
203 list_add(&blkn->list, &info->free_list);
208 /* We don't need it anymore */
209 release_slot(info, blkn);
211 /* Grow the before block */
212 if (before != NULL && after == NULL) {
213 before->size += size;
217 /* Grow the after block backwards */
218 if (before == NULL && after != NULL) {
219 after->start -= size;
224 /* Grow the before block, and release the after block */
225 before->size += size + after->size;
226 list_del(&after->list);
227 release_slot(info, after);
230 static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
235 /* Find the block immediately before the given one (if any) */
236 list_for_each(l, &info->taken_list) {
237 blk = list_entry(l, rh_block_t, list);
238 if (blk->start > blkn->start) {
239 list_add_tail(&blkn->list, &blk->list);
244 list_add_tail(&blkn->list, &info->taken_list);
248 * Create a remote heap dynamically. Note that no memory for the blocks
249 * are allocated. It will upon the first allocation
251 rh_info_t *rh_create(unsigned int alignment)
255 /* Alignment must be a power of two */
256 if ((alignment & (alignment - 1)) != 0)
257 return ERR_PTR(-EINVAL);
259 info = kmalloc(sizeof(*info), GFP_KERNEL);
261 return ERR_PTR(-ENOMEM);
263 info->alignment = alignment;
265 /* Initially everything as empty */
267 info->max_blocks = 0;
268 info->empty_slots = 0;
271 INIT_LIST_HEAD(&info->empty_list);
272 INIT_LIST_HEAD(&info->free_list);
273 INIT_LIST_HEAD(&info->taken_list);
279 * Destroy a dynamically created remote heap. Deallocate only if the areas
282 void rh_destroy(rh_info_t * info)
284 if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
287 if ((info->flags & RHIF_STATIC_INFO) == 0)
292 * Initialize in place a remote heap info block. This is needed to support
293 * operation very early in the startup of the kernel, when it is not yet safe
296 void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
302 /* Alignment must be a power of two */
303 if ((alignment & (alignment - 1)) != 0)
306 info->alignment = alignment;
308 /* Initially everything as empty */
310 info->max_blocks = max_blocks;
311 info->empty_slots = max_blocks;
312 info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
314 INIT_LIST_HEAD(&info->empty_list);
315 INIT_LIST_HEAD(&info->free_list);
316 INIT_LIST_HEAD(&info->taken_list);
318 /* Add all new blocks to the free list */
319 for (i = 0, blk = block; i < max_blocks; i++, blk++)
320 list_add(&blk->list, &info->empty_list);
323 /* Attach a free memory region, coalesces regions if adjuscent */
324 int rh_attach_region(rh_info_t * info, unsigned long start, int size)
327 unsigned long s, e, m;
330 /* The region must be aligned */
333 m = info->alignment - 1;
341 if (IS_ERR_VALUE(e) || (e < s))
344 /* Take final values */
348 /* Grow the blocks, if needed */
349 r = assure_empty(info, 1);
353 blk = get_slot(info);
358 attach_free_block(info, blk);
363 /* Detatch given address range, splits free block if needed. */
364 unsigned long rh_detach_region(rh_info_t * info, unsigned long start, int size)
367 rh_block_t *blk, *newblk;
368 unsigned long s, e, m, bs, be;
372 return (unsigned long) -EINVAL;
374 /* The region must be aligned */
377 m = info->alignment - 1;
385 if (assure_empty(info, 1) < 0)
386 return (unsigned long) -ENOMEM;
389 list_for_each(l, &info->free_list) {
390 blk = list_entry(l, rh_block_t, list);
391 /* The range must lie entirely inside one free block */
393 be = blk->start + blk->size;
394 if (s >= bs && e <= be)
400 return (unsigned long) -ENOMEM;
403 if (bs == s && be == e) {
404 /* Delete from free list, release slot */
405 list_del(&blk->list);
406 release_slot(info, blk);
410 /* blk still in free list, with updated start and/or size */
411 if (bs == s || be == e) {
417 /* The front free fragment */
420 /* the back free fragment */
421 newblk = get_slot(info);
423 newblk->size = be - e;
425 list_add(&newblk->list, &blk->list);
431 /* Allocate a block of memory at the specified alignment. The value returned
432 * is an offset into the buffer initialized by rh_init(), or a negative number
433 * if there is an error.
435 unsigned long rh_alloc_align(rh_info_t * info, int size, int alignment, const char *owner)
442 /* Validate size, and alignment must be power of two */
443 if (size <= 0 || (alignment & (alignment - 1)) != 0)
444 return (unsigned long) -EINVAL;
446 /* given alignment larger that default rheap alignment */
447 if (alignment > info->alignment)
448 size += alignment - 1;
450 /* Align to configured alignment */
451 size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
453 if (assure_empty(info, 1) < 0)
454 return (unsigned long) -ENOMEM;
457 list_for_each(l, &info->free_list) {
458 blk = list_entry(l, rh_block_t, list);
459 if (size <= blk->size)
465 return (unsigned long) -ENOMEM;
468 if (blk->size == size) {
469 /* Move from free list to taken list */
470 list_del(&blk->list);
474 attach_taken_block(info, blk);
479 newblk = get_slot(info);
480 newblk->start = blk->start;
482 newblk->owner = owner;
484 /* blk still in free list, with updated start, size */
488 start = newblk->start;
490 attach_taken_block(info, newblk);
492 /* for larger alignment return fixed up pointer */
493 /* this is no problem with the deallocator since */
494 /* we scan for pointers that lie in the blocks */
495 if (alignment > info->alignment)
496 start = (start + alignment - 1) & ~(alignment - 1);
501 /* Allocate a block of memory at the default alignment. The value returned is
502 * an offset into the buffer initialized by rh_init(), or a negative number if
505 unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
507 return rh_alloc_align(info, size, info->alignment, owner);
510 /* Allocate a block of memory at the given offset, rounded up to the default
511 * alignment. The value returned is an offset into the buffer initialized by
512 * rh_init(), or a negative number if there is an error.
514 unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
517 rh_block_t *blk, *newblk1, *newblk2;
518 unsigned long s, e, m, bs = 0, be = 0;
522 return (unsigned long) -EINVAL;
524 /* The region must be aligned */
527 m = info->alignment - 1;
535 if (assure_empty(info, 2) < 0)
536 return (unsigned long) -ENOMEM;
539 list_for_each(l, &info->free_list) {
540 blk = list_entry(l, rh_block_t, list);
541 /* The range must lie entirely inside one free block */
543 be = blk->start + blk->size;
544 if (s >= bs && e <= be)
549 return (unsigned long) -ENOMEM;
552 if (bs == s && be == e) {
553 /* Move from free list to taken list */
554 list_del(&blk->list);
558 attach_taken_block(info, blk);
564 /* blk still in free list, with updated start and/or size */
565 if (bs == s || be == e) {
571 /* The front free fragment */
574 /* The back free fragment */
575 newblk2 = get_slot(info);
577 newblk2->size = be - e;
579 list_add(&newblk2->list, &blk->list);
582 newblk1 = get_slot(info);
584 newblk1->size = e - s;
585 newblk1->owner = owner;
587 start = newblk1->start;
588 attach_taken_block(info, newblk1);
593 /* Deallocate the memory previously allocated by one of the rh_alloc functions.
594 * The return value is the size of the deallocated block, or a negative number
595 * if there is an error.
597 int rh_free(rh_info_t * info, unsigned long start)
599 rh_block_t *blk, *blk2;
603 /* Linear search for block */
605 list_for_each(l, &info->taken_list) {
606 blk2 = list_entry(l, rh_block_t, list);
607 if (start < blk2->start)
612 if (blk == NULL || start > (blk->start + blk->size))
615 /* Remove from taken list */
616 list_del(&blk->list);
618 /* Get size of freed block */
620 attach_free_block(info, blk);
625 int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
635 h = &info->free_list;
639 h = &info->taken_list;
646 /* Linear search for block */
648 list_for_each(l, h) {
649 blk = list_entry(l, rh_block_t, list);
650 if (stats != NULL && nr < max_stats) {
651 stats->start = blk->start;
652 stats->size = blk->size;
653 stats->owner = blk->owner;
662 int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
664 rh_block_t *blk, *blk2;
668 /* Linear search for block */
670 list_for_each(l, &info->taken_list) {
671 blk2 = list_entry(l, rh_block_t, list);
672 if (start < blk2->start)
677 if (blk == NULL || start > (blk->start + blk->size))
686 void rh_dump(rh_info_t * info)
688 static rh_stats_t st[32]; /* XXX maximum 32 blocks */
692 maxnr = ARRAY_SIZE(st);
695 "info @0x%p (%d slots empty / %d max)\n",
696 info, info->empty_slots, info->max_blocks);
698 printk(KERN_INFO " Free:\n");
699 nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
702 for (i = 0; i < nr; i++)
704 " 0x%lx-0x%lx (%u)\n",
705 st[i].start, st[i].start + st[i].size,
707 printk(KERN_INFO "\n");
709 printk(KERN_INFO " Taken:\n");
710 nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
713 for (i = 0; i < nr; i++)
715 " 0x%lx-0x%lx (%u) %s\n",
716 st[i].start, st[i].start + st[i].size,
717 st[i].size, st[i].owner != NULL ? st[i].owner : "");
718 printk(KERN_INFO "\n");
721 void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
724 "blk @0x%p: 0x%lx-0x%lx (%u)\n",
725 blk, blk->start, blk->start + blk->size, blk->size);