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_align(rh_info_t
* info
, int size
, int alignment
, const char *owner
)
433 /* Validate size, (must be power of two) */
434 if (size
<= 0 || (alignment
& (alignment
- 1)) != 0)
435 return ERR_PTR(-EINVAL
);
437 /* given alignment larger that default rheap alignment */
438 if (alignment
> info
->alignment
)
439 size
+= alignment
- 1;
441 /* Align to configured alignment */
442 size
= (size
+ (info
->alignment
- 1)) & ~(info
->alignment
- 1);
444 if (assure_empty(info
, 1) < 0)
445 return ERR_PTR(-ENOMEM
);
448 list_for_each(l
, &info
->free_list
) {
449 blk
= list_entry(l
, rh_block_t
, list
);
450 if (size
<= blk
->size
)
456 return ERR_PTR(-ENOMEM
);
459 if (blk
->size
== size
) {
460 /* Move from free list to taken list */
461 list_del(&blk
->list
);
465 attach_taken_block(info
, blk
);
470 newblk
= get_slot(info
);
471 newblk
->start
= blk
->start
;
473 newblk
->owner
= owner
;
475 /* blk still in free list, with updated start, size */
476 blk
->start
= (int8_t *)blk
->start
+ size
;
479 start
= newblk
->start
;
481 attach_taken_block(info
, newblk
);
483 /* for larger alignment return fixed up pointer */
484 /* this is no problem with the deallocator since */
485 /* we scan for pointers that lie in the blocks */
486 if (alignment
> info
->alignment
)
487 start
= (void *)(((unsigned long)start
+ alignment
- 1) &
493 void *rh_alloc(rh_info_t
* info
, int size
, const char *owner
)
495 return rh_alloc_align(info
, size
, info
->alignment
, owner
);
498 /* allocate at precisely the given address */
499 void *rh_alloc_fixed(rh_info_t
* info
, void *start
, int size
, const char *owner
)
502 rh_block_t
*blk
, *newblk1
, *newblk2
;
503 unsigned long s
, e
, m
, bs
= 0, be
= 0;
507 return ERR_PTR(-EINVAL
);
509 /* The region must be aligned */
510 s
= (unsigned long)start
;
512 m
= info
->alignment
- 1;
520 if (assure_empty(info
, 2) < 0)
521 return ERR_PTR(-ENOMEM
);
524 list_for_each(l
, &info
->free_list
) {
525 blk
= list_entry(l
, rh_block_t
, list
);
526 /* The range must lie entirely inside one free block */
527 bs
= (unsigned long)blk
->start
;
528 be
= (unsigned long)blk
->start
+ blk
->size
;
529 if (s
>= bs
&& e
<= be
)
534 return ERR_PTR(-ENOMEM
);
537 if (bs
== s
&& be
== e
) {
538 /* Move from free list to taken list */
539 list_del(&blk
->list
);
543 attach_taken_block(info
, blk
);
549 /* blk still in free list, with updated start and/or size */
550 if (bs
== s
|| be
== e
) {
552 blk
->start
= (int8_t *)blk
->start
+ size
;
556 /* The front free fragment */
559 /* The back free fragment */
560 newblk2
= get_slot(info
);
561 newblk2
->start
= (void *)e
;
562 newblk2
->size
= be
- e
;
564 list_add(&newblk2
->list
, &blk
->list
);
567 newblk1
= get_slot(info
);
568 newblk1
->start
= (void *)s
;
569 newblk1
->size
= e
- s
;
570 newblk1
->owner
= owner
;
572 start
= newblk1
->start
;
573 attach_taken_block(info
, newblk1
);
578 int rh_free(rh_info_t
* info
, void *start
)
580 rh_block_t
*blk
, *blk2
;
584 /* Linear search for block */
586 list_for_each(l
, &info
->taken_list
) {
587 blk2
= list_entry(l
, rh_block_t
, list
);
588 if (start
< blk2
->start
)
593 if (blk
== NULL
|| start
> (blk
->start
+ blk
->size
))
596 /* Remove from taken list */
597 list_del(&blk
->list
);
599 /* Get size of freed block */
601 attach_free_block(info
, blk
);
606 int rh_get_stats(rh_info_t
* info
, int what
, int max_stats
, rh_stats_t
* stats
)
616 h
= &info
->free_list
;
620 h
= &info
->taken_list
;
627 /* Linear search for block */
629 list_for_each(l
, h
) {
630 blk
= list_entry(l
, rh_block_t
, list
);
631 if (stats
!= NULL
&& nr
< max_stats
) {
632 stats
->start
= blk
->start
;
633 stats
->size
= blk
->size
;
634 stats
->owner
= blk
->owner
;
643 int rh_set_owner(rh_info_t
* info
, void *start
, const char *owner
)
645 rh_block_t
*blk
, *blk2
;
649 /* Linear search for block */
651 list_for_each(l
, &info
->taken_list
) {
652 blk2
= list_entry(l
, rh_block_t
, list
);
653 if (start
< blk2
->start
)
658 if (blk
== NULL
|| start
> (blk
->start
+ blk
->size
))
667 void rh_dump(rh_info_t
* info
)
669 static rh_stats_t st
[32]; /* XXX maximum 32 blocks */
673 maxnr
= sizeof(st
) / sizeof(st
[0]);
676 "info @0x%p (%d slots empty / %d max)\n",
677 info
, info
->empty_slots
, info
->max_blocks
);
679 printk(KERN_INFO
" Free:\n");
680 nr
= rh_get_stats(info
, RHGS_FREE
, maxnr
, st
);
683 for (i
= 0; i
< nr
; i
++)
686 st
[i
].start
, (int8_t *) st
[i
].start
+ st
[i
].size
,
688 printk(KERN_INFO
"\n");
690 printk(KERN_INFO
" Taken:\n");
691 nr
= rh_get_stats(info
, RHGS_TAKEN
, maxnr
, st
);
694 for (i
= 0; i
< nr
; i
++)
696 " 0x%p-0x%p (%u) %s\n",
697 st
[i
].start
, (int8_t *) st
[i
].start
+ st
[i
].size
,
698 st
[i
].size
, st
[i
].owner
!= NULL
? st
[i
].owner
: "");
699 printk(KERN_INFO
"\n");
702 void rh_dump_blk(rh_info_t
* info
, rh_block_t
* blk
)
705 "blk @0x%p: 0x%p-0x%p (%u)\n",
706 blk
, blk
->start
, (int8_t *) blk
->start
+ blk
->size
, blk
->size
);