rheap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * arch/ppc/syslib/rheap.c
  3. *
  4. * A Remote Heap. Remote means that we don't touch the memory that the
  5. * heap points to. Normal heap implementations use the memory they manage
  6. * to place their list. We cannot do that because the memory we manage may
  7. * have special properties, for example it is uncachable or of different
  8. * endianess.
  9. *
  10. * Author: Pantelis Antoniou <panto@intracom.gr>
  11. *
  12. * 2004 (c) INTRACOM S.A. Greece. This file is licensed under
  13. * the terms of the GNU General Public License version 2. This program
  14. * is licensed "as is" without any warranty of any kind, whether express
  15. * or implied.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <asm/rheap.h>
  22. /*
  23. * Fixup a list_head, needed when copying lists. If the pointers fall
  24. * between s and e, apply the delta. This assumes that
  25. * sizeof(struct list_head *) == sizeof(unsigned long *).
  26. */
  27. static inline void fixup(unsigned long s, unsigned long e, int d,
  28. struct list_head *l)
  29. {
  30. unsigned long *pp;
  31. pp = (unsigned long *)&l->next;
  32. if (*pp >= s && *pp < e)
  33. *pp += d;
  34. pp = (unsigned long *)&l->prev;
  35. if (*pp >= s && *pp < e)
  36. *pp += d;
  37. }
  38. /* Grow the allocated blocks */
  39. static int grow(rh_info_t * info, int max_blocks)
  40. {
  41. rh_block_t *block, *blk;
  42. int i, new_blocks;
  43. int delta;
  44. unsigned long blks, blke;
  45. if (max_blocks <= info->max_blocks)
  46. return -EINVAL;
  47. new_blocks = max_blocks - info->max_blocks;
  48. block = kmalloc(sizeof(rh_block_t) * max_blocks, GFP_KERNEL);
  49. if (block == NULL)
  50. return -ENOMEM;
  51. if (info->max_blocks > 0) {
  52. /* copy old block area */
  53. memcpy(block, info->block,
  54. sizeof(rh_block_t) * info->max_blocks);
  55. delta = (char *)block - (char *)info->block;
  56. /* and fixup list pointers */
  57. blks = (unsigned long)info->block;
  58. blke = (unsigned long)(info->block + info->max_blocks);
  59. for (i = 0, blk = block; i < info->max_blocks; i++, blk++)
  60. fixup(blks, blke, delta, &blk->list);
  61. fixup(blks, blke, delta, &info->empty_list);
  62. fixup(blks, blke, delta, &info->free_list);
  63. fixup(blks, blke, delta, &info->taken_list);
  64. /* free the old allocated memory */
  65. if ((info->flags & RHIF_STATIC_BLOCK) == 0)
  66. kfree(info->block);
  67. }
  68. info->block = block;
  69. info->empty_slots += new_blocks;
  70. info->max_blocks = max_blocks;
  71. info->flags &= ~RHIF_STATIC_BLOCK;
  72. /* add all new blocks to the free list */
  73. for (i = 0, blk = block + info->max_blocks; 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 = NULL;
  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 = (unsigned long)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 = (unsigned long)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 != NULL && s != (unsigned long)before->start + before->size)
  153. before = NULL;
  154. if (after != NULL && e != (unsigned long)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 = (int8_t *)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, void *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 = (unsigned long)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. /* Take final values */
  272. start = (void *)s;
  273. size = (int)(e - s);
  274. /* Grow the blocks, if needed */
  275. r = assure_empty(info, 1);
  276. if (r < 0)
  277. return r;
  278. blk = get_slot(info);
  279. blk->start = start;
  280. blk->size = size;
  281. blk->owner = NULL;
  282. attach_free_block(info, blk);
  283. return 0;
  284. }
  285. /* Detatch given address range, splits free block if needed. */
  286. void *rh_detach_region(rh_info_t * info, void *start, int size)
  287. {
  288. struct list_head *l;
  289. rh_block_t *blk, *newblk;
  290. unsigned long s, e, m, bs, be;
  291. /* Validate size */
  292. if (size <= 0)
  293. return ERR_PTR(-EINVAL);
  294. /* The region must be aligned */
  295. s = (unsigned long)start;
  296. e = s + size;
  297. m = info->alignment - 1;
  298. /* Round start up */
  299. s = (s + m) & ~m;
  300. /* Round end down */
  301. e = e & ~m;
  302. if (assure_empty(info, 1) < 0)
  303. return ERR_PTR(-ENOMEM);
  304. blk = NULL;
  305. list_for_each(l, &info->free_list) {
  306. blk = list_entry(l, rh_block_t, list);
  307. /* The range must lie entirely inside one free block */
  308. bs = (unsigned long)blk->start;
  309. be = (unsigned long)blk->start + blk->size;
  310. if (s >= bs && e <= be)
  311. break;
  312. blk = NULL;
  313. }
  314. if (blk == NULL)
  315. return ERR_PTR(-ENOMEM);
  316. /* Perfect fit */
  317. if (bs == s && be == e) {
  318. /* Delete from free list, release slot */
  319. list_del(&blk->list);
  320. release_slot(info, blk);
  321. return (void *)s;
  322. }
  323. /* blk still in free list, with updated start and/or size */
  324. if (bs == s || be == e) {
  325. if (bs == s)
  326. blk->start = (int8_t *)blk->start + size;
  327. blk->size -= size;
  328. } else {
  329. /* The front free fragment */
  330. blk->size = s - bs;
  331. /* the back free fragment */
  332. newblk = get_slot(info);
  333. newblk->start = (void *)e;
  334. newblk->size = be - e;
  335. list_add(&newblk->list, &blk->list);
  336. }
  337. return (void *)s;
  338. }
  339. void *rh_alloc(rh_info_t * info, int size, const char *owner)
  340. {
  341. struct list_head *l;
  342. rh_block_t *blk;
  343. rh_block_t *newblk;
  344. void *start;
  345. /* Validate size */
  346. if (size <= 0)
  347. return ERR_PTR(-EINVAL);
  348. /* Align to configured alignment */
  349. size = (size + (info->alignment - 1)) & ~(info->alignment - 1);
  350. if (assure_empty(info, 1) < 0)
  351. return ERR_PTR(-ENOMEM);
  352. blk = NULL;
  353. list_for_each(l, &info->free_list) {
  354. blk = list_entry(l, rh_block_t, list);
  355. if (size <= blk->size)
  356. break;
  357. blk = NULL;
  358. }
  359. if (blk == NULL)
  360. return ERR_PTR(-ENOMEM);
  361. /* Just fits */
  362. if (blk->size == size) {
  363. /* Move from free list to taken list */
  364. list_del(&blk->list);
  365. blk->owner = owner;
  366. start = blk->start;
  367. attach_taken_block(info, blk);
  368. return start;
  369. }
  370. newblk = get_slot(info);
  371. newblk->start = blk->start;
  372. newblk->size = size;
  373. newblk->owner = owner;
  374. /* blk still in free list, with updated start, size */
  375. blk->start = (int8_t *)blk->start + size;
  376. blk->size -= size;
  377. start = newblk->start;
  378. attach_taken_block(info, newblk);
  379. return start;
  380. }
  381. /* allocate at precisely the given address */
  382. void *rh_alloc_fixed(rh_info_t * info, void *start, int size, const char *owner)
  383. {
  384. struct list_head *l;
  385. rh_block_t *blk, *newblk1, *newblk2;
  386. unsigned long s, e, m, bs, be;
  387. /* Validate size */
  388. if (size <= 0)
  389. return ERR_PTR(-EINVAL);
  390. /* The region must be aligned */
  391. s = (unsigned long)start;
  392. e = s + size;
  393. m = info->alignment - 1;
  394. /* Round start up */
  395. s = (s + m) & ~m;
  396. /* Round end down */
  397. e = e & ~m;
  398. if (assure_empty(info, 2) < 0)
  399. return ERR_PTR(-ENOMEM);
  400. blk = NULL;
  401. list_for_each(l, &info->free_list) {
  402. blk = list_entry(l, rh_block_t, list);
  403. /* The range must lie entirely inside one free block */
  404. bs = (unsigned long)blk->start;
  405. be = (unsigned long)blk->start + blk->size;
  406. if (s >= bs && e <= be)
  407. break;
  408. }
  409. if (blk == NULL)
  410. return ERR_PTR(-ENOMEM);
  411. /* Perfect fit */
  412. if (bs == s && be == e) {
  413. /* Move from free list to taken list */
  414. list_del(&blk->list);
  415. blk->owner = owner;
  416. start = blk->start;
  417. attach_taken_block(info, blk);
  418. return start;
  419. }
  420. /* blk still in free list, with updated start and/or size */
  421. if (bs == s || be == e) {
  422. if (bs == s)
  423. blk->start = (int8_t *)blk->start + size;
  424. blk->size -= size;
  425. } else {
  426. /* The front free fragment */
  427. blk->size = s - bs;
  428. /* The back free fragment */
  429. newblk2 = get_slot(info);
  430. newblk2->start = (void *)e;
  431. newblk2->size = be - e;
  432. list_add(&newblk2->list, &blk->list);
  433. }
  434. newblk1 = get_slot(info);
  435. newblk1->start = (void *)s;
  436. newblk1->size = e - s;
  437. newblk1->owner = owner;
  438. start = newblk1->start;
  439. attach_taken_block(info, newblk1);
  440. return start;
  441. }
  442. int rh_free(rh_info_t * info, void *start)
  443. {
  444. rh_block_t *blk, *blk2;
  445. struct list_head *l;
  446. int size;
  447. /* Linear search for block */
  448. blk = NULL;
  449. list_for_each(l, &info->taken_list) {
  450. blk2 = list_entry(l, rh_block_t, list);
  451. if (start < blk2->start)
  452. break;
  453. blk = blk2;
  454. }
  455. if (blk == NULL || start > (blk->start + blk->size))
  456. return -EINVAL;
  457. /* Remove from taken list */
  458. list_del(&blk->list);
  459. /* Get size of freed block */
  460. size = blk->size;
  461. attach_free_block(info, blk);
  462. return size;
  463. }
  464. int rh_get_stats(rh_info_t * info, int what, int max_stats, rh_stats_t * stats)
  465. {
  466. rh_block_t *blk;
  467. struct list_head *l;
  468. struct list_head *h;
  469. int nr;
  470. switch (what) {
  471. case RHGS_FREE:
  472. h = &info->free_list;
  473. break;
  474. case RHGS_TAKEN:
  475. h = &info->taken_list;
  476. break;
  477. default:
  478. return -EINVAL;
  479. }
  480. /* Linear search for block */
  481. nr = 0;
  482. list_for_each(l, h) {
  483. blk = list_entry(l, rh_block_t, list);
  484. if (stats != NULL && nr < max_stats) {
  485. stats->start = blk->start;
  486. stats->size = blk->size;
  487. stats->owner = blk->owner;
  488. stats++;
  489. }
  490. nr++;
  491. }
  492. return nr;
  493. }
  494. int rh_set_owner(rh_info_t * info, void *start, const char *owner)
  495. {
  496. rh_block_t *blk, *blk2;
  497. struct list_head *l;
  498. int size;
  499. /* Linear search for block */
  500. blk = NULL;
  501. list_for_each(l, &info->taken_list) {
  502. blk2 = list_entry(l, rh_block_t, list);
  503. if (start < blk2->start)
  504. break;
  505. blk = blk2;
  506. }
  507. if (blk == NULL || start > (blk->start + blk->size))
  508. return -EINVAL;
  509. blk->owner = owner;
  510. size = blk->size;
  511. return size;
  512. }
  513. void rh_dump(rh_info_t * info)
  514. {
  515. static rh_stats_t st[32]; /* XXX maximum 32 blocks */
  516. int maxnr;
  517. int i, nr;
  518. maxnr = sizeof(st) / sizeof(st[0]);
  519. printk(KERN_INFO
  520. "info @0x%p (%d slots empty / %d max)\n",
  521. info, info->empty_slots, info->max_blocks);
  522. printk(KERN_INFO " Free:\n");
  523. nr = rh_get_stats(info, RHGS_FREE, maxnr, st);
  524. if (nr > maxnr)
  525. nr = maxnr;
  526. for (i = 0; i < nr; i++)
  527. printk(KERN_INFO
  528. " 0x%p-0x%p (%u)\n",
  529. st[i].start, (int8_t *) st[i].start + st[i].size,
  530. st[i].size);
  531. printk(KERN_INFO "\n");
  532. printk(KERN_INFO " Taken:\n");
  533. nr = rh_get_stats(info, RHGS_TAKEN, maxnr, st);
  534. if (nr > maxnr)
  535. nr = maxnr;
  536. for (i = 0; i < nr; i++)
  537. printk(KERN_INFO
  538. " 0x%p-0x%p (%u) %s\n",
  539. st[i].start, (int8_t *) st[i].start + st[i].size,
  540. st[i].size, st[i].owner != NULL ? st[i].owner : "");
  541. printk(KERN_INFO "\n");
  542. }
  543. void rh_dump_blk(rh_info_t * info, rh_block_t * blk)
  544. {
  545. printk(KERN_INFO
  546. "blk @0x%p: 0x%p-0x%p (%u)\n",
  547. blk, blk->start, (int8_t *) blk->start + blk->size, blk->size);
  548. }