rheap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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;
  351. /* Validate size, and alignment must be power of two */
  352. if (size <= 0 || (alignment & (alignment - 1)) != 0)
  353. return (unsigned long) -EINVAL;
  354. /* given alignment larger that default rheap alignment */
  355. if (alignment > info->alignment)
  356. size += alignment - 1;
  357. /* Align to configured alignment */
  358. size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
  359. if (assure_empty(info, 1) < 0)
  360. return (unsigned long) -ENOMEM;
  361. blk = NULL;
  362. list_for_each(l, &info->free_list) {
  363. blk = list_entry(l, rh_block_t, list);
  364. if (size <= blk->size)
  365. break;
  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. blk->owner = owner;
  375. start = blk->start;
  376. attach_taken_block(info, blk);
  377. return start;
  378. }
  379. newblk = get_slot(info);
  380. newblk->start = blk->start;
  381. newblk->size = size;
  382. newblk->owner = owner;
  383. /* blk still in free list, with updated start, size */
  384. blk->start += size;
  385. blk->size -= size;
  386. start = newblk->start;
  387. attach_taken_block(info, newblk);
  388. /* for larger alignment return fixed up pointer */
  389. /* this is no problem with the deallocator since */
  390. /* we scan for pointers that lie in the blocks */
  391. if (alignment > info->alignment)
  392. start = (start + alignment - 1) & ~(alignment - 1);
  393. return start;
  394. }
  395. /* Allocate a block of memory at the default alignment. The value returned is
  396. * an offset into the buffer initialized by rh_init(), or a negative number if
  397. * there is an error.
  398. */
  399. unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
  400. {
  401. return rh_alloc_align(info, size, info->alignment, owner);
  402. }
  403. /* Allocate a block of memory at the given offset, rounded up to the default
  404. * alignment. The value returned is an offset into the buffer initialized by
  405. * rh_init(), or a negative number if there is an error.
  406. */
  407. unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
  408. {
  409. struct list_head *l;
  410. rh_block_t *blk, *newblk1, *newblk2;
  411. unsigned long s, e, m, bs = 0, be = 0;
  412. /* Validate size */
  413. if (size <= 0)
  414. return (unsigned long) -EINVAL;
  415. /* The region must be aligned */
  416. s = start;
  417. e = s + size;
  418. m = info->alignment - 1;
  419. /* Round start up */
  420. s = (s + m) & ~m;
  421. /* Round end down */
  422. e = e & ~m;
  423. if (assure_empty(info, 2) < 0)
  424. return (unsigned long) -ENOMEM;
  425. blk = NULL;
  426. list_for_each(l, &info->free_list) {
  427. blk = list_entry(l, rh_block_t, list);
  428. /* The range must lie entirely inside one free block */
  429. bs = blk->start;
  430. be = blk->start + blk->size;
  431. if (s >= bs && e <= be)
  432. break;
  433. }
  434. if (blk == NULL)
  435. return (unsigned long) -ENOMEM;
  436. /* Perfect fit */
  437. if (bs == s && be == e) {
  438. /* Move from free list to taken list */
  439. list_del(&blk->list);
  440. blk->owner = owner;
  441. start = blk->start;
  442. attach_taken_block(info, blk);
  443. return start;
  444. }
  445. /* blk still in free list, with updated start and/or size */
  446. if (bs == s || be == e) {
  447. if (bs == s)
  448. blk->start += size;
  449. blk->size -= size;
  450. } else {
  451. /* The front free fragment */
  452. blk->size = s - bs;
  453. /* The back free fragment */
  454. newblk2 = get_slot(info);
  455. newblk2->start = e;
  456. newblk2->size = be - e;
  457. list_add(&newblk2->list, &blk->list);
  458. }
  459. newblk1 = get_slot(info);
  460. newblk1->start = s;
  461. newblk1->size = e - s;
  462. newblk1->owner = owner;
  463. start = newblk1->start;
  464. attach_taken_block(info, newblk1);
  465. return start;
  466. }
  467. /* Deallocate the memory previously allocated by one of the rh_alloc functions.
  468. * The return value is the size of the deallocated block, or a negative number
  469. * if there is an error.
  470. */
  471. int rh_free(rh_info_t * info, unsigned long start)
  472. {
  473. rh_block_t *blk, *blk2;
  474. struct list_head *l;
  475. int size;
  476. /* Linear search for block */
  477. blk = NULL;
  478. list_for_each(l, &info->taken_list) {
  479. blk2 = list_entry(l, rh_block_t, list);
  480. if (start < blk2->start)
  481. break;
  482. blk = blk2;
  483. }
  484. if (blk == NULL || start > (blk->start + blk->size))
  485. return -EINVAL;
  486. /* Remove from taken list */
  487. list_del(&blk->list);
  488. /* Get size of freed block */
  489. size = blk->size;
  490. attach_free_block(info, blk);
  491. return size;
  492. }
  493. int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
  494. {
  495. rh_block_t *blk;
  496. struct list_head *l;
  497. struct list_head *h;
  498. int nr;
  499. switch (what) {
  500. case RHGS_FREE:
  501. h = &info->free_list;
  502. break;
  503. case RHGS_TAKEN:
  504. h = &info->taken_list;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. /* Linear search for block */
  510. nr = 0;
  511. list_for_each(l, h) {
  512. blk = list_entry(l, rh_block_t, list);
  513. if (stats != NULL && nr < max_stats) {
  514. stats->start = blk->start;
  515. stats->size = blk->size;
  516. stats->owner = blk->owner;
  517. stats++;
  518. }
  519. nr++;
  520. }
  521. return nr;
  522. }
  523. int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
  524. {
  525. rh_block_t *blk, *blk2;
  526. struct list_head *l;
  527. int size;
  528. /* Linear search for block */
  529. blk = NULL;
  530. list_for_each(l, &info->taken_list) {
  531. blk2 = list_entry(l, rh_block_t, list);
  532. if (start < blk2->start)
  533. break;
  534. blk = blk2;
  535. }
  536. if (blk == NULL || start > (blk->start + blk->size))
  537. return -EINVAL;
  538. blk->owner = owner;
  539. size = blk->size;
  540. return size;
  541. }
  542. void rh_dump(rh_info_t * info)
  543. {
  544. static rh_stats_t st[32]; /* XXX maximum 32 blocks */
  545. int maxnr;
  546. int i, nr;
  547. maxnr = ARRAY_SIZE(st);
  548. printk(KERN_INFO
  549. "info @0x%p (%d slots empty / %d max)\n",
  550. info, info->empty_slots, info->max_blocks);
  551. printk(KERN_INFO " Free:\n");
  552. nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
  553. if (nr > maxnr)
  554. nr = maxnr;
  555. for (i = 0; i < nr; i++)
  556. printk(KERN_INFO
  557. " 0x%lx-0x%lx (%u)\n",
  558. st[i].start, st[i].start + st[i].size,
  559. st[i].size);
  560. printk(KERN_INFO "\n");
  561. printk(KERN_INFO " Taken:\n");
  562. nr = rh_get_stats(info, RHGS_TAKEN, 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) %s\n",
  568. st[i].start, st[i].start + st[i].size,
  569. st[i].size, st[i].owner != NULL ? st[i].owner : "");
  570. printk(KERN_INFO "\n");
  571. }
  572. void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
  573. {
  574. printk(KERN_INFO
  575. "blk @0x%p: 0x%lx-0x%lx (%u)\n",
  576. blk, blk->start, blk->start + blk->size, blk->size);
  577. }