ion_page_pool.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * drivers/gpu/ion/ion_mem_pool.c
  3. *
  4. * Copyright (C) 2011 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/debugfs.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/err.h>
  19. #include <linux/fs.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/swap.h>
  25. #include "ion_priv.h"
  26. struct ion_page_pool_item {
  27. struct page *page;
  28. struct list_head list;
  29. };
  30. static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
  31. {
  32. struct page *page;
  33. page = alloc_pages(pool->gfp_mask & ~__GFP_ZERO, pool->order);
  34. if (!page)
  35. return NULL;
  36. if (pool->gfp_mask & __GFP_ZERO)
  37. if (ion_heap_high_order_page_zero(page, pool->order))
  38. goto error_free_pages;
  39. return page;
  40. error_free_pages:
  41. __free_pages(page, pool->order);
  42. return NULL;
  43. }
  44. static void ion_page_pool_free_pages(struct ion_page_pool *pool,
  45. struct page *page)
  46. {
  47. __free_pages(page, pool->order);
  48. }
  49. static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
  50. {
  51. struct ion_page_pool_item *item;
  52. item = kmalloc(sizeof(struct ion_page_pool_item), GFP_KERNEL);
  53. if (!item)
  54. return -ENOMEM;
  55. mutex_lock(&pool->mutex);
  56. item->page = page;
  57. if (PageHighMem(page)) {
  58. list_add_tail(&item->list, &pool->high_items);
  59. pool->high_count++;
  60. } else {
  61. list_add_tail(&item->list, &pool->low_items);
  62. pool->low_count++;
  63. }
  64. mutex_unlock(&pool->mutex);
  65. return 0;
  66. }
  67. static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
  68. {
  69. struct ion_page_pool_item *item;
  70. struct page *page;
  71. if (high) {
  72. BUG_ON(!pool->high_count);
  73. item = list_first_entry(&pool->high_items,
  74. struct ion_page_pool_item, list);
  75. pool->high_count--;
  76. } else {
  77. BUG_ON(!pool->low_count);
  78. item = list_first_entry(&pool->low_items,
  79. struct ion_page_pool_item, list);
  80. pool->low_count--;
  81. }
  82. list_del(&item->list);
  83. page = item->page;
  84. kfree(item);
  85. return page;
  86. }
  87. void *ion_page_pool_alloc(struct ion_page_pool *pool, bool *from_pool)
  88. {
  89. struct page *page = NULL;
  90. BUG_ON(!pool);
  91. *from_pool = true;
  92. if (mutex_trylock(&pool->mutex)) {
  93. if (pool->high_count)
  94. page = ion_page_pool_remove(pool, true);
  95. else if (pool->low_count)
  96. page = ion_page_pool_remove(pool, false);
  97. mutex_unlock(&pool->mutex);
  98. }
  99. if (!page) {
  100. page = ion_page_pool_alloc_pages(pool);
  101. *from_pool = false;
  102. }
  103. return page;
  104. }
  105. void ion_page_pool_free(struct ion_page_pool *pool, struct page* page)
  106. {
  107. int ret;
  108. ret = ion_page_pool_add(pool, page);
  109. if (ret)
  110. ion_page_pool_free_pages(pool, page);
  111. }
  112. static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
  113. {
  114. int total = 0;
  115. total += high ? (pool->high_count + pool->low_count) *
  116. (1 << pool->order) :
  117. pool->low_count * (1 << pool->order);
  118. return total;
  119. }
  120. int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
  121. int nr_to_scan)
  122. {
  123. int nr_freed = 0;
  124. int i;
  125. bool high;
  126. high = gfp_mask & __GFP_HIGHMEM;
  127. if (current_is_kswapd())
  128. high = 1;
  129. if (nr_to_scan == 0)
  130. return ion_page_pool_total(pool, high);
  131. for (i = 0; i < nr_to_scan; i++) {
  132. struct page *page;
  133. mutex_lock(&pool->mutex);
  134. if (high && pool->high_count) {
  135. page = ion_page_pool_remove(pool, true);
  136. } else if (pool->low_count) {
  137. page = ion_page_pool_remove(pool, false);
  138. } else {
  139. mutex_unlock(&pool->mutex);
  140. break;
  141. }
  142. mutex_unlock(&pool->mutex);
  143. ion_page_pool_free_pages(pool, page);
  144. nr_freed += (1 << pool->order);
  145. }
  146. return nr_freed;
  147. }
  148. struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order)
  149. {
  150. struct ion_page_pool *pool = kmalloc(sizeof(struct ion_page_pool),
  151. GFP_KERNEL);
  152. if (!pool)
  153. return NULL;
  154. pool->high_count = 0;
  155. pool->low_count = 0;
  156. INIT_LIST_HEAD(&pool->low_items);
  157. INIT_LIST_HEAD(&pool->high_items);
  158. pool->gfp_mask = gfp_mask;
  159. pool->order = order;
  160. mutex_init(&pool->mutex);
  161. plist_node_init(&pool->list, order);
  162. return pool;
  163. }
  164. void ion_page_pool_destroy(struct ion_page_pool *pool)
  165. {
  166. kfree(pool);
  167. }
  168. static int __init ion_page_pool_init(void)
  169. {
  170. return 0;
  171. }
  172. static void __exit ion_page_pool_exit(void)
  173. {
  174. }
  175. module_init(ion_page_pool_init);
  176. module_exit(ion_page_pool_exit);