123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/mempool.h>
- #include <linux/mm.h>
- #include <linux/err.h>
- #include <linux/of.h>
- #include <net/cnss_prealloc.h>
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("CNSS prealloc driver");
- struct cnss_pool {
- size_t size;
- int min;
- const char name[50];
- mempool_t *mp;
- struct kmem_cache *cache;
- };
- static struct cnss_pool cnss_pools[] = {
- {8 * 1024, 22, "cnss-pool-8k", NULL, NULL},
- {16 * 1024, 16, "cnss-pool-16k", NULL, NULL},
- {32 * 1024, 6, "cnss-pool-32k", NULL, NULL},
- {64 * 1024, 8, "cnss-pool-64k", NULL, NULL},
- {128 * 1024, 2, "cnss-pool-128k", NULL, NULL},
- };
- static inline size_t cnss_pool_alloc_threshold(void)
- {
- return cnss_pools[0].size;
- }
- static int cnss_pool_init(void)
- {
- int i;
- for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
-
- cnss_pools[i].cache =
- kmem_cache_create_usercopy(cnss_pools[i].name,
- cnss_pools[i].size, 0,
- SLAB_ACCOUNT, 0,
- cnss_pools[i].size, NULL);
- if (!cnss_pools[i].cache) {
- pr_err("cnss_prealloc: cache %s failed\n",
- cnss_pools[i].name);
- continue;
- }
-
- cnss_pools[i].mp =
- mempool_create(cnss_pools[i].min, mempool_alloc_slab,
- mempool_free_slab, cnss_pools[i].cache);
- if (!cnss_pools[i].mp) {
- pr_err("cnss_prealloc: mempool %s failed\n",
- cnss_pools[i].name);
- kmem_cache_destroy(cnss_pools[i].cache);
- cnss_pools[i].cache = NULL;
- continue;
- }
- pr_info("cnss_prealloc: created mempool %s of min size %d * %zu\n",
- cnss_pools[i].name, cnss_pools[i].min,
- cnss_pools[i].size);
- }
- return 0;
- }
- static void cnss_pool_deinit(void)
- {
- int i;
- for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
- pr_info("cnss_prealloc: destroy mempool %s\n",
- cnss_pools[i].name);
- mempool_destroy(cnss_pools[i].mp);
- kmem_cache_destroy(cnss_pools[i].cache);
- }
- }
- static int cnss_pool_get_index(void *mem)
- {
- struct page *page;
- struct kmem_cache *cache;
- int i;
- if (!virt_addr_valid(mem))
- return -EINVAL;
-
- page = virt_to_head_page(mem);
- if (!page)
- return -ENOENT;
- cache = page->slab_cache;
- if (!cache)
- return -ENOENT;
-
- for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
- if (cnss_pools[i].cache == cache)
- return i;
- }
- return -ENOENT;
- }
- void *wcnss_prealloc_get(size_t size)
- {
- void *mem = NULL;
- gfp_t gfp_mask = __GFP_ZERO;
- int i;
- if (in_interrupt() || irqs_disabled())
- gfp_mask |= GFP_ATOMIC;
- else
- gfp_mask |= GFP_KERNEL;
- if (size >= cnss_pool_alloc_threshold()) {
- for (i = 0; i < ARRAY_SIZE(cnss_pools); i++) {
- if (cnss_pools[i].size >= size) {
- mem = mempool_alloc(cnss_pools[i].mp, gfp_mask);
- if (mem)
- break;
- }
- }
- }
- if (!mem && size >= cnss_pool_alloc_threshold()) {
- pr_debug("cnss_prealloc: not available for size %zu, flag %x\n",
- size, gfp_mask);
- }
- return mem;
- }
- EXPORT_SYMBOL(wcnss_prealloc_get);
- int wcnss_prealloc_put(void *mem)
- {
- int i;
- if (!mem)
- return 0;
- i = cnss_pool_get_index(mem);
- if (i >= 0 && i < ARRAY_SIZE(cnss_pools)) {
- mempool_free(mem, cnss_pools[i].mp);
- return 1;
- }
- return 0;
- }
- EXPORT_SYMBOL(wcnss_prealloc_put);
- void wcnss_prealloc_check_memory_leak(void) {}
- EXPORT_SYMBOL(wcnss_prealloc_check_memory_leak);
- int wcnss_pre_alloc_reset(void) { return -EOPNOTSUPP; }
- EXPORT_SYMBOL(wcnss_pre_alloc_reset);
- static bool cnss_prealloc_is_valid_dt_node_found(void)
- {
- struct device_node *dn = NULL;
- for_each_node_with_property(dn, "qcom,wlan") {
- if (of_device_is_available(dn))
- break;
- }
- if (dn)
- return true;
- return false;
- }
- static int __init cnss_prealloc_init(void)
- {
- if (!cnss_prealloc_is_valid_dt_node_found())
- return -ENODEV;
- return cnss_pool_init();
- }
- static void __exit cnss_prealloc_exit(void)
- {
- cnss_pool_deinit();
- }
- module_init(cnss_prealloc_init);
- module_exit(cnss_prealloc_exit);
|