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 /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
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 /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
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 /* CONFIG_OBJ_CORE_STATS_MEM_SLAB */
93 #endif /* CONFIG_OBJ_CORE_MEM_SLAB */
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 	char *p;
107 
108 	/* blocks must be word aligned */
109 	CHECKIF(((slab->info.block_size | (uintptr_t)slab->buffer) &
110 				(sizeof(void *) - 1)) != 0U) {
111 		return -EINVAL;
112 	}
113 
114 	slab->free_list = NULL;
115 	p = slab->buffer + slab->info.block_size * (slab->info.num_blocks - 1);
116 
117 	while (p >= slab->buffer) {
118 		*(char **)p = slab->free_list;
119 		slab->free_list = p;
120 		p -= slab->info.block_size;
121 	}
122 	return 0;
123 }
124 
125 /**
126  * @brief Complete initialization of statically defined memory slabs.
127  *
128  * Perform any initialization that wasn't done at build time.
129  *
130  * @return 0 on success, fails otherwise.
131  */
init_mem_slab_obj_core_list(void)132 static int init_mem_slab_obj_core_list(void)
133 {
134 	int rc = 0;
135 
136 	/* Initialize mem_slab object type */
137 
138 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
139 	z_obj_type_init(&obj_type_mem_slab, K_OBJ_TYPE_MEM_SLAB_ID,
140 			offsetof(struct k_mem_slab, obj_core));
141 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
142 	k_obj_type_stats_init(&obj_type_mem_slab, &mem_slab_stats_desc);
143 #endif /* CONFIG_OBJ_CORE_STATS_MEM_SLAB */
144 #endif /* CONFIG_OBJ_CORE_MEM_SLAB */
145 
146 	/* Initialize statically defined mem_slabs */
147 
148 	STRUCT_SECTION_FOREACH(k_mem_slab, slab) {
149 		rc = create_free_list(slab);
150 		if (rc < 0) {
151 			goto out;
152 		}
153 		k_object_init(slab);
154 
155 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
156 		k_obj_core_init_and_link(K_OBJ_CORE(slab), &obj_type_mem_slab);
157 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
158 		k_obj_core_stats_register(K_OBJ_CORE(slab), &slab->info,
159 					  sizeof(struct k_mem_slab_info));
160 #endif /* CONFIG_OBJ_CORE_STATS_MEM_SLAB */
161 #endif /* CONFIG_OBJ_CORE_MEM_SLAB */
162 	}
163 
164 out:
165 	return rc;
166 }
167 
168 SYS_INIT(init_mem_slab_obj_core_list, PRE_KERNEL_1,
169 	 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
170 
k_mem_slab_init(struct k_mem_slab * slab,void * buffer,size_t block_size,uint32_t num_blocks)171 int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
172 		    size_t block_size, uint32_t num_blocks)
173 {
174 	int rc;
175 
176 	slab->info.num_blocks = num_blocks;
177 	slab->info.block_size = block_size;
178 	slab->buffer = buffer;
179 	slab->info.num_used = 0U;
180 	slab->lock = (struct k_spinlock) {};
181 
182 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
183 	slab->info.max_used = 0U;
184 #endif /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
185 
186 	rc = create_free_list(slab);
187 	if (rc < 0) {
188 		goto out;
189 	}
190 
191 #ifdef CONFIG_OBJ_CORE_MEM_SLAB
192 	k_obj_core_init_and_link(K_OBJ_CORE(slab), &obj_type_mem_slab);
193 #endif /* CONFIG_OBJ_CORE_MEM_SLAB */
194 #ifdef CONFIG_OBJ_CORE_STATS_MEM_SLAB
195 	k_obj_core_stats_register(K_OBJ_CORE(slab), &slab->info,
196 				  sizeof(struct k_mem_slab_info));
197 #endif /* CONFIG_OBJ_CORE_STATS_MEM_SLAB */
198 
199 	z_waitq_init(&slab->wait_q);
200 	k_object_init(slab);
201 out:
202 	SYS_PORT_TRACING_OBJ_INIT(k_mem_slab, slab, rc);
203 
204 	return rc;
205 }
206 
slab_ptr_is_good(struct k_mem_slab * slab,const void * ptr)207 static bool slab_ptr_is_good(struct k_mem_slab *slab, const void *ptr)
208 {
209 	if (!IS_ENABLED(CONFIG_MEM_SLAB_POINTER_VALIDATE)) {
210 		return true;
211 	}
212 
213 	const char *p = ptr;
214 	ptrdiff_t offset = p - slab->buffer;
215 
216 	return (offset >= 0) &&
217 	       (offset < (slab->info.block_size * slab->info.num_blocks)) &&
218 	       ((offset % slab->info.block_size) == 0);
219 }
220 
k_mem_slab_alloc(struct k_mem_slab * slab,void ** mem,k_timeout_t timeout)221 int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
222 {
223 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
224 	int result;
225 
226 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, alloc, slab, timeout);
227 
228 	if (slab->free_list != NULL) {
229 		/* take a free block */
230 		*mem = slab->free_list;
231 		slab->free_list = *(char **)(slab->free_list);
232 		slab->info.num_used++;
233 		__ASSERT((slab->free_list == NULL &&
234 			  slab->info.num_used == slab->info.num_blocks) ||
235 			 slab_ptr_is_good(slab, slab->free_list),
236 			 "slab corruption detected");
237 
238 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
239 		slab->info.max_used = MAX(slab->info.num_used,
240 					  slab->info.max_used);
241 #endif /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
242 
243 		result = 0;
244 	} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT) ||
245 		   !IS_ENABLED(CONFIG_MULTITHREADING)) {
246 		/* don't wait for a free block to become available */
247 		*mem = NULL;
248 		result = -ENOMEM;
249 	} else {
250 		SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mem_slab, alloc, slab, timeout);
251 
252 		/* wait for a free block or timeout */
253 		result = z_pend_curr(&slab->lock, key, &slab->wait_q, timeout);
254 		if (result == 0) {
255 			*mem = arch_current_thread()->base.swap_data;
256 		}
257 
258 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
259 
260 		return result;
261 	}
262 
263 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
264 
265 	k_spin_unlock(&slab->lock, key);
266 
267 	return result;
268 }
269 
k_mem_slab_free(struct k_mem_slab * slab,void * mem)270 void k_mem_slab_free(struct k_mem_slab *slab, void *mem)
271 {
272 	if (!slab_ptr_is_good(slab, mem)) {
273 		__ASSERT(false, "Invalid memory pointer provided");
274 		k_panic();
275 		return;
276 	}
277 
278 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
279 
280 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, free, slab);
281 	if (unlikely(slab->free_list == NULL) && IS_ENABLED(CONFIG_MULTITHREADING)) {
282 		struct k_thread *pending_thread = z_unpend_first_thread(&slab->wait_q);
283 
284 		if (unlikely(pending_thread != NULL)) {
285 			SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
286 
287 			z_thread_return_value_set_with_data(pending_thread, 0, mem);
288 			z_ready_thread(pending_thread);
289 			z_reschedule(&slab->lock, key);
290 			return;
291 		}
292 	}
293 	*(char **) mem = slab->free_list;
294 	slab->free_list = (char *) mem;
295 	slab->info.num_used--;
296 
297 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
298 
299 	k_spin_unlock(&slab->lock, key);
300 }
301 
k_mem_slab_runtime_stats_get(struct k_mem_slab * slab,struct sys_memory_stats * stats)302 int k_mem_slab_runtime_stats_get(struct k_mem_slab *slab, struct sys_memory_stats *stats)
303 {
304 	if ((slab == NULL) || (stats == NULL)) {
305 		return -EINVAL;
306 	}
307 
308 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
309 
310 	stats->allocated_bytes = slab->info.num_used * slab->info.block_size;
311 	stats->free_bytes = (slab->info.num_blocks - slab->info.num_used) *
312 			    slab->info.block_size;
313 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
314 	stats->max_allocated_bytes = slab->info.max_used *
315 				     slab->info.block_size;
316 #else
317 	stats->max_allocated_bytes = 0;
318 #endif /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
319 
320 	k_spin_unlock(&slab->lock, key);
321 
322 	return 0;
323 }
324 
325 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
k_mem_slab_runtime_stats_reset_max(struct k_mem_slab * slab)326 int k_mem_slab_runtime_stats_reset_max(struct k_mem_slab *slab)
327 {
328 	if (slab == NULL) {
329 		return -EINVAL;
330 	}
331 
332 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
333 
334 	slab->info.max_used = slab->info.num_used;
335 
336 	k_spin_unlock(&slab->lock, key);
337 
338 	return 0;
339 }
340 #endif /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
341