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>
18 #include <linux/module.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
23 #include <asm/rheap.h>
26 * Fixup a list_head, needed when copying lists. If the pointers fall
27 * between s and e, apply the delta. This assumes that
28 * sizeof(struct list_head *) == sizeof(unsigned long *).
30 static inline void fixup(unsigned long s
, unsigned long e
, int d
,
35 pp
= (unsigned long *)&l
->next
;
36 if (*pp
>= s
&& *pp
< e
)
39 pp
= (unsigned long *)&l
->prev
;
40 if (*pp
>= s
&& *pp
< e
)
44 /* Grow the allocated blocks */
45 static int grow(rh_info_t
* info
, int max_blocks
)
47 rh_block_t
*block
, *blk
;
50 unsigned long blks
, blke
;
52 if (max_blocks
<= info
->max_blocks
)
55 new_blocks
= max_blocks
- info
->max_blocks
;
57 block
= kmalloc(sizeof(rh_block_t
) * max_blocks
, GFP_ATOMIC
);
61 if (info
->max_blocks
> 0) {
63 /* copy old block area */
64 memcpy(block
, info
->block
,
65 sizeof(rh_block_t
) * info
->max_blocks
);
67 delta
= (char *)block
- (char *)info
->block
;
69 /* and fixup list pointers */
70 blks
= (unsigned long)info
->block
;
71 blke
= (unsigned long)(info
->block
+ info
->max_blocks
);
73 for (i
= 0, blk
= block
; i
< info
->max_blocks
; i
++, blk
++)
74 fixup(blks
, blke
, delta
, &blk
->list
);
76 fixup(blks
, blke
, delta
, &info
->empty_list
);
77 fixup(blks
, blke
, delta
, &info
->free_list
);
78 fixup(blks
, blke
, delta
, &info
->taken_list
);
80 /* free the old allocated memory */
81 if ((info
->flags
& RHIF_STATIC_BLOCK
) == 0)
86 info
->empty_slots
+= new_blocks
;
87 info
->max_blocks
= max_blocks
;
88 info
->flags
&= ~RHIF_STATIC_BLOCK
;
90 /* add all new blocks to the free list */
91 blk
= block
+ info
->max_blocks
- new_blocks
;
92 for (i
= 0; i
< new_blocks
; i
++, blk
++)
93 list_add(&blk
->list
, &info
->empty_list
);
99 * Assure at least the required amount of empty slots. If this function
100 * causes a grow in the block area then all pointers kept to the block
103 static int assure_empty(rh_info_t
* info
, int slots
)
107 /* This function is not meant to be used to grow uncontrollably */
112 if (info
->empty_slots
>= slots
)
115 /* Next 16 sized block */
116 max_blocks
= ((info
->max_blocks
+ slots
) + 15) & ~15;
118 return grow(info
, max_blocks
);
121 static rh_block_t
*get_slot(rh_info_t
* info
)
125 /* If no more free slots, and failure to extend. */
126 /* XXX: You should have called assure_empty before */
127 if (info
->empty_slots
== 0) {
128 printk(KERN_ERR
"rh: out of slots; crash is imminent.\n");
132 /* Get empty slot to use */
133 blk
= list_entry(info
->empty_list
.next
, rh_block_t
, list
);
134 list_del_init(&blk
->list
);
145 static inline void release_slot(rh_info_t
* info
, rh_block_t
* blk
)
147 list_add(&blk
->list
, &info
->empty_list
);
151 static void attach_free_block(rh_info_t
* info
, rh_block_t
* blkn
)
158 unsigned long s
, e
, bs
, be
;
161 /* We assume that they are aligned properly */
166 /* Find the blocks immediately before and after the given one
172 list_for_each(l
, &info
->free_list
) {
173 blk
= list_entry(l
, rh_block_t
, list
);
178 if (next
== NULL
&& s
>= bs
)
187 /* If both are not null, break now */
188 if (before
!= NULL
&& after
!= NULL
)
192 /* Now check if they are really adjacent */
193 if (before
&& s
!= (before
->start
+ before
->size
))
196 if (after
&& e
!= after
->start
)
199 /* No coalescing; list insert and return */
200 if (before
== NULL
&& after
== NULL
) {
203 list_add(&blkn
->list
, &next
->list
);
205 list_add(&blkn
->list
, &info
->free_list
);
210 /* We don't need it anymore */
211 release_slot(info
, blkn
);
213 /* Grow the before block */
214 if (before
!= NULL
&& after
== NULL
) {
215 before
->size
+= size
;
219 /* Grow the after block backwards */
220 if (before
== NULL
&& after
!= NULL
) {
221 after
->start
-= size
;
226 /* Grow the before block, and release the after block */
227 before
->size
+= size
+ after
->size
;
228 list_del(&after
->list
);
229 release_slot(info
, after
);
232 static void attach_taken_block(rh_info_t
* info
, rh_block_t
* blkn
)
237 /* Find the block immediately before the given one (if any) */
238 list_for_each(l
, &info
->taken_list
) {
239 blk
= list_entry(l
, rh_block_t
, list
);
240 if (blk
->start
> blkn
->start
) {
241 list_add_tail(&blkn
->list
, &blk
->list
);
246 list_add_tail(&blkn
->list
, &info
->taken_list
);
250 * Create a remote heap dynamically. Note that no memory for the blocks
251 * are allocated. It will upon the first allocation
253 rh_info_t
*rh_create(unsigned int alignment
)
257 /* Alignment must be a power of two */
258 if ((alignment
& (alignment
- 1)) != 0)
259 return ERR_PTR(-EINVAL
);
261 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
263 return ERR_PTR(-ENOMEM
);
265 info
->alignment
= alignment
;
267 /* Initially everything as empty */
269 info
->max_blocks
= 0;
270 info
->empty_slots
= 0;
273 INIT_LIST_HEAD(&info
->empty_list
);
274 INIT_LIST_HEAD(&info
->free_list
);
275 INIT_LIST_HEAD(&info
->taken_list
);
279 EXPORT_SYMBOL_GPL(rh_create
);
282 * Destroy a dynamically created remote heap. Deallocate only if the areas
285 void rh_destroy(rh_info_t
* info
)
287 if ((info
->flags
& RHIF_STATIC_BLOCK
) == 0 && info
->block
!= NULL
)
290 if ((info
->flags
& RHIF_STATIC_INFO
) == 0)
293 EXPORT_SYMBOL_GPL(rh_destroy
);
296 * Initialize in place a remote heap info block. This is needed to support
297 * operation very early in the startup of the kernel, when it is not yet safe
300 void rh_init(rh_info_t
* info
, unsigned int alignment
, int max_blocks
,
306 /* Alignment must be a power of two */
307 if ((alignment
& (alignment
- 1)) != 0)
310 info
->alignment
= alignment
;
312 /* Initially everything as empty */
314 info
->max_blocks
= max_blocks
;
315 info
->empty_slots
= max_blocks
;
316 info
->flags
= RHIF_STATIC_INFO
| RHIF_STATIC_BLOCK
;
318 INIT_LIST_HEAD(&info
->empty_list
);
319 INIT_LIST_HEAD(&info
->free_list
);
320 INIT_LIST_HEAD(&info
->taken_list
);
322 /* Add all new blocks to the free list */
323 for (i
= 0, blk
= block
; i
< max_blocks
; i
++, blk
++)
324 list_add(&blk
->list
, &info
->empty_list
);
326 EXPORT_SYMBOL_GPL(rh_init
);
328 /* Attach a free memory region, coalesces regions if adjuscent */
329 int rh_attach_region(rh_info_t
* info
, unsigned long start
, int size
)
332 unsigned long s
, e
, m
;
335 /* The region must be aligned */
338 m
= info
->alignment
- 1;
346 if (IS_ERR_VALUE(e
) || (e
< s
))
349 /* Take final values */
353 /* Grow the blocks, if needed */
354 r
= assure_empty(info
, 1);
358 blk
= get_slot(info
);
363 attach_free_block(info
, blk
);
367 EXPORT_SYMBOL_GPL(rh_attach_region
);
369 /* Detatch given address range, splits free block if needed. */
370 unsigned long rh_detach_region(rh_info_t
* info
, unsigned long start
, int size
)
373 rh_block_t
*blk
, *newblk
;
374 unsigned long s
, e
, m
, bs
, be
;
378 return (unsigned long) -EINVAL
;
380 /* The region must be aligned */
383 m
= info
->alignment
- 1;
391 if (assure_empty(info
, 1) < 0)
392 return (unsigned long) -ENOMEM
;
395 list_for_each(l
, &info
->free_list
) {
396 blk
= list_entry(l
, rh_block_t
, list
);
397 /* The range must lie entirely inside one free block */
399 be
= blk
->start
+ blk
->size
;
400 if (s
>= bs
&& e
<= be
)
406 return (unsigned long) -ENOMEM
;
409 if (bs
== s
&& be
== e
) {
410 /* Delete from free list, release slot */
411 list_del(&blk
->list
);
412 release_slot(info
, blk
);
416 /* blk still in free list, with updated start and/or size */
417 if (bs
== s
|| be
== e
) {
423 /* The front free fragment */
426 /* the back free fragment */
427 newblk
= get_slot(info
);
429 newblk
->size
= be
- e
;
431 list_add(&newblk
->list
, &blk
->list
);
436 EXPORT_SYMBOL_GPL(rh_detach_region
);
438 /* Allocate a block of memory at the specified alignment. The value returned
439 * is an offset into the buffer initialized by rh_init(), or a negative number
440 * if there is an error.
442 unsigned long rh_alloc_align(rh_info_t
* info
, int size
, int alignment
, const char *owner
)
447 unsigned long start
, sp_size
;
449 /* Validate size, and alignment must be power of two */
450 if (size
<= 0 || (alignment
& (alignment
- 1)) != 0)
451 return (unsigned long) -EINVAL
;
453 /* Align to configured alignment */
454 size
= (size
+ (info
->alignment
- 1)) & ~(info
->alignment
- 1);
456 if (assure_empty(info
, 2) < 0)
457 return (unsigned long) -ENOMEM
;
460 list_for_each(l
, &info
->free_list
) {
461 blk
= list_entry(l
, rh_block_t
, list
);
462 if (size
<= blk
->size
) {
463 start
= (blk
->start
+ alignment
- 1) & ~(alignment
- 1);
464 if (start
+ size
<= blk
->start
+ blk
->size
)
471 return (unsigned long) -ENOMEM
;
474 if (blk
->size
== size
) {
475 /* Move from free list to taken list */
476 list_del(&blk
->list
);
479 /* Fragment caused, split if needed */
480 /* Create block for fragment in the beginning */
481 sp_size
= start
- blk
->start
;
485 spblk
= get_slot(info
);
486 spblk
->start
= blk
->start
;
487 spblk
->size
= sp_size
;
488 /* add before the blk */
489 list_add(&spblk
->list
, blk
->list
.prev
);
491 newblk
= get_slot(info
);
492 newblk
->start
= start
;
495 /* blk still in free list, with updated start and size
496 * for fragment in the end */
497 blk
->start
= start
+ size
;
498 blk
->size
-= sp_size
+ size
;
499 /* No fragment in the end, remove blk */
500 if (blk
->size
== 0) {
501 list_del(&blk
->list
);
502 release_slot(info
, blk
);
506 newblk
->owner
= owner
;
507 attach_taken_block(info
, newblk
);
511 EXPORT_SYMBOL_GPL(rh_alloc_align
);
513 /* Allocate a block of memory at the default alignment. The value returned is
514 * an offset into the buffer initialized by rh_init(), or a negative number if
517 unsigned long rh_alloc(rh_info_t
* info
, int size
, const char *owner
)
519 return rh_alloc_align(info
, size
, info
->alignment
, owner
);
521 EXPORT_SYMBOL_GPL(rh_alloc
);
523 /* Allocate a block of memory at the given offset, rounded up to the default
524 * alignment. The value returned is an offset into the buffer initialized by
525 * rh_init(), or a negative number if there is an error.
527 unsigned long rh_alloc_fixed(rh_info_t
* info
, unsigned long start
, int size
, const char *owner
)
530 rh_block_t
*blk
, *newblk1
, *newblk2
;
531 unsigned long s
, e
, m
, bs
= 0, be
= 0;
535 return (unsigned long) -EINVAL
;
537 /* The region must be aligned */
540 m
= info
->alignment
- 1;
548 if (assure_empty(info
, 2) < 0)
549 return (unsigned long) -ENOMEM
;
552 list_for_each(l
, &info
->free_list
) {
553 blk
= list_entry(l
, rh_block_t
, list
);
554 /* The range must lie entirely inside one free block */
556 be
= blk
->start
+ blk
->size
;
557 if (s
>= bs
&& e
<= be
)
563 return (unsigned long) -ENOMEM
;
566 if (bs
== s
&& be
== e
) {
567 /* Move from free list to taken list */
568 list_del(&blk
->list
);
572 attach_taken_block(info
, blk
);
578 /* blk still in free list, with updated start and/or size */
579 if (bs
== s
|| be
== e
) {
585 /* The front free fragment */
588 /* The back free fragment */
589 newblk2
= get_slot(info
);
591 newblk2
->size
= be
- e
;
593 list_add(&newblk2
->list
, &blk
->list
);
596 newblk1
= get_slot(info
);
598 newblk1
->size
= e
- s
;
599 newblk1
->owner
= owner
;
601 start
= newblk1
->start
;
602 attach_taken_block(info
, newblk1
);
606 EXPORT_SYMBOL_GPL(rh_alloc_fixed
);
608 /* Deallocate the memory previously allocated by one of the rh_alloc functions.
609 * The return value is the size of the deallocated block, or a negative number
610 * if there is an error.
612 int rh_free(rh_info_t
* info
, unsigned long start
)
614 rh_block_t
*blk
, *blk2
;
618 /* Linear search for block */
620 list_for_each(l
, &info
->taken_list
) {
621 blk2
= list_entry(l
, rh_block_t
, list
);
622 if (start
< blk2
->start
)
627 if (blk
== NULL
|| start
> (blk
->start
+ blk
->size
))
630 /* Remove from taken list */
631 list_del(&blk
->list
);
633 /* Get size of freed block */
635 attach_free_block(info
, blk
);
639 EXPORT_SYMBOL_GPL(rh_free
);
641 int rh_get_stats(rh_info_t
* info
, int what
, int max_stats
, rh_stats_t
* stats
)
651 h
= &info
->free_list
;
655 h
= &info
->taken_list
;
662 /* Linear search for block */
664 list_for_each(l
, h
) {
665 blk
= list_entry(l
, rh_block_t
, list
);
666 if (stats
!= NULL
&& nr
< max_stats
) {
667 stats
->start
= blk
->start
;
668 stats
->size
= blk
->size
;
669 stats
->owner
= blk
->owner
;
677 EXPORT_SYMBOL_GPL(rh_get_stats
);
679 int rh_set_owner(rh_info_t
* info
, unsigned long start
, const char *owner
)
681 rh_block_t
*blk
, *blk2
;
685 /* Linear search for block */
687 list_for_each(l
, &info
->taken_list
) {
688 blk2
= list_entry(l
, rh_block_t
, list
);
689 if (start
< blk2
->start
)
694 if (blk
== NULL
|| start
> (blk
->start
+ blk
->size
))
702 EXPORT_SYMBOL_GPL(rh_set_owner
);
704 void rh_dump(rh_info_t
* info
)
706 static rh_stats_t st
[32]; /* XXX maximum 32 blocks */
710 maxnr
= ARRAY_SIZE(st
);
713 "info @0x%p (%d slots empty / %d max)\n",
714 info
, info
->empty_slots
, info
->max_blocks
);
716 printk(KERN_INFO
" Free:\n");
717 nr
= rh_get_stats(info
, RHGS_FREE
, maxnr
, st
);
720 for (i
= 0; i
< nr
; i
++)
722 " 0x%lx-0x%lx (%u)\n",
723 st
[i
].start
, st
[i
].start
+ st
[i
].size
,
725 printk(KERN_INFO
"\n");
727 printk(KERN_INFO
" Taken:\n");
728 nr
= rh_get_stats(info
, RHGS_TAKEN
, maxnr
, st
);
731 for (i
= 0; i
< nr
; i
++)
733 " 0x%lx-0x%lx (%u) %s\n",
734 st
[i
].start
, st
[i
].start
+ st
[i
].size
,
735 st
[i
].size
, st
[i
].owner
!= NULL
? st
[i
].owner
: "");
736 printk(KERN_INFO
"\n");
738 EXPORT_SYMBOL_GPL(rh_dump
);
740 void rh_dump_blk(rh_info_t
* info
, rh_block_t
* blk
)
743 "blk @0x%p: 0x%lx-0x%lx (%u)\n",
744 blk
, blk
->start
, blk
->start
+ blk
->size
, blk
->size
);
746 EXPORT_SYMBOL_GPL(rh_dump_blk
);