Skip to content

Commit bb595a8

Browse files
dcpleungnashif
authored andcommitted
kernel: mem_domain: add/remove partition funcs to return errors
This changes both k_mem_domain_add_partition() and k_mem_domain_remove_partition() to return errors instead of asserting when errors are encountered. This gives the application chance to recover. The arch_mem_domain_parition_add()/_remove() will be modified later together with all the other arch_mem_domain_*() changes since the architecture code for partition addition and removal functions usually cannot be separately changed. Signed-off-by: Daniel Leung <[email protected]>
1 parent fb91ce2 commit bb595a8

File tree

9 files changed

+210
-81
lines changed

9 files changed

+210
-81
lines changed

include/app_memory/mem_domain.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ extern int k_mem_domain_init(struct k_mem_domain *domain, uint8_t num_parts,
151151
*
152152
* @param domain The memory domain to be added a memory partition.
153153
* @param part The memory partition to be added
154+
*
155+
* @retval 0 if successful
156+
* @retval -EINVAL if invalid parameters supplied
157+
* @retval -ENOSPC if no free partition slots available
154158
*/
155-
extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
159+
extern int k_mem_domain_add_partition(struct k_mem_domain *domain,
156160
struct k_mem_partition *part);
157161

158162
/**
@@ -162,8 +166,12 @@ extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
162166
*
163167
* @param domain The memory domain to be removed a memory partition.
164168
* @param part The memory partition to be removed
169+
*
170+
* @retval 0 if successful
171+
* @retval -EINVAL if invalid parameters supplied
172+
* @retval -ENOENT if no matching partition found
165173
*/
166-
extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
174+
extern int k_mem_domain_remove_partition(struct k_mem_domain *domain,
167175
struct k_mem_partition *part);
168176

169177
/**

kernel/mem_domain.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,23 @@ int k_mem_domain_init(struct k_mem_domain *domain, uint8_t num_parts,
151151
return ret;
152152
}
153153

154-
void k_mem_domain_add_partition(struct k_mem_domain *domain,
155-
struct k_mem_partition *part)
154+
int k_mem_domain_add_partition(struct k_mem_domain *domain,
155+
struct k_mem_partition *part)
156156
{
157157
int p_idx;
158158
k_spinlock_key_t key;
159+
int ret = 0;
159160

160-
__ASSERT_NO_MSG(domain != NULL);
161-
__ASSERT(check_add_partition(domain, part),
162-
"invalid partition %p", part);
161+
CHECKIF(domain == NULL) {
162+
ret = -EINVAL;
163+
goto out;
164+
}
165+
166+
CHECKIF(!check_add_partition(domain, part)) {
167+
LOG_ERR("invalid partition %p", part);
168+
ret = -EINVAL;
169+
goto out;
170+
}
163171

164172
key = k_spin_lock(&z_mem_domain_lock);
165173

@@ -170,8 +178,11 @@ void k_mem_domain_add_partition(struct k_mem_domain *domain,
170178
}
171179
}
172180

173-
__ASSERT(p_idx < max_partitions,
174-
"no free partition slots available");
181+
CHECKIF(!(p_idx < max_partitions)) {
182+
LOG_ERR("no free partition slots available");
183+
ret = -ENOSPC;
184+
goto unlock_out;
185+
}
175186

176187
LOG_DBG("add partition base %lx size %zu to domain %p\n",
177188
part->start, part->size, domain);
@@ -185,17 +196,25 @@ void k_mem_domain_add_partition(struct k_mem_domain *domain,
185196
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
186197
arch_mem_domain_partition_add(domain, p_idx);
187198
#endif
199+
200+
unlock_out:
188201
k_spin_unlock(&z_mem_domain_lock, key);
202+
203+
out:
204+
return ret;
189205
}
190206

191-
void k_mem_domain_remove_partition(struct k_mem_domain *domain,
207+
int k_mem_domain_remove_partition(struct k_mem_domain *domain,
192208
struct k_mem_partition *part)
193209
{
194210
int p_idx;
195211
k_spinlock_key_t key;
212+
int ret = 0;
196213

197-
__ASSERT_NO_MSG(domain != NULL);
198-
__ASSERT_NO_MSG(part != NULL);
214+
CHECKIF((domain == NULL) || (part == NULL)) {
215+
ret = -EINVAL;
216+
goto out;
217+
}
199218

200219
key = k_spin_lock(&z_mem_domain_lock);
201220

@@ -207,7 +226,11 @@ void k_mem_domain_remove_partition(struct k_mem_domain *domain,
207226
}
208227
}
209228

210-
__ASSERT(p_idx < max_partitions, "no matching partition found");
229+
CHECKIF(!(p_idx < max_partitions)) {
230+
LOG_ERR("no matching partition found");
231+
ret = -ENOENT;
232+
goto unlock_out;
233+
}
211234

212235
LOG_DBG("remove partition base %lx size %zu from domain %p\n",
213236
part->start, part->size, domain);
@@ -221,7 +244,11 @@ void k_mem_domain_remove_partition(struct k_mem_domain *domain,
221244

222245
domain->num_partitions--;
223246

247+
unlock_out:
224248
k_spin_unlock(&z_mem_domain_lock, key);
249+
250+
out:
251+
return ret;
225252
}
226253

227254
static void add_thread_locked(struct k_mem_domain *domain,
@@ -301,7 +328,9 @@ static int init_mem_domain_module(const struct device *arg)
301328
__ASSERT(ret == 0, "failed to init default mem domain");
302329

303330
#ifdef Z_LIBC_PARTITION_EXISTS
304-
k_mem_domain_add_partition(&k_mem_domain_default, &z_libc_partition);
331+
ret = k_mem_domain_add_partition(&k_mem_domain_default,
332+
&z_libc_partition);
333+
__ASSERT(ret == 0, "failed to add default libc mem partition");
305334
#endif /* Z_LIBC_PARTITION_EXISTS */
306335

307336
return 0;

samples/userspace/prod_consumer/src/app_b.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,27 @@ static void processor_thread(void *p1, void *p2, void *p3)
7676

7777
void app_b_entry(void *p1, void *p2, void *p3)
7878
{
79+
int ret;
80+
7981
/* Much like how we are reusing the main thread as this application's
8082
* processor thread, we will re-use the default memory domain as the
8183
* domain for application B.
8284
*/
83-
k_mem_domain_add_partition(&k_mem_domain_default, &app_b_partition);
84-
k_mem_domain_add_partition(&k_mem_domain_default, &shared_partition);
85+
ret = k_mem_domain_add_partition(&k_mem_domain_default,
86+
&app_b_partition);
87+
if (ret != 0) {
88+
LOG_ERR("Failed to add app_b_partition to mem domain (%d)",
89+
ret);
90+
k_oops();
91+
}
92+
93+
ret = k_mem_domain_add_partition(&k_mem_domain_default,
94+
&shared_partition);
95+
if (ret != 0) {
96+
LOG_ERR("Failed to add shared_partition to mem domain (%d)",
97+
ret);
98+
k_oops();
99+
}
85100

86101
/* Assign a resource pool to serve for kernel-side allocations on
87102
* behalf of application A. Needed for k_queue_alloc_append().

samples/userspace/shared_mem/src/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,20 @@ void main(void)
158158
k_thread_access_grant(tCT, &allforone);
159159
printk("CT Thread Created %p\n", tCT);
160160
/* Re-using the default memory domain for CT */
161-
k_mem_domain_add_partition(&k_mem_domain_default, &ct_part);
162-
k_mem_domain_add_partition(&k_mem_domain_default, &blk_part);
161+
ret = k_mem_domain_add_partition(&k_mem_domain_default, &ct_part);
162+
if (ret != 0) {
163+
printk("Failed to add ct_part to mem domain (%d)\n", ret);
164+
k_oops();
165+
}
163166
printk("ct partitions installed\n");
164167

168+
ret = k_mem_domain_add_partition(&k_mem_domain_default, &blk_part);
169+
if (ret != 0) {
170+
printk("Failed to add blk_part to mem domain (%d)\n", ret);
171+
k_oops();
172+
}
173+
printk("blk partitions installed\n");
174+
165175
k_thread_start(&enc_thread);
166176
/* need to start all three threads. let enc go first to perform init step */
167177

subsys/testsuite/ztest/src/ztest.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,29 @@ int main(void)
509509
void main(void)
510510
{
511511
#ifdef CONFIG_USERSPACE
512+
int ret;
513+
512514
/* Partition containing globals tagged with ZTEST_DMEM and ZTEST_BMEM
513515
* macros. Any variables that user code may reference need to be
514516
* placed in this partition if no other memory domain configuration
515517
* is made.
516518
*/
517-
k_mem_domain_add_partition(&k_mem_domain_default,
518-
&ztest_mem_partition);
519+
ret = k_mem_domain_add_partition(&k_mem_domain_default,
520+
&ztest_mem_partition);
521+
if (ret != 0) {
522+
PRINT("ERROR: failed to add ztest_mem_partition to mem domain (%d)\n",
523+
ret);
524+
k_oops();
525+
}
519526
#ifdef Z_MALLOC_PARTITION_EXISTS
520527
/* Allow access to malloc() memory */
521-
k_mem_domain_add_partition(&k_mem_domain_default,
522-
&z_malloc_partition);
528+
ret = k_mem_domain_add_partition(&k_mem_domain_default,
529+
&z_malloc_partition);
530+
if (ret != 0) {
531+
PRINT("ERROR: failed to add z_malloc_partition to mem domain (%d)\n",
532+
ret);
533+
k_oops();
534+
}
523535
#endif
524536
#endif /* CONFIG_USERSPACE */
525537

tests/crypto/mbedtls/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ extern void test_mbedtls(void);
1313
void test_main(void)
1414
{
1515
#ifdef CONFIG_USERSPACE
16-
k_mem_domain_add_partition(&k_mem_domain_default, &k_mbedtls_partition);
16+
int ret = k_mem_domain_add_partition(&k_mem_domain_default,
17+
&k_mbedtls_partition);
18+
if (ret != 0) {
19+
printk("Failed to add memory partition (%d)\n", ret);
20+
k_oops();
21+
}
1722
#endif
1823
ztest_test_suite(test_mbedtls_fn,
1924
ztest_user_unit_test(test_mbedtls));

0 commit comments

Comments
 (0)