xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/tests/kutf/kutf_mem.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2014, 2017, 2020-2021 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 /* Kernel UTF memory management functions */
23 
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
27 
28 #include <kutf/kutf_mem.h>
29 
30 
31 /**
32  * struct kutf_alloc_entry - Structure representing an allocation.
33  * @node:	List node for use with kutf_mempool.
34  * @data:	Data area of the allocation
35  */
36 struct kutf_alloc_entry {
37 	struct list_head node;
38 	u8 data[0];
39 };
40 
kutf_mempool_init(struct kutf_mempool * pool)41 int kutf_mempool_init(struct kutf_mempool *pool)
42 {
43 	if (!pool) {
44 		pr_err("NULL pointer passed to %s\n", __func__);
45 		return -1;
46 	}
47 
48 	INIT_LIST_HEAD(&pool->head);
49 	mutex_init(&pool->lock);
50 
51 	return 0;
52 }
53 EXPORT_SYMBOL(kutf_mempool_init);
54 
kutf_mempool_destroy(struct kutf_mempool * pool)55 void kutf_mempool_destroy(struct kutf_mempool *pool)
56 {
57 	struct list_head *remove;
58 	struct list_head *tmp;
59 
60 	if (!pool) {
61 		pr_err("NULL pointer passed to %s\n", __func__);
62 		return;
63 	}
64 
65 	mutex_lock(&pool->lock);
66 	list_for_each_safe(remove, tmp, &pool->head) {
67 		struct kutf_alloc_entry *remove_alloc;
68 
69 		remove_alloc = list_entry(remove, struct kutf_alloc_entry, node);
70 		list_del(&remove_alloc->node);
71 		kfree(remove_alloc);
72 	}
73 	mutex_unlock(&pool->lock);
74 
75 }
76 EXPORT_SYMBOL(kutf_mempool_destroy);
77 
kutf_mempool_alloc(struct kutf_mempool * pool,size_t size)78 void *kutf_mempool_alloc(struct kutf_mempool *pool, size_t size)
79 {
80 	struct kutf_alloc_entry *ret;
81 
82 	if (!pool) {
83 		pr_err("NULL pointer passed to %s\n", __func__);
84 		goto fail_pool;
85 	}
86 
87 	mutex_lock(&pool->lock);
88 
89 	ret = kmalloc(sizeof(*ret) + size, GFP_KERNEL);
90 	if (!ret) {
91 		pr_err("Failed to allocate memory\n");
92 		goto fail_alloc;
93 	}
94 
95 	INIT_LIST_HEAD(&ret->node);
96 	list_add(&ret->node, &pool->head);
97 
98 	mutex_unlock(&pool->lock);
99 
100 	return &ret->data[0];
101 
102 fail_alloc:
103 	mutex_unlock(&pool->lock);
104 fail_pool:
105 	return NULL;
106 }
107 EXPORT_SYMBOL(kutf_mempool_alloc);
108