1 /*
2  * Copyright (c) 2016 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <kernel.h>
8 #include <kernel_structs.h>
9 
10 #include <toolchain.h>
11 #include <linker/sections.h>
12 #include <wait_q.h>
13 #include <sys/dlist.h>
14 #include <ksched.h>
15 #include <init.h>
16 #include <sys/check.h>
17 
18 /**
19  * @brief Initialize kernel memory slab subsystem.
20  *
21  * Perform any initialization of memory slabs that wasn't done at build time.
22  * Currently this just involves creating the list of free blocks for each slab.
23  *
24  * @return N/A
25  */
create_free_list(struct k_mem_slab * slab)26 static int create_free_list(struct k_mem_slab *slab)
27 {
28 	uint32_t j;
29 	char *p;
30 
31 	/* blocks must be word aligned */
32 	CHECKIF(((slab->block_size | (uintptr_t)slab->buffer) &
33 				(sizeof(void *) - 1)) != 0U) {
34 		return -EINVAL;
35 	}
36 
37 	slab->free_list = NULL;
38 	p = slab->buffer;
39 
40 	for (j = 0U; j < slab->num_blocks; j++) {
41 		*(char **)p = slab->free_list;
42 		slab->free_list = p;
43 		p += slab->block_size;
44 	}
45 	return 0;
46 }
47 
48 /**
49  * @brief Complete initialization of statically defined memory slabs.
50  *
51  * Perform any initialization that wasn't done at build time.
52  *
53  * @return N/A
54  */
init_mem_slab_module(const struct device * dev)55 static int init_mem_slab_module(const struct device *dev)
56 {
57 	int rc = 0;
58 	ARG_UNUSED(dev);
59 
60 	STRUCT_SECTION_FOREACH(k_mem_slab, slab) {
61 		rc = create_free_list(slab);
62 		if (rc < 0) {
63 			goto out;
64 		}
65 		z_object_init(slab);
66 	}
67 
68 out:
69 	return rc;
70 }
71 
72 SYS_INIT(init_mem_slab_module, PRE_KERNEL_1,
73 	 CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
74 
k_mem_slab_init(struct k_mem_slab * slab,void * buffer,size_t block_size,uint32_t num_blocks)75 int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
76 		    size_t block_size, uint32_t num_blocks)
77 {
78 	int rc = 0;
79 
80 	slab->num_blocks = num_blocks;
81 	slab->block_size = block_size;
82 	slab->buffer = buffer;
83 	slab->num_used = 0U;
84 	slab->lock = (struct k_spinlock) {};
85 
86 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
87 	slab->max_used = 0U;
88 #endif
89 
90 	rc = create_free_list(slab);
91 	if (rc < 0) {
92 		goto out;
93 	}
94 
95 	z_waitq_init(&slab->wait_q);
96 	z_object_init(slab);
97 out:
98 	SYS_PORT_TRACING_OBJ_INIT(k_mem_slab, slab, rc);
99 
100 	return rc;
101 }
102 
k_mem_slab_alloc(struct k_mem_slab * slab,void ** mem,k_timeout_t timeout)103 int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
104 {
105 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
106 	int result;
107 
108 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, alloc, slab, timeout);
109 
110 	if (slab->free_list != NULL) {
111 		/* take a free block */
112 		*mem = slab->free_list;
113 		slab->free_list = *(char **)(slab->free_list);
114 		slab->num_used++;
115 
116 #ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
117 		slab->max_used = MAX(slab->num_used, slab->max_used);
118 #endif
119 
120 		result = 0;
121 	} else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT) ||
122 		   !IS_ENABLED(CONFIG_MULTITHREADING)) {
123 		/* don't wait for a free block to become available */
124 		*mem = NULL;
125 		result = -ENOMEM;
126 	} else {
127 		SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_mem_slab, alloc, slab, timeout);
128 
129 		/* wait for a free block or timeout */
130 		result = z_pend_curr(&slab->lock, key, &slab->wait_q, timeout);
131 		if (result == 0) {
132 			*mem = _current->base.swap_data;
133 		}
134 
135 		SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
136 
137 		return result;
138 	}
139 
140 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, alloc, slab, timeout, result);
141 
142 	k_spin_unlock(&slab->lock, key);
143 
144 	return result;
145 }
146 
k_mem_slab_free(struct k_mem_slab * slab,void ** mem)147 void k_mem_slab_free(struct k_mem_slab *slab, void **mem)
148 {
149 	k_spinlock_key_t key = k_spin_lock(&slab->lock);
150 
151 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_mem_slab, free, slab);
152 	if (slab->free_list == NULL && IS_ENABLED(CONFIG_MULTITHREADING)) {
153 		struct k_thread *pending_thread = z_unpend_first_thread(&slab->wait_q);
154 
155 		if (pending_thread != NULL) {
156 			SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
157 
158 			z_thread_return_value_set_with_data(pending_thread, 0, *mem);
159 			z_ready_thread(pending_thread);
160 			z_reschedule(&slab->lock, key);
161 			return;
162 		}
163 	}
164 	**(char ***) mem = slab->free_list;
165 	slab->free_list = *(char **) mem;
166 	slab->num_used--;
167 
168 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_mem_slab, free, slab);
169 
170 	k_spin_unlock(&slab->lock, key);
171 }
172