rheap.c 15 KB

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