rheap.c 16 KB

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