cnss_prealloc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2012,2014-2017,2019-2021 The Linux Foundation. All rights reserved. */
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/mempool.h>
  6. #include <linux/mm.h>
  7. #include <linux/err.h>
  8. #include <linux/of.h>
  9. #include <net/cnss_prealloc.h>
  10. MODULE_LICENSE("GPL v2");
  11. MODULE_DESCRIPTION("CNSS prealloc driver");
  12. /* cnss preallocation scheme is a memory pool that always tries to keep a
  13. * list of free memory for use in emergencies. It is implemented on kernel
  14. * features: memorypool and kmem cache.
  15. */
  16. struct cnss_pool {
  17. size_t size;
  18. int min;
  19. const char name[50];
  20. mempool_t *mp;
  21. struct kmem_cache *cache;
  22. };
  23. /**
  24. * Memory pool
  25. * -----------
  26. *
  27. * How to update this table:
  28. *
  29. * 1. Add a new row with following elements
  30. * size : Size of one allocation unit in bytes.
  31. * min : Minimum units to be reserved. Used only if a regular
  32. * allocation fails.
  33. * name : Name of the cache/pool. Will be displayed in /proc/slabinfo
  34. * if not merged with another pool.
  35. * mp : A pointer to memory pool. Updated during init.
  36. * cache : A pointer to cache. Updated during init.
  37. * 2. Always keep the table in increasing order
  38. * 3. Please keep the reserve pool as minimum as possible as it's always
  39. * preallocated.
  40. * 4. Always profile with different use cases after updating this table.
  41. * 5. A dynamic view of this pool can be viewed at /proc/slabinfo.
  42. * 6. Each pool has a sys node at /sys/kernel/slab/<name>
  43. *
  44. */
  45. /* size, min pool reserve, name, memorypool handler, cache handler*/
  46. static struct cnss_pool cnss_pools[] = {
  47. {8 * 1024, 22, "cnss-pool-8k", NULL, NULL},
  48. {16 * 1024, 16, "cnss-pool-16k", NULL, NULL},
  49. {32 * 1024, 6, "cnss-pool-32k", NULL, NULL},
  50. {64 * 1024, 8, "cnss-pool-64k", NULL, NULL},
  51. {128 * 1024, 2, "cnss-pool-128k", NULL, NULL},
  52. };
  53. /**
  54. * cnss_pool_alloc_threshold() - Allocation threshold
  55. *
  56. * Minimum memory size to be part of cnss pool.
  57. *
  58. * Return: Size
  59. *
  60. */
  61. static inline size_t cnss_pool_alloc_threshold(void)
  62. {
  63. return cnss_pools[0].size;
  64. }
  65. /**
  66. * cnss_pool_int() - Initialize memory pools.
  67. *
  68. * Create cnss pools as configured by cnss_pools[]. It is the responsibility of
  69. * the caller to invoke cnss_pool_deinit() routine to clean it up. This
  70. * function needs to be called at early boot to preallocate minimum buffers in
  71. * the pool.
  72. *
  73. * Return: 0 - success, otherwise error code.
  74. *
  75. */
  76. static int cnss_pool_init(void)
  77. {
  78. int i;
  79. for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
  80. /* Create the slab cache */
  81. cnss_pools[i].cache =
  82. kmem_cache_create_usercopy(cnss_pools[i].name,
  83. cnss_pools[i].size, 0,
  84. SLAB_ACCOUNT, 0,
  85. cnss_pools[i].size, NULL);
  86. if (!cnss_pools[i].cache) {
  87. pr_err("cnss_prealloc: cache %s failed\n",
  88. cnss_pools[i].name);
  89. continue;
  90. }
  91. /* Create the pool and associate to slab cache */
  92. cnss_pools[i].mp =
  93. mempool_create(cnss_pools[i].min, mempool_alloc_slab,
  94. mempool_free_slab, cnss_pools[i].cache);
  95. if (!cnss_pools[i].mp) {
  96. pr_err("cnss_prealloc: mempool %s failed\n",
  97. cnss_pools[i].name);
  98. kmem_cache_destroy(cnss_pools[i].cache);
  99. cnss_pools[i].cache = NULL;
  100. continue;
  101. }
  102. pr_info("cnss_prealloc: created mempool %s of min size %d * %zu\n",
  103. cnss_pools[i].name, cnss_pools[i].min,
  104. cnss_pools[i].size);
  105. }
  106. return 0;
  107. }
  108. /**
  109. * cnss_pool_deinit() - Free memory pools.
  110. *
  111. * Free the memory pools and return resources back to the system. It warns
  112. * if there is any pending element in memory pool or cache.
  113. *
  114. */
  115. static void cnss_pool_deinit(void)
  116. {
  117. int i;
  118. for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
  119. pr_info("cnss_prealloc: destroy mempool %s\n",
  120. cnss_pools[i].name);
  121. mempool_destroy(cnss_pools[i].mp);
  122. kmem_cache_destroy(cnss_pools[i].cache);
  123. }
  124. }
  125. /**
  126. * cnss_pool_get_index() - Get the index of memory pool
  127. * @mem: Allocated memory
  128. *
  129. * Returns the index of the memory pool which fits the reqested memory. The
  130. * complexity of this check is O(num of memory pools). Returns a negative
  131. * value with error code in case of failure.
  132. *
  133. */
  134. static int cnss_pool_get_index(void *mem)
  135. {
  136. struct page *page;
  137. struct kmem_cache *cache;
  138. int i;
  139. if (!virt_addr_valid(mem))
  140. return -EINVAL;
  141. /* mem -> page -> cache */
  142. page = virt_to_head_page(mem);
  143. if (!page)
  144. return -ENOENT;
  145. cache = page->slab_cache;
  146. if (!cache)
  147. return -ENOENT;
  148. /* Check if memory belongs to a pool */
  149. for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
  150. if (cnss_pools[i].cache == cache)
  151. return i;
  152. }
  153. return -ENOENT;
  154. }
  155. /**
  156. * wcnss_prealloc_get() - Get preallocated memory from a pool
  157. * @size: Size to allocate
  158. *
  159. * Memory pool is chosen based on the size. If memory is not available in a
  160. * given pool it goes to next higher sized pool until it succeeds.
  161. *
  162. * Return: A void pointer to allocated memory
  163. */
  164. void *wcnss_prealloc_get(size_t size)
  165. {
  166. void *mem = NULL;
  167. gfp_t gfp_mask = __GFP_ZERO;
  168. int i;
  169. if (in_interrupt() || irqs_disabled())
  170. gfp_mask |= GFP_ATOMIC;
  171. else
  172. gfp_mask |= GFP_KERNEL;
  173. if (size >= cnss_pool_alloc_threshold()) {
  174. for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
  175. if (cnss_pools[i].size >= size) {
  176. mem = mempool_alloc(cnss_pools[i].mp, gfp_mask);
  177. if (mem)
  178. break;
  179. }
  180. }
  181. }
  182. if (!mem && size >= cnss_pool_alloc_threshold()) {
  183. pr_debug("cnss_prealloc: not available for size %zu, flag %x\n",
  184. size, gfp_mask);
  185. }
  186. return mem;
  187. }
  188. EXPORT_SYMBOL(wcnss_prealloc_get);
  189. /**
  190. * wcnss_prealloc_put() - Relase allocated memory
  191. * @mem: Allocated memory
  192. *
  193. * Free the memory got by wcnss_prealloc_get() to slab or pool reserve if memory
  194. * pool doesn't have enough elements.
  195. *
  196. * Return: 1 - success
  197. * 0 - fail
  198. */
  199. int wcnss_prealloc_put(void *mem)
  200. {
  201. int i;
  202. if (!mem)
  203. return 0;
  204. i = cnss_pool_get_index(mem);
  205. if (i >= 0 && i < ARRAY_SIZE(cnss_pools)) {
  206. mempool_free(mem, cnss_pools[i].mp);
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(wcnss_prealloc_put);
  212. /* Not implemented. Make use of Linux SLAB features. */
  213. void wcnss_prealloc_check_memory_leak(void) {}
  214. EXPORT_SYMBOL(wcnss_prealloc_check_memory_leak);
  215. /* Not implemented. Make use of Linux SLAB features. */
  216. int wcnss_pre_alloc_reset(void) { return -EOPNOTSUPP; }
  217. EXPORT_SYMBOL(wcnss_pre_alloc_reset);
  218. /**
  219. * cnss_prealloc_is_valid_dt_node_found - Check if valid device tree node
  220. * present
  221. *
  222. * Valid device tree node means a node with "qcom,wlan" property present
  223. * and "status" property not disabled.
  224. *
  225. * Return: true if valid device tree node found, false if not found
  226. */
  227. static bool cnss_prealloc_is_valid_dt_node_found(void)
  228. {
  229. struct device_node *dn = NULL;
  230. for_each_node_with_property(dn, "qcom,wlan") {
  231. if (of_device_is_available(dn))
  232. break;
  233. }
  234. if (dn)
  235. return true;
  236. return false;
  237. }
  238. static int __init cnss_prealloc_init(void)
  239. {
  240. if (!cnss_prealloc_is_valid_dt_node_found())
  241. return -ENODEV;
  242. return cnss_pool_init();
  243. }
  244. static void __exit cnss_prealloc_exit(void)
  245. {
  246. cnss_pool_deinit();
  247. }
  248. module_init(cnss_prealloc_init);
  249. module_exit(cnss_prealloc_exit);