rheap.c 16 KB

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