1 /*
2  * Copyright (c) 2016 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/kernel_structs.h>
9 
10 #include <zephyr/toolchain.h>
11 #include <zephyr/linker/sections.h>
12 #include <zephyr/sys/dlist.h>
13 #include <zephyr/init.h>
14 #include <zephyr/sys/check.h>
15 #include <zephyr/sys/iterable_sections.h>
16 #include <string.h>
17 /* private kernel APIs */
18 #include <ksched.h>
19 #include <wait_q.h>
20 
21 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
22 static struct k_obj_type obj_type_mem_slab;
23 
24 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
25 
k_mem_slab_stats_raw(struct k_obj_core * obj_core,void * stats)26 static int k_mem_slab_stats_raw(struct k_obj_core *obj_core, void *stats)
27 {
28 	__ASSERT((obj_core != NULL) && (stats != NULL), "NULL parameter");
29 
30 	struct k_mem_slab *slab;
31 	k_spinlock_key_t   key;
32 
33 	slab = CONTAINER_OF(obj_core, struct k_mem_slab, obj_core);
34 	key = k_spin_lock(&slab->lock);
35 	memcpy(stats, &slab->info, sizeof(slab->info));
36 	k_spin_unlock(&slab->lock, key);
37 
38 	return 0;
39 }
40 
k_mem_slab_stats_query(struct k_obj_core * obj_core,void * stats)41 static int k_mem_slab_stats_query(struct k_obj_core *obj_core, void *stats)
42 {
43 	__ASSERT((obj_core != NULL) && (stats != NULL), "NULL parameter");
44 
45 	struct k_mem_slab *slab;
46 	k_spinlock_key_t   key;
47 	struct sys_memory_stats *ptr = stats;
48 
49 	slab = CONTAINER_OF(obj_core, struct k_mem_slab, obj_core);
50 	key = k_spin_lock(&slab->lock);
51 	ptr->free_bytes = (slab->info.num_blocks - slab->info.num_used) *
52 			  slab->info.block_size;
53 	ptr->allocated_bytes = slab->info.num_used * slab->info.block_size;
54 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
55 	ptr->max_allocated_bytes = slab->info.max_used * slab->info.block_size;
56 #else
57 	ptr->max_allocated_bytes = 0;
58 #endif
59 	k_spin_unlock(&slab->lock, key);
60 
61 	return 0;
62 }
63 
k_mem_slab_stats_reset(struct k_obj_core * obj_core)64 static int k_mem_slab_stats_reset(struct k_obj_core *obj_core)
65 {
66 	__ASSERT(obj_core != NULL, "NULL parameter");
67 
68 	struct k_mem_slab *slab;
69 	k_spinlock_key_t   key;
70 
71 	slab = CONTAINER_OF(obj_core, struct k_mem_slab, obj_core);
72 	key = k_spin_lock(&slab->lock);
73 
74 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
75 	slab->info.max_used = slab->info.num_used;
76 #endif
77 
78 	k_spin_unlock(&slab->lock, key);
79 
80 	return 0;
81 }
82 
83 static struct k_obj_core_stats_desc mem_slab_stats_desc = {
84 	.raw_size = sizeof(struct k_mem_slab_info),
85 	.query_size = sizeof(struct sys_memory_stats),
86 	.raw   = k_mem_slab_stats_raw,
87 	.query = k_mem_slab_stats_query,
88 	.reset = k_mem_slab_stats_reset,
89 	.disable = NULL,
90 	.enable = NULL,
91 };
92 #endif
93 #endif
94 
95 /**
96  * @brief Initialize kernel memory slab subsystem.
97  *
98  * Perform any initialization of memory slabs that wasn't done at build time.
99  * Currently this just involves creating the list of free blocks for each slab.
100  *
101  * @retval 0 on success.
102  * @retval -EINVAL if @p slab contains invalid configuration and/or values.
103  */
create_free_list(struct k_mem_slab * slab)104 static int create_free_list(struct k_mem_slab *slab)
105 {
106 	uint32_t j;
107 	char *p;
108 
109 	/* blocks must be word aligned */
110 	CHECKIF(((slab->info.block_size | (uintptr_t)slab->buffer) &
111 				(sizeof(void *) - 1)) != 0U) {
112 		return -EINVAL;
113 	}
114 
115 	slab->free_list = NULL;
116 	p = slab->buffer;
117 
118 	for (j = 0U; j < slab->info.num_blocks; j++) {
119 		*(char **)p = slab->free_list;
120 		slab->free_list = p;
121 		p += slab->info.block_size;
122 	}
123 	return 0;
124 }
125 
126 /**
127  * @brief Complete initialization of statically defined memory slabs.
128  *
129  * Perform any initialization that wasn't done at build time.
130  *
131  * @return 0 on success, fails otherwise.
132  */
init_mem_slab_obj_core_list(void)133 static int init_mem_slab_obj_core_list(void)
134 {
135 	int rc = 0;
136 
137 	/* Initialize mem_slab object type */
138 
139 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
140 	z_obj_type_init(&obj_type_mem_slab, K_OBJ_TYPE_MEM_SLAB_ID,
141 			offsetof(struct k_mem_slab, obj_core));
142 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
143 	k_obj_type_stats_init(&obj_type_mem_slab, &mem_slab_stats_desc);
144 #endif
145 #endif
146 
147 	/* Initialize statically defined mem_slabs */
148 
149 	STRUCT_SECTION_FOREACH(k_mem_slab, slab) {
150 		rc = create_free_list(slab);
151 		if (rc < 0) {
152 			goto out;
153 		}
154 		k_object_init(slab);
155 
156 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
157 		k_obj_core_init_and_link(K_OBJ_CORE(slab), &obj_type_mem_slab);
158 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
159 		k_obj_core_stats_register(K_OBJ_CORE(slab), &slab->info,
160 					  sizeof(struct k_mem_slab_info));
161 #endif
162 #endif
163 	}
164 
165 out:
166 	return rc;
167 }
168 
169 SYS_INIT(init_mem_slab_obj_core_list, PRE_KERNEL_1,
170 	 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
171 
k_mem_slab_init(struct k_mem_slab * slab,void * buffer,size_t block_size,uint32_t num_blocks)172 int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
173 		    size_t block_size, uint32_t num_blocks)
174 {
175 	int rc = 0;
176 
177 	slab->info.num_blocks = num_blocks;
178 	slab->info.block_size = block_size;
179 	slab->buffer = buffer;
180 	slab->info.num_used = 0U;
181 	slab->lock = (struct k_spinlock) {};
182 
183 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
184 	slab->info.max_used = 0U;
185 #endif
186 
187 	rc = create_free_list(slab);
188 	if (rc < 0) {
189 		goto out;
190 	}
191 
192 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
193 	k_obj_core_init_and_link(K_OBJ_CORE(slab), &obj_type_mem_slab);
194 #endif
195 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
196 	k_obj_core_stats_register(K_OBJ_CORE(slab), &slab->info,
197 				  sizeof(struct k_mem_slab_info));
198 #endif
199 
200 	z_waitq_init(&slab->wait_q);
201 	k_object_init(slab);
202 out:
203 	SYS_PORT_TRACING_OBJ_INIT(k_mem_slab, slab, rc);
204 
205 	return rc;
206 }
207 
k_mem_slab_alloc(struct k_mem_slab * slab,void ** mem,k_timeout_t timeout)208 int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
209 {
210 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
211 	int result;
212 
213 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, alloc, slab, timeout);
214 
215 	if (slab->free_list != NULL) {
216 		/* take a free block */
217 		*mem = slab->free_list;
218 		slab->free_list = *(char **)(slab->free_list);
219 		slab->info.num_used++;
220 
221 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
222 		slab->info.max_used = MAX(slab->info.num_used,
223 					  slab->info.max_used);
224 #endif
225 
226 		result = 0;
227 	} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT) ||
228 		   !IS_ENABLED(CONFIG_MULTITHREADING)) {
229 		/* don't wait for a free block to become available */
230 		*mem = NULL;
231 		result = -ENOMEM;
232 	} else {
233 		SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mem_slab, alloc, slab, timeout);
234 
235 		/* wait for a free block or timeout */
236 		result = z_pend_curr(&slab->lock, key, &slab->wait_q, timeout);
237 		if (result == 0) {
238 			*mem = _current->base.swap_data;
239 		}
240 
241 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
242 
243 		return result;
244 	}
245 
246 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
247 
248 	k_spin_unlock(&slab->lock, key);
249 
250 	return result;
251 }
252 
k_mem_slab_free(struct k_mem_slab * slab,void * mem)253 void k_mem_slab_free(struct k_mem_slab *slab, void *mem)
254 {
255 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
256 
257 	__ASSERT(((char *)mem >= slab->buffer) &&
258 		 ((((char *)mem - slab->buffer) % slab->info.block_size) == 0) &&
259 		 ((char *)mem <= (slab->buffer + (slab->info.block_size *
260 						  (slab->info.num_blocks - 1)))),
261 		 "Invalid memory pointer provided");
262 
263 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, free, slab);
264 	if (slab->free_list == NULL && IS_ENABLED(CONFIG_MULTITHREADING)) {
265 		struct k_thread *pending_thread = z_unpend_first_thread(&slab->wait_q);
266 
267 		if (pending_thread != NULL) {
268 			SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
269 
270 			z_thread_return_value_set_with_data(pending_thread, 0, mem);
271 			z_ready_thread(pending_thread);
272 			z_reschedule(&slab->lock, key);
273 			return;
274 		}
275 	}
276 	*(char **) mem = slab->free_list;
277 	slab->free_list = (char *) mem;
278 	slab->info.num_used--;
279 
280 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
281 
282 	k_spin_unlock(&slab->lock, key);
283 }
284 
k_mem_slab_runtime_stats_get(struct k_mem_slab * slab,struct sys_memory_stats * stats)285 int k_mem_slab_runtime_stats_get(struct k_mem_slab *slab, struct sys_memory_stats *stats)
286 {
287 	if ((slab == NULL) || (stats == NULL)) {
288 		return -EINVAL;
289 	}
290 
291 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
292 
293 	stats->allocated_bytes = slab->info.num_used * slab->info.block_size;
294 	stats->free_bytes = (slab->info.num_blocks - slab->info.num_used) *
295 			    slab->info.block_size;
296 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
297 	stats->max_allocated_bytes = slab->info.max_used *
298 				     slab->info.block_size;
299 #else
300 	stats->max_allocated_bytes = 0;
301 #endif
302 
303 	k_spin_unlock(&slab->lock, key);
304 
305 	return 0;
306 }
307 
308 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
k_mem_slab_runtime_stats_reset_max(struct k_mem_slab * slab)309 int k_mem_slab_runtime_stats_reset_max(struct k_mem_slab *slab)
310 {
311 	if (slab == NULL) {
312 		return -EINVAL;
313 	}
314 
315 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
316 
317 	slab->info.max_used = slab->info.num_used;
318 
319 	k_spin_unlock(&slab->lock, key);
320 
321 	return 0;
322 }
323 #endif
324