rheap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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(rh_info_t * info, int size, 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 */
  344. if (size <= 0)
  345. return ERR_PTR(-EINVAL);
  346. /* Align to configured alignment */
  347. size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
  348. if (assure_empty(info, 1) < 0)
  349. return ERR_PTR(-ENOMEM);
  350. blk = NULL;
  351. list_for_each(l, &info->free_list) {
  352. blk = list_entry(l, rh_block_t, list);
  353. if (size <= blk->size)
  354. break;
  355. blk = NULL;
  356. }
  357. if (blk == NULL)
  358. return ERR_PTR(-ENOMEM);
  359. /* Just fits */
  360. if (blk->size == size) {
  361. /* Move from free list to taken list */
  362. list_del(&blk->list);
  363. blk->owner = owner;
  364. start = blk->start;
  365. attach_taken_block(info, blk);
  366. return start;
  367. }
  368. newblk = get_slot(info);
  369. newblk->start = blk->start;
  370. newblk->size = size;
  371. newblk->owner = owner;
  372. /* blk still in free list, with updated start, size */
  373. blk->start = (int8_t *)blk->start + size;
  374. blk->size -= size;
  375. start = newblk->start;
  376. attach_taken_block(info, newblk);
  377. return start;
  378. }
  379. /* allocate at precisely the given address */
  380. void *rh_alloc_fixed(rh_info_t * info, void *start, int size, const char *owner)
  381. {
  382. struct list_head *l;
  383. rh_block_t *blk, *newblk1, *newblk2;
  384. unsigned long s, e, m, bs, be;
  385. /* Validate size */
  386. if (size <= 0)
  387. return ERR_PTR(-EINVAL);
  388. /* The region must be aligned */
  389. s = (unsigned long)start;
  390. e = s + size;
  391. m = info->alignment - 1;
  392. /* Round start up */
  393. s = (s + m) & ~m;
  394. /* Round end down */
  395. e = e & ~m;
  396. if (assure_empty(info, 2) < 0)
  397. return ERR_PTR(-ENOMEM);
  398. blk = NULL;
  399. list_for_each(l, &info->free_list) {
  400. blk = list_entry(l, rh_block_t, list);
  401. /* The range must lie entirely inside one free block */
  402. bs = (unsigned long)blk->start;
  403. be = (unsigned long)blk->start + blk->size;
  404. if (s >= bs && e <= be)
  405. break;
  406. }
  407. if (blk == NULL)
  408. return ERR_PTR(-ENOMEM);
  409. /* Perfect fit */
  410. if (bs == s && be == e) {
  411. /* Move from free list to taken list */
  412. list_del(&blk->list);
  413. blk->owner = owner;
  414. start = blk->start;
  415. attach_taken_block(info, blk);
  416. return start;
  417. }
  418. /* blk still in free list, with updated start and/or size */
  419. if (bs == s || be == e) {
  420. if (bs == s)
  421. blk->start = (int8_t *)blk->start + size;
  422. blk->size -= size;
  423. } else {
  424. /* The front free fragment */
  425. blk->size = s - bs;
  426. /* The back free fragment */
  427. newblk2 = get_slot(info);
  428. newblk2->start = (void *)e;
  429. newblk2->size = be - e;
  430. list_add(&newblk2->list, &blk->list);
  431. }
  432. newblk1 = get_slot(info);
  433. newblk1->start = (void *)s;
  434. newblk1->size = e - s;
  435. newblk1->owner = owner;
  436. start = newblk1->start;
  437. attach_taken_block(info, newblk1);
  438. return start;
  439. }
  440. int rh_free(rh_info_t * info, void *start)
  441. {
  442. rh_block_t *blk, *blk2;
  443. struct list_head *l;
  444. int size;
  445. /* Linear search for block */
  446. blk = NULL;
  447. list_for_each(l, &info->taken_list) {
  448. blk2 = list_entry(l, rh_block_t, list);
  449. if (start < blk2->start)
  450. break;
  451. blk = blk2;
  452. }
  453. if (blk == NULL || start > (blk->start + blk->size))
  454. return -EINVAL;
  455. /* Remove from taken list */
  456. list_del(&blk->list);
  457. /* Get size of freed block */
  458. size = blk->size;
  459. attach_free_block(info, blk);
  460. return size;
  461. }
  462. int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
  463. {
  464. rh_block_t *blk;
  465. struct list_head *l;
  466. struct list_head *h;
  467. int nr;
  468. switch (what) {
  469. case RHGS_FREE:
  470. h = &info->free_list;
  471. break;
  472. case RHGS_TAKEN:
  473. h = &info->taken_list;
  474. break;
  475. default:
  476. return -EINVAL;
  477. }
  478. /* Linear search for block */
  479. nr = 0;
  480. list_for_each(l, h) {
  481. blk = list_entry(l, rh_block_t, list);
  482. if (stats != NULL && nr < max_stats) {
  483. stats->start = blk->start;
  484. stats->size = blk->size;
  485. stats->owner = blk->owner;
  486. stats++;
  487. }
  488. nr++;
  489. }
  490. return nr;
  491. }
  492. int rh_set_owner(rh_info_t * info, void *start, const char *owner)
  493. {
  494. rh_block_t *blk, *blk2;
  495. struct list_head *l;
  496. int size;
  497. /* Linear search for block */
  498. blk = NULL;
  499. list_for_each(l, &info->taken_list) {
  500. blk2 = list_entry(l, rh_block_t, list);
  501. if (start < blk2->start)
  502. break;
  503. blk = blk2;
  504. }
  505. if (blk == NULL || start > (blk->start + blk->size))
  506. return -EINVAL;
  507. blk->owner = owner;
  508. size = blk->size;
  509. return size;
  510. }
  511. void rh_dump(rh_info_t * info)
  512. {
  513. static rh_stats_t st[32]; /* XXX maximum 32 blocks */
  514. int maxnr;
  515. int i, nr;
  516. maxnr = sizeof(st) / sizeof(st[0]);
  517. printk(KERN_INFO
  518. "info @0x%p (%d slots empty / %d max)\n",
  519. info, info->empty_slots, info->max_blocks);
  520. printk(KERN_INFO " Free:\n");
  521. nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
  522. if (nr > maxnr)
  523. nr = maxnr;
  524. for (i = 0; i < nr; i++)
  525. printk(KERN_INFO
  526. " 0x%p-0x%p (%u)\n",
  527. st[i].start, (int8_t *) st[i].start + st[i].size,
  528. st[i].size);
  529. printk(KERN_INFO "\n");
  530. printk(KERN_INFO " Taken:\n");
  531. nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
  532. if (nr > maxnr)
  533. nr = maxnr;
  534. for (i = 0; i < nr; i++)
  535. printk(KERN_INFO
  536. " 0x%p-0x%p (%u) %s\n",
  537. st[i].start, (int8_t *) st[i].start + st[i].size,
  538. st[i].size, st[i].owner != NULL ? st[i].owner : "");
  539. printk(KERN_INFO "\n");
  540. }
  541. void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
  542. {
  543. printk(KERN_INFO
  544. "blk @0x%p: 0x%p-0x%p (%u)\n",
  545. blk, blk->start, (int8_t *) blk->start + blk->size, blk->size);
  546. }