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/err.h>
20 #include <linux/slab.h>
22 #include <asm/rheap.h>
25 * Fixup a list_head, needed when copying lists. If the pointers fall
26 * between s and e, apply the delta. This assumes that
27 * sizeof(struct list_head *) == sizeof(unsigned long *).
29 static inline void fixup(unsigned long s, unsigned long e, int d,
34 pp = (unsigned long *)&l->next;
35 if (*pp >= s && *pp < e)
38 pp = (unsigned long *)&l->prev;
39 if (*pp >= s && *pp < e)
43 /* Grow the allocated blocks */
44 static int grow(rh_info_t * info, int max_blocks)
46 rh_block_t *block, *blk;
49 unsigned long blks, blke;
51 if (max_blocks <= info->max_blocks)
54 new_blocks = max_blocks - info->max_blocks;
56 block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
60 if (info->max_blocks > 0) {
62 /* copy old block area */
63 memcpy(block, info->block,
64 sizeof(rh_block_t) * info->max_blocks);
66 delta = (char *)block - (char *)info->block;
68 /* and fixup list pointers */
69 blks = (unsigned long)info->block;
70 blke = (unsigned long)(info->block + info->max_blocks);
72 for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
73 fixup(blks, blke, delta, &blk->list);
75 fixup(blks, blke, delta, &info->empty_list);
76 fixup(blks, blke, delta, &info->free_list);
77 fixup(blks, blke, delta, &info->taken_list);
79 /* free the old allocated memory */
80 if ((info->flags & RHIF_STATIC_BLOCK) == 0)
85 info->empty_slots += new_blocks;
86 info->max_blocks = max_blocks;
87 info->flags &= ~RHIF_STATIC_BLOCK;
89 /* add all new blocks to the free list */
90 blk = block + info->max_blocks - new_blocks;
91 for (i = 0; i < new_blocks; i++, blk++)
92 list_add(&blk->list, &info->empty_list);
98 * Assure at least the required amount of empty slots. If this function
99 * causes a grow in the block area then all pointers kept to the block
102 static int assure_empty(rh_info_t * info, int slots)
106 /* This function is not meant to be used to grow uncontrollably */
111 if (info->empty_slots >= slots)
114 /* Next 16 sized block */
115 max_blocks = ((info->max_blocks + slots) + 15) & ~15;
117 return grow(info, max_blocks);
120 static rh_block_t *get_slot(rh_info_t * info)
124 /* If no more free slots, and failure to extend. */
125 /* XXX: You should have called assure_empty before */
126 if (info->empty_slots == 0) {
127 printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
131 /* Get empty slot to use */
132 blk = list_entry(info->empty_list.next, rh_block_t, list);
133 list_del_init(&blk->list);
144 static inline void release_slot(rh_info_t * info, rh_block_t * blk)
146 list_add(&blk->list, &info->empty_list);
150 static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
157 unsigned long s, e, bs, be;
160 /* We assume that they are aligned properly */
165 /* Find the blocks immediately before and after the given one
171 list_for_each(l, &info->free_list) {
172 blk = list_entry(l, rh_block_t, list);
177 if (next == NULL && s >= bs)
186 /* If both are not null, break now */
187 if (before != NULL && after != NULL)
191 /* Now check if they are really adjacent */
192 if (before && s != (before->start + before->size))
195 if (after && e != after->start)
198 /* No coalescing; list insert and return */
199 if (before == NULL && after == NULL) {
202 list_add(&blkn->list, &next->list);
204 list_add(&blkn->list, &info->free_list);
209 /* We don't need it anymore */
210 release_slot(info, blkn);
212 /* Grow the before block */
213 if (before != NULL && after == NULL) {
214 before->size += size;
218 /* Grow the after block backwards */
219 if (before == NULL && after != NULL) {
220 after->start -= size;
225 /* Grow the before block, and release the after block */
226 before->size += size + after->size;
227 list_del(&after->list);
228 release_slot(info, after);
231 static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
236 /* Find the block immediately before the given one (if any) */
237 list_for_each(l, &info->taken_list) {
238 blk = list_entry(l, rh_block_t, list);
239 if (blk->start > blkn->start) {
240 list_add_tail(&blkn->list, &blk->list);
245 list_add_tail(&blkn->list, &info->taken_list);
249 * Create a remote heap dynamically. Note that no memory for the blocks
250 * are allocated. It will upon the first allocation
252 rh_info_t *rh_create(unsigned int alignment)
256 /* Alignment must be a power of two */
257 if ((alignment & (alignment - 1)) != 0)
258 return ERR_PTR(-EINVAL);
260 info = kmalloc(sizeof(*info), GFP_KERNEL);
262 return ERR_PTR(-ENOMEM);
264 info->alignment = alignment;
266 /* Initially everything as empty */
268 info->max_blocks = 0;
269 info->empty_slots = 0;
272 INIT_LIST_HEAD(&info->empty_list);
273 INIT_LIST_HEAD(&info->free_list);
274 INIT_LIST_HEAD(&info->taken_list);
280 * Destroy a dynamically created remote heap. Deallocate only if the areas
283 void rh_destroy(rh_info_t * info)
285 if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
288 if ((info->flags & RHIF_STATIC_INFO) == 0)
293 * Initialize in place a remote heap info block. This is needed to support
294 * operation very early in the startup of the kernel, when it is not yet safe
297 void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
303 /* Alignment must be a power of two */
304 if ((alignment & (alignment - 1)) != 0)
307 info->alignment = alignment;
309 /* Initially everything as empty */
311 info->max_blocks = max_blocks;
312 info->empty_slots = max_blocks;
313 info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
315 INIT_LIST_HEAD(&info->empty_list);
316 INIT_LIST_HEAD(&info->free_list);
317 INIT_LIST_HEAD(&info->taken_list);
319 /* Add all new blocks to the free list */
320 for (i = 0, blk = block; i < max_blocks; i++, blk++)
321 list_add(&blk->list, &info->empty_list);
324 /* Attach a free memory region, coalesces regions if adjuscent */
325 int rh_attach_region(rh_info_t * info, unsigned long start, int size)
328 unsigned long s, e, m;
331 /* The region must be aligned */
334 m = info->alignment - 1;
342 if (IS_ERR_VALUE(e) || (e < s))
345 /* Take final values */
349 /* Grow the blocks, if needed */
350 r = assure_empty(info, 1);
354 blk = get_slot(info);
359 attach_free_block(info, blk);
364 /* Detatch given address range, splits free block if needed. */
365 unsigned long rh_detach_region(rh_info_t * info, unsigned long start, int size)
368 rh_block_t *blk, *newblk;
369 unsigned long s, e, m, bs, be;
373 return (unsigned long) -EINVAL;
375 /* The region must be aligned */
378 m = info->alignment - 1;
386 if (assure_empty(info, 1) < 0)
387 return (unsigned long) -ENOMEM;
390 list_for_each(l, &info->free_list) {
391 blk = list_entry(l, rh_block_t, list);
392 /* The range must lie entirely inside one free block */
394 be = blk->start + blk->size;
395 if (s >= bs && e <= be)
401 return (unsigned long) -ENOMEM;
404 if (bs == s && be == e) {
405 /* Delete from free list, release slot */
406 list_del(&blk->list);
407 release_slot(info, blk);
411 /* blk still in free list, with updated start and/or size */
412 if (bs == s || be == e) {
418 /* The front free fragment */
421 /* the back free fragment */
422 newblk = get_slot(info);
424 newblk->size = be - e;
426 list_add(&newblk->list, &blk->list);
432 /* Allocate a block of memory at the specified alignment. The value returned
433 * is an offset into the buffer initialized by rh_init(), or a negative number
434 * if there is an error.
436 unsigned long rh_alloc_align(rh_info_t * info, int size, int alignment, const char *owner)
441 unsigned long start, sp_size;
443 /* Validate size, and alignment must be power of two */
444 if (size <= 0 || (alignment & (alignment - 1)) != 0)
445 return (unsigned long) -EINVAL;
447 /* Align to configured alignment */
448 size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
450 if (assure_empty(info, 2) < 0)
451 return (unsigned long) -ENOMEM;
454 list_for_each(l, &info->free_list) {
455 blk = list_entry(l, rh_block_t, list);
456 if (size <= blk->size) {
457 start = (blk->start + alignment - 1) & ~(alignment - 1);
458 if (start + size <= blk->start + 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);
473 /* Fragment caused, split if needed */
474 /* Create block for fragment in the beginning */
475 sp_size = start - blk->start;
479 spblk = get_slot(info);
480 spblk->start = blk->start;
481 spblk->size = sp_size;
482 /* add before the blk */
483 list_add(&spblk->list, blk->list.prev);
485 newblk = get_slot(info);
486 newblk->start = start;
489 /* blk still in free list, with updated start and size
490 * for fragment in the end */
491 blk->start = start + size;
492 blk->size -= sp_size + size;
493 /* No fragment in the end, remove blk */
494 if (blk->size == 0) {
495 list_del(&blk->list);
496 release_slot(info, blk);
500 newblk->owner = owner;
501 attach_taken_block(info, newblk);
506 /* Allocate a block of memory at the default alignment. The value returned is
507 * an offset into the buffer initialized by rh_init(), or a negative number if
510 unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
512 return rh_alloc_align(info, size, info->alignment, owner);
515 /* Allocate a block of memory at the given offset, rounded up to the default
516 * alignment. The value returned is an offset into the buffer initialized by
517 * rh_init(), or a negative number if there is an error.
519 unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
522 rh_block_t *blk, *newblk1, *newblk2;
523 unsigned long s, e, m, bs = 0, be = 0;
527 return (unsigned long) -EINVAL;
529 /* The region must be aligned */
532 m = info->alignment - 1;
540 if (assure_empty(info, 2) < 0)
541 return (unsigned long) -ENOMEM;
544 list_for_each(l, &info->free_list) {
545 blk = list_entry(l, rh_block_t, list);
546 /* The range must lie entirely inside one free block */
548 be = blk->start + blk->size;
549 if (s >= bs && e <= be)
554 return (unsigned long) -ENOMEM;
557 if (bs == s && be == e) {
558 /* Move from free list to taken list */
559 list_del(&blk->list);
563 attach_taken_block(info, blk);
569 /* blk still in free list, with updated start and/or size */
570 if (bs == s || be == e) {
576 /* The front free fragment */
579 /* The back free fragment */
580 newblk2 = get_slot(info);
582 newblk2->size = be - e;
584 list_add(&newblk2->list, &blk->list);
587 newblk1 = get_slot(info);
589 newblk1->size = e - s;
590 newblk1->owner = owner;
592 start = newblk1->start;
593 attach_taken_block(info, newblk1);
598 /* Deallocate the memory previously allocated by one of the rh_alloc functions.
599 * The return value is the size of the deallocated block, or a negative number
600 * if there is an error.
602 int rh_free(rh_info_t * info, unsigned long start)
604 rh_block_t *blk, *blk2;
608 /* Linear search for block */
610 list_for_each(l, &info->taken_list) {
611 blk2 = list_entry(l, rh_block_t, list);
612 if (start < blk2->start)
617 if (blk == NULL || start > (blk->start + blk->size))
620 /* Remove from taken list */
621 list_del(&blk->list);
623 /* Get size of freed block */
625 attach_free_block(info, blk);
630 int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
640 h = &info->free_list;
644 h = &info->taken_list;
651 /* Linear search for block */
653 list_for_each(l, h) {
654 blk = list_entry(l, rh_block_t, list);
655 if (stats != NULL && nr < max_stats) {
656 stats->start = blk->start;
657 stats->size = blk->size;
658 stats->owner = blk->owner;
667 int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
669 rh_block_t *blk, *blk2;
673 /* Linear search for block */
675 list_for_each(l, &info->taken_list) {
676 blk2 = list_entry(l, rh_block_t, list);
677 if (start < blk2->start)
682 if (blk == NULL || start > (blk->start + blk->size))
691 void rh_dump(rh_info_t * info)
693 static rh_stats_t st[32]; /* XXX maximum 32 blocks */
697 maxnr = ARRAY_SIZE(st);
700 "info @0x%p (%d slots empty / %d max)\n",
701 info, info->empty_slots, info->max_blocks);
703 printk(KERN_INFO " Free:\n");
704 nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
707 for (i = 0; i < nr; i++)
709 " 0x%lx-0x%lx (%u)\n",
710 st[i].start, st[i].start + st[i].size,
712 printk(KERN_INFO "\n");
714 printk(KERN_INFO " Taken:\n");
715 nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
718 for (i = 0; i < nr; i++)
720 " 0x%lx-0x%lx (%u) %s\n",
721 st[i].start, st[i].start + st[i].size,
722 st[i].size, st[i].owner != NULL ? st[i].owner : "");
723 printk(KERN_INFO "\n");
726 void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
729 "blk @0x%p: 0x%lx-0x%lx (%u)\n",
730 blk, blk->start, blk->start + blk->size, blk->size);