rheap.c 15 KB

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