rheap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 = 0;
  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 = 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 = 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 && s != (before->start + before->size))
  152. before = NULL;
  153. if (after && e != 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 -= 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, unsigned long 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 = 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. if (IS_ERR_VALUE(e) || (e < s))
  271. return -ERANGE;
  272. /* Take final values */
  273. start = s;
  274. size = e - s;
  275. /* Grow the blocks, if needed */
  276. r = assure_empty(info, 1);
  277. if (r < 0)
  278. return r;
  279. blk = get_slot(info);
  280. blk->start = start;
  281. blk->size = size;
  282. blk->owner = NULL;
  283. attach_free_block(info, blk);
  284. return 0;
  285. }
  286. /* Detatch given address range, splits free block if needed. */
  287. unsigned long rh_detach_region(rh_info_t * info, unsigned long start, int size)
  288. {
  289. struct list_head *l;
  290. rh_block_t *blk, *newblk;
  291. unsigned long s, e, m, bs, be;
  292. /* Validate size */
  293. if (size <= 0)
  294. return (unsigned long) -EINVAL;
  295. /* The region must be aligned */
  296. s = start;
  297. e = s + size;
  298. m = info->alignment - 1;
  299. /* Round start up */
  300. s = (s + m) & ~m;
  301. /* Round end down */
  302. e = e & ~m;
  303. if (assure_empty(info, 1) < 0)
  304. return (unsigned long) -ENOMEM;
  305. blk = NULL;
  306. list_for_each(l, &info->free_list) {
  307. blk = list_entry(l, rh_block_t, list);
  308. /* The range must lie entirely inside one free block */
  309. bs = blk->start;
  310. be = blk->start + blk->size;
  311. if (s >= bs && e <= be)
  312. break;
  313. blk = NULL;
  314. }
  315. if (blk == NULL)
  316. return (unsigned long) -ENOMEM;
  317. /* Perfect fit */
  318. if (bs == s && be == e) {
  319. /* Delete from free list, release slot */
  320. list_del(&blk->list);
  321. release_slot(info, blk);
  322. return s;
  323. }
  324. /* blk still in free list, with updated start and/or size */
  325. if (bs == s || be == e) {
  326. if (bs == s)
  327. blk->start += size;
  328. blk->size -= size;
  329. } else {
  330. /* The front free fragment */
  331. blk->size = s - bs;
  332. /* the back free fragment */
  333. newblk = get_slot(info);
  334. newblk->start = e;
  335. newblk->size = be - e;
  336. list_add(&newblk->list, &blk->list);
  337. }
  338. return s;
  339. }
  340. unsigned long rh_alloc(rh_info_t * info, int size, const char *owner)
  341. {
  342. struct list_head *l;
  343. rh_block_t *blk;
  344. rh_block_t *newblk;
  345. unsigned long start;
  346. /* Validate size */
  347. if (size <= 0)
  348. return (unsigned long) -EINVAL;
  349. /* Align to configured alignment */
  350. size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
  351. if (assure_empty(info, 1) < 0)
  352. return (unsigned long) -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 (unsigned long) -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 += size;
  377. blk->size -= size;
  378. start = newblk->start;
  379. attach_taken_block(info, newblk);
  380. return start;
  381. }
  382. /* allocate at precisely the given address */
  383. unsigned long rh_alloc_fixed(rh_info_t * info, unsigned long start, int size, const char *owner)
  384. {
  385. struct list_head *l;
  386. rh_block_t *blk, *newblk1, *newblk2;
  387. unsigned long s, e, m, bs=0, be=0;
  388. /* Validate size */
  389. if (size <= 0)
  390. return (unsigned long) -EINVAL;
  391. /* The region must be aligned */
  392. s = start;
  393. e = s + size;
  394. m = info->alignment - 1;
  395. /* Round start up */
  396. s = (s + m) & ~m;
  397. /* Round end down */
  398. e = e & ~m;
  399. if (assure_empty(info, 2) < 0)
  400. return (unsigned long) -ENOMEM;
  401. blk = NULL;
  402. list_for_each(l, &info->free_list) {
  403. blk = list_entry(l, rh_block_t, list);
  404. /* The range must lie entirely inside one free block */
  405. bs = blk->start;
  406. be = blk->start + blk->size;
  407. if (s >= bs && e <= be)
  408. break;
  409. }
  410. if (blk == NULL)
  411. return (unsigned long) -ENOMEM;
  412. /* Perfect fit */
  413. if (bs == s && be == e) {
  414. /* Move from free list to taken list */
  415. list_del(&blk->list);
  416. blk->owner = owner;
  417. start = blk->start;
  418. attach_taken_block(info, blk);
  419. return start;
  420. }
  421. /* blk still in free list, with updated start and/or size */
  422. if (bs == s || be == e) {
  423. if (bs == s)
  424. blk->start += size;
  425. blk->size -= size;
  426. } else {
  427. /* The front free fragment */
  428. blk->size = s - bs;
  429. /* The back free fragment */
  430. newblk2 = get_slot(info);
  431. newblk2->start = e;
  432. newblk2->size = be - e;
  433. list_add(&newblk2->list, &blk->list);
  434. }
  435. newblk1 = get_slot(info);
  436. newblk1->start = s;
  437. newblk1->size = e - s;
  438. newblk1->owner = owner;
  439. start = newblk1->start;
  440. attach_taken_block(info, newblk1);
  441. return start;
  442. }
  443. int rh_free(rh_info_t * info, unsigned long start)
  444. {
  445. rh_block_t *blk, *blk2;
  446. struct list_head *l;
  447. int size;
  448. /* Linear search for block */
  449. blk = NULL;
  450. list_for_each(l, &info->taken_list) {
  451. blk2 = list_entry(l, rh_block_t, list);
  452. if (start < blk2->start)
  453. break;
  454. blk = blk2;
  455. }
  456. if (blk == NULL || start > (blk->start + blk->size))
  457. return -EINVAL;
  458. /* Remove from taken list */
  459. list_del(&blk->list);
  460. /* Get size of freed block */
  461. size = blk->size;
  462. attach_free_block(info, blk);
  463. return size;
  464. }
  465. int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
  466. {
  467. rh_block_t *blk;
  468. struct list_head *l;
  469. struct list_head *h;
  470. int nr;
  471. switch (what) {
  472. case RHGS_FREE:
  473. h = &info->free_list;
  474. break;
  475. case RHGS_TAKEN:
  476. h = &info->taken_list;
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. /* Linear search for block */
  482. nr = 0;
  483. list_for_each(l, h) {
  484. blk = list_entry(l, rh_block_t, list);
  485. if (stats != NULL && nr < max_stats) {
  486. stats->start = blk->start;
  487. stats->size = blk->size;
  488. stats->owner = blk->owner;
  489. stats++;
  490. }
  491. nr++;
  492. }
  493. return nr;
  494. }
  495. int rh_set_owner(rh_info_t * info, unsigned long start, const char *owner)
  496. {
  497. rh_block_t *blk, *blk2;
  498. struct list_head *l;
  499. int size;
  500. /* Linear search for block */
  501. blk = NULL;
  502. list_for_each(l, &info->taken_list) {
  503. blk2 = list_entry(l, rh_block_t, list);
  504. if (start < blk2->start)
  505. break;
  506. blk = blk2;
  507. }
  508. if (blk == NULL || start > (blk->start + blk->size))
  509. return -EINVAL;
  510. blk->owner = owner;
  511. size = blk->size;
  512. return size;
  513. }
  514. void rh_dump(rh_info_t * info)
  515. {
  516. static rh_stats_t st[32]; /* XXX maximum 32 blocks */
  517. int maxnr;
  518. int i, nr;
  519. maxnr = ARRAY_SIZE(st);
  520. printk(KERN_INFO
  521. "info @0x%p (%d slots empty / %d max)\n",
  522. info, info->empty_slots, info->max_blocks);
  523. printk(KERN_INFO " Free:\n");
  524. nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
  525. if (nr > maxnr)
  526. nr = maxnr;
  527. for (i = 0; i < nr; i++)
  528. printk(KERN_INFO
  529. " 0x%lx-0x%lx (%u)\n",
  530. st[i].start, st[i].start + st[i].size,
  531. st[i].size);
  532. printk(KERN_INFO "\n");
  533. printk(KERN_INFO " Taken:\n");
  534. nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
  535. if (nr > maxnr)
  536. nr = maxnr;
  537. for (i = 0; i < nr; i++)
  538. printk(KERN_INFO
  539. " 0x%lx-0x%lx (%u) %s\n",
  540. st[i].start, st[i].start + st[i].size,
  541. st[i].size, st[i].owner != NULL ? st[i].owner : "");
  542. printk(KERN_INFO "\n");
  543. }
  544. void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
  545. {
  546. printk(KERN_INFO
  547. "blk @0x%p: 0x%lx-0x%lx (%u)\n",
  548. blk, blk->start, blk->start + blk->size, blk->size);
  549. }