rheap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  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
  6. * endianess.
  7. *
  8. * Author: Pantelis Antoniou <panto@intracom.gr>
  9. *
  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
  13. * or implied.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <asm/rheap.h>
  22. /*
  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 *).
  26. */
  27. static inline void fixup(unsigned long s, unsigned long e, int d,
  28. struct list_head *l)
  29. {
  30. unsigned long *pp;
  31. pp = (unsigned long *)&l->next;
  32. if (*pp >= s && *pp < e)
  33. *pp += d;
  34. pp = (unsigned long *)&l->prev;
  35. if (*pp >= s && *pp < e)
  36. *pp += d;
  37. }
  38. /* Grow the allocated blocks */
  39. static int grow(rh_info_t * info, int max_blocks)
  40. {
  41. rh_block_t *block, *blk;
  42. int i, new_blocks;
  43. int delta;
  44. unsigned long blks, blke;
  45. if (max_blocks <= info->max_blocks)
  46. return -EINVAL;
  47. new_blocks = max_blocks - info->max_blocks;
  48. block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
  49. if (block == NULL)
  50. return -ENOMEM;
  51. if (info->max_blocks > 0) {
  52. /* copy old block area */
  53. memcpy(block, info->block,
  54. sizeof(rh_block_t) * info->max_blocks);
  55. delta = (char *)block - (char *)info->block;
  56. /* and fixup list pointers */
  57. blks = (unsigned long)info->block;
  58. blke = (unsigned long)(info->block + info->max_blocks);
  59. for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
  60. fixup(blks, blke, delta, &blk->list);
  61. fixup(blks, blke, delta, &info->empty_list);
  62. fixup(blks, blke, delta, &info->free_list);
  63. fixup(blks, blke, delta, &info->taken_list);
  64. /* free the old allocated memory */
  65. if ((info->flags & RHIF_STATIC_BLOCK) == 0)
  66. kfree(info->block);
  67. }
  68. info->block = block;
  69. info->empty_slots += new_blocks;
  70. info->max_blocks = max_blocks;
  71. info->flags &= ~RHIF_STATIC_BLOCK;
  72. /* add all new blocks to the free list */
  73. blk = block + info->max_blocks - new_blocks;
  74. for (i = 0; i < new_blocks; i++, blk++)
  75. list_add(&blk->list, &info->empty_list);
  76. return 0;
  77. }
  78. /*
  79. * Assure at least the required amount of empty slots. If this function
  80. * causes a grow in the block area then all pointers kept to the block
  81. * area are invalid!
  82. */
  83. static int assure_empty(rh_info_t * info, int slots)
  84. {
  85. int max_blocks;
  86. /* This function is not meant to be used to grow uncontrollably */
  87. if (slots >= 4)
  88. return -EINVAL;
  89. /* Enough space */
  90. if (info->empty_slots >= slots)
  91. return 0;
  92. /* Next 16 sized block */
  93. max_blocks = ((info->max_blocks + slots) + 15) & ~15;
  94. return grow(info, max_blocks);
  95. }
  96. static rh_block_t *get_slot(rh_info_t * info)
  97. {
  98. rh_block_t *blk;
  99. /* If no more free slots, and failure to extend. */
  100. /* XXX: You should have called assure_empty before */
  101. if (info->empty_slots == 0) {
  102. printk(KERN_ERR "rh: out of slots; crash is imminent.\n");
  103. return NULL;
  104. }
  105. /* Get empty slot to use */
  106. blk = list_entry(info->empty_list.next, rh_block_t, list);
  107. list_del_init(&blk->list);
  108. info->empty_slots--;
  109. /* Initialize */
  110. blk->start = 0;
  111. blk->size = 0;
  112. blk->owner = NULL;
  113. return blk;
  114. }
  115. static inline void release_slot(rh_info_t * info, rh_block_t * blk)
  116. {
  117. list_add(&blk->list, &info->empty_list);
  118. info->empty_slots++;
  119. }
  120. static void attach_free_block(rh_info_t * info, rh_block_t * blkn)
  121. {
  122. rh_block_t *blk;
  123. rh_block_t *before;
  124. rh_block_t *after;
  125. rh_block_t *next;
  126. int size;
  127. unsigned long s, e, bs, be;
  128. struct list_head *l;
  129. /* We assume that they are aligned properly */
  130. size = blkn->size;
  131. s = blkn->start;
  132. e = s + size;
  133. /* Find the blocks immediately before and after the given one
  134. * (if any) */
  135. before = NULL;
  136. after = NULL;
  137. next = NULL;
  138. list_for_each(l, &info->free_list) {
  139. blk = list_entry(l, rh_block_t, list);
  140. bs = blk->start;
  141. be = bs + blk->size;
  142. if (next == NULL && s >= bs)
  143. next = blk;
  144. if (be == s)
  145. before = blk;
  146. if (e == bs)
  147. after = blk;
  148. /* If both are not null, break now */
  149. if (before != NULL && after != NULL)
  150. break;
  151. }
  152. /* Now check if they are really adjacent */
  153. if (before && s != (before->start + before->size))
  154. before = NULL;
  155. if (after && e != after->start)
  156. after = NULL;
  157. /* No coalescing; list insert and return */
  158. if (before == NULL && after == NULL) {
  159. if (next != NULL)
  160. list_add(&blkn->list, &next->list);
  161. else
  162. list_add(&blkn->list, &info->free_list);
  163. return;
  164. }
  165. /* We don't need it anymore */
  166. release_slot(info, blkn);
  167. /* Grow the before block */
  168. if (before != NULL && after == NULL) {
  169. before->size += size;
  170. return;
  171. }
  172. /* Grow the after block backwards */
  173. if (before == NULL && after != NULL) {
  174. after->start -= size;
  175. after->size += size;
  176. return;
  177. }
  178. /* Grow the before block, and release the after block */
  179. before->size += size + after->size;
  180. list_del(&after->list);
  181. release_slot(info, after);
  182. }
  183. static void attach_taken_block(rh_info_t * info, rh_block_t * blkn)
  184. {
  185. rh_block_t *blk;
  186. struct list_head *l;
  187. /* Find the block immediately before the given one (if any) */
  188. list_for_each(l, &info->taken_list) {
  189. blk = list_entry(l, rh_block_t, list);
  190. if (blk->start > blkn->start) {
  191. list_add_tail(&blkn->list, &blk->list);
  192. return;
  193. }
  194. }
  195. list_add_tail(&blkn->list, &info->taken_list);
  196. }
  197. /*
  198. * Create a remote heap dynamically. Note that no memory for the blocks
  199. * are allocated. It will upon the first allocation
  200. */
  201. rh_info_t *rh_create(unsigned int alignment)
  202. {
  203. rh_info_t *info;
  204. /* Alignment must be a power of two */
  205. if ((alignment & (alignment - 1)) != 0)
  206. return ERR_PTR(-EINVAL);
  207. info = kmalloc(sizeof(*info), GFP_KERNEL);
  208. if (info == NULL)
  209. return ERR_PTR(-ENOMEM);
  210. info->alignment = alignment;
  211. /* Initially everything as empty */
  212. info->block = NULL;
  213. info->max_blocks = 0;
  214. info->empty_slots = 0;
  215. info->flags = 0;
  216. INIT_LIST_HEAD(&info->empty_list);
  217. INIT_LIST_HEAD(&info->free_list);
  218. INIT_LIST_HEAD(&info->taken_list);
  219. return info;
  220. }
  221. /*
  222. * Destroy a dynamically created remote heap. Deallocate only if the areas
  223. * are not static
  224. */
  225. void rh_destroy(rh_info_t * info)
  226. {
  227. if ((info->flags & RHIF_STATIC_BLOCK) == 0 && info->block != NULL)
  228. kfree(info->block);
  229. if ((info->flags & RHIF_STATIC_INFO) == 0)
  230. kfree(info);
  231. }
  232. /*
  233. * Initialize in place a remote heap info block. This is needed to support
  234. * operation very early in the startup of the kernel, when it is not yet safe
  235. * to call kmalloc.
  236. */
  237. void rh_init(rh_info_t * info, unsigned int alignment, int max_blocks,
  238. rh_block_t * block)
  239. {
  240. int i;
  241. rh_block_t *blk;
  242. /* Alignment must be a power of two */
  243. if ((alignment & (alignment - 1)) != 0)
  244. return;
  245. info->alignment = alignment;
  246. /* Initially everything as empty */
  247. info->block = block;
  248. info->max_blocks = max_blocks;
  249. info->empty_slots = max_blocks;
  250. info->flags = RHIF_STATIC_INFO | RHIF_STATIC_BLOCK;
  251. INIT_LIST_HEAD(&info->empty_list);
  252. INIT_LIST_HEAD(&info->free_list);
  253. INIT_LIST_HEAD(&info->taken_list);
  254. /* Add all new blocks to the free list */
  255. for (i = 0, blk = block; i < max_blocks; i++, blk++)
  256. list_add(&blk->list, &info->empty_list);
  257. }
  258. /* Attach a free memory region, coalesces regions if adjuscent */
  259. int rh_attach_region(rh_info_t * info, unsigned long start, int size)
  260. {
  261. rh_block_t *blk;
  262. unsigned long s, e, m;
  263. int r;
  264. /* The region must be aligned */
  265. s = start;
  266. e = s + size;
  267. m = info->alignment - 1;
  268. /* Round start up */
  269. s = (s + m) & ~m;
  270. /* Round end down */
  271. e = e & ~m;
  272. if (IS_ERR_VALUE(e) || (e < s))
  273. return -ERANGE;
  274. /* Take final values */
  275. start = s;
  276. size = e - s;
  277. /* Grow the blocks, if needed */
  278. r = assure_empty(info, 1);
  279. if (r < 0)
  280. return r;
  281. blk = get_slot(info);
  282. blk->start = start;
  283. blk->size = size;
  284. blk->owner = NULL;
  285. attach_free_block(info, blk);
  286. return 0;
  287. }
  288. /* Detatch given address range, splits free block if needed. */
  289. unsigned long rh_detach_region(rh_info_t * info, unsigned long start, int size)
  290. {
  291. struct list_head *l;
  292. rh_block_t *blk, *newblk;
  293. unsigned long s, e, m, bs, be;
  294. /* Validate size */
  295. if (size <= 0)
  296. return (unsigned long) -EINVAL;
  297. /* The region must be aligned */
  298. s = start;
  299. e = s + size;
  300. m = info->alignment - 1;
  301. /* Round start up */
  302. s = (s + m) & ~m;
  303. /* Round end down */
  304. e = e & ~m;
  305. if (assure_empty(info, 1) < 0)
  306. return (unsigned long) -ENOMEM;
  307. blk = NULL;
  308. list_for_each(l, &info->free_list) {
  309. blk = list_entry(l, rh_block_t, list);
  310. /* The range must lie entirely inside one free block */
  311. bs = blk->start;
  312. be = blk->start + blk->size;
  313. if (s >= bs && e <= be)
  314. break;
  315. blk = NULL;
  316. }
  317. if (blk == NULL)
  318. return (unsigned long) -ENOMEM;
  319. /* Perfect fit */
  320. if (bs == s && be == e) {
  321. /* Delete from free list, release slot */
  322. list_del(&blk->list);
  323. release_slot(info, blk);
  324. return s;
  325. }
  326. /* blk still in free list, with updated start and/or size */
  327. if (bs == s || be == e) {
  328. if (bs == s)
  329. blk->start += size;
  330. blk->size -= size;
  331. } else {
  332. /* The front free fragment */
  333. blk->size = s - bs;
  334. /* the back free fragment */
  335. newblk = get_slot(info);
  336. newblk->start = e;
  337. newblk->size = be - e;
  338. list_add(&newblk->list, &blk->list);
  339. }
  340. return s;
  341. }
  342. /* Allocate a block of memory at the specified alignment. The value returned
  343. * is an offset into the buffer initialized by rh_init(), or a negative number
  344. * if there is an error.
  345. */
  346. unsigned long rh_alloc_align(rh_info_t * info, int size, int alignment, const char *owner)
  347. {
  348. struct list_head *l;
  349. rh_block_t *blk;
  350. rh_block_t *newblk;
  351. unsigned long start, sp_size;
  352. /* Validate size, and alignment must be power of two */
  353. if (size <= 0 || (alignment & (alignment - 1)) != 0)
  354. return (unsigned long) -EINVAL;
  355. /* Align to configured alignment */
  356. size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
  357. if (assure_empty(info, 2) < 0)
  358. return (unsigned long) -ENOMEM;
  359. blk = NULL;
  360. list_for_each(l, &info->free_list) {
  361. blk = list_entry(l, rh_block_t, list);
  362. if (size <= blk->size) {
  363. start = (blk->start + alignment - 1) & ~(alignment - 1);
  364. if (start + size <= blk->start + blk->size)
  365. break;
  366. }
  367. blk = NULL;
  368. }
  369. if (blk == NULL)
  370. return (unsigned long) -ENOMEM;
  371. /* Just fits */
  372. if (blk->size == size) {
  373. /* Move from free list to taken list */
  374. list_del(&blk->list);
  375. newblk = blk;
  376. } else {
  377. /* Fragment caused, split if needed */
  378. /* Create block for fragment in the beginning */
  379. sp_size = start - blk->start;
  380. if (sp_size) {
  381. rh_block_t *spblk;
  382. spblk = get_slot(info);
  383. spblk->start = blk->start;
  384. spblk->size = sp_size;
  385. /* add before the blk */
  386. list_add(&spblk->list, blk->list.prev);
  387. }
  388. newblk = get_slot(info);
  389. newblk->start = start;
  390. newblk->size = size;
  391. /* blk still in free list, with updated start and size
  392. * for fragment in the end */
  393. blk->start = start + size;
  394. blk->size -= sp_size + size;
  395. /* No fragment in the end, remove blk */
  396. if (blk->size == 0) {
  397. list_del(&blk->list);
  398. release_slot(info, blk);
  399. }
  400. }
  401. newblk->owner = owner;
  402. attach_taken_block(info, newblk);
  403. return start;
  404. }
  405. /* Allocate a block of memory at the default alignment. The value returned is
  406. * an offset into the buffer initialized by rh_init(), or a negative number if
  407. * there is an error.
  408. */
  409. unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
  410. {
  411. return rh_alloc_align(info, size, info->alignment, owner);
  412. }
  413. /* Allocate a block of memory at the given offset, rounded up to the default
  414. * alignment. The value returned is an offset into the buffer initialized by
  415. * rh_init(), or a negative number if there is an error.
  416. */
  417. unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
  418. {
  419. struct list_head *l;
  420. rh_block_t *blk, *newblk1, *newblk2;
  421. unsigned long s, e, m, bs = 0, be = 0;
  422. /* Validate size */
  423. if (size <= 0)
  424. return (unsigned long) -EINVAL;
  425. /* The region must be aligned */
  426. s = start;
  427. e = s + size;
  428. m = info->alignment - 1;
  429. /* Round start up */
  430. s = (s + m) & ~m;
  431. /* Round end down */
  432. e = e & ~m;
  433. if (assure_empty(info, 2) < 0)
  434. return (unsigned long) -ENOMEM;
  435. blk = NULL;
  436. list_for_each(l, &info->free_list) {
  437. blk = list_entry(l, rh_block_t, list);
  438. /* The range must lie entirely inside one free block */
  439. bs = blk->start;
  440. be = blk->start + blk->size;
  441. if (s >= bs && e <= be)
  442. break;
  443. }
  444. if (blk == NULL)
  445. return (unsigned long) -ENOMEM;
  446. /* Perfect fit */
  447. if (bs == s && be == e) {
  448. /* Move from free list to taken list */
  449. list_del(&blk->list);
  450. blk->owner = owner;
  451. start = blk->start;
  452. attach_taken_block(info, blk);
  453. return start;
  454. }
  455. /* blk still in free list, with updated start and/or size */
  456. if (bs == s || be == e) {
  457. if (bs == s)
  458. blk->start += size;
  459. blk->size -= size;
  460. } else {
  461. /* The front free fragment */
  462. blk->size = s - bs;
  463. /* The back free fragment */
  464. newblk2 = get_slot(info);
  465. newblk2->start = e;
  466. newblk2->size = be - e;
  467. list_add(&newblk2->list, &blk->list);
  468. }
  469. newblk1 = get_slot(info);
  470. newblk1->start = s;
  471. newblk1->size = e - s;
  472. newblk1->owner = owner;
  473. start = newblk1->start;
  474. attach_taken_block(info, newblk1);
  475. return start;
  476. }
  477. /* Deallocate the memory previously allocated by one of the rh_alloc functions.
  478. * The return value is the size of the deallocated block, or a negative number
  479. * if there is an error.
  480. */
  481. int rh_free(rh_info_t * info, unsigned long start)
  482. {
  483. rh_block_t *blk, *blk2;
  484. struct list_head *l;
  485. int size;
  486. /* Linear search for block */
  487. blk = NULL;
  488. list_for_each(l, &info->taken_list) {
  489. blk2 = list_entry(l, rh_block_t, list);
  490. if (start < blk2->start)
  491. break;
  492. blk = blk2;
  493. }
  494. if (blk == NULL || start > (blk->start + blk->size))
  495. return -EINVAL;
  496. /* Remove from taken list */
  497. list_del(&blk->list);
  498. /* Get size of freed block */
  499. size = blk->size;
  500. attach_free_block(info, blk);
  501. return size;
  502. }
  503. int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
  504. {
  505. rh_block_t *blk;
  506. struct list_head *l;
  507. struct list_head *h;
  508. int nr;
  509. switch (what) {
  510. case RHGS_FREE:
  511. h = &info->free_list;
  512. break;
  513. case RHGS_TAKEN:
  514. h = &info->taken_list;
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. /* Linear search for block */
  520. nr = 0;
  521. list_for_each(l, h) {
  522. blk = list_entry(l, rh_block_t, list);
  523. if (stats != NULL && nr < max_stats) {
  524. stats->start = blk->start;
  525. stats->size = blk->size;
  526. stats->owner = blk->owner;
  527. stats++;
  528. }
  529. nr++;
  530. }
  531. return nr;
  532. }
  533. int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
  534. {
  535. rh_block_t *blk, *blk2;
  536. struct list_head *l;
  537. int size;
  538. /* Linear search for block */
  539. blk = NULL;
  540. list_for_each(l, &info->taken_list) {
  541. blk2 = list_entry(l, rh_block_t, list);
  542. if (start < blk2->start)
  543. break;
  544. blk = blk2;
  545. }
  546. if (blk == NULL || start > (blk->start + blk->size))
  547. return -EINVAL;
  548. blk->owner = owner;
  549. size = blk->size;
  550. return size;
  551. }
  552. void rh_dump(rh_info_t * info)
  553. {
  554. static rh_stats_t st[32]; /* XXX maximum 32 blocks */
  555. int maxnr;
  556. int i, nr;
  557. maxnr = ARRAY_SIZE(st);
  558. printk(KERN_INFO
  559. "info @0x%p (%d slots empty / %d max)\n",
  560. info, info->empty_slots, info->max_blocks);
  561. printk(KERN_INFO " Free:\n");
  562. nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
  563. if (nr > maxnr)
  564. nr = maxnr;
  565. for (i = 0; i < nr; i++)
  566. printk(KERN_INFO
  567. " 0x%lx-0x%lx (%u)\n",
  568. st[i].start, st[i].start + st[i].size,
  569. st[i].size);
  570. printk(KERN_INFO "\n");
  571. printk(KERN_INFO " Taken:\n");
  572. nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
  573. if (nr > maxnr)
  574. nr = maxnr;
  575. for (i = 0; i < nr; i++)
  576. printk(KERN_INFO
  577. " 0x%lx-0x%lx (%u) %s\n",
  578. st[i].start, st[i].start + st[i].size,
  579. st[i].size, st[i].owner != NULL ? st[i].owner : "");
  580. printk(KERN_INFO "\n");
  581. }
  582. void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
  583. {
  584. printk(KERN_INFO
  585. "blk @0x%p: 0x%lx-0x%lx (%u)\n",
  586. blk, blk->start, blk->start + blk->size, blk->size);
  587. }