Skip to content

Commit a603171

Browse files
dcpleungnashif
authored andcommitted
kernel: mem_domain: k_mem_domain_add_thread to return errors
This updates k_mem_domain_add_thread() to return errors so the application has a chance to recover. Signed-off-by: Daniel Leung <[email protected]>
1 parent 1cd7ccc commit a603171

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

include/app_memory/mem_domain.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ extern int k_mem_domain_remove_partition(struct k_mem_domain *domain,
183183
* @param domain The memory domain that the thread is going to be added into.
184184
* @param thread ID of thread going to be added into the memory domain.
185185
*
186+
* @return 0 if successful, fails otherwise.
186187
*/
187-
extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
188-
k_tid_t thread);
188+
extern int k_mem_domain_add_thread(struct k_mem_domain *domain,
189+
k_tid_t thread);
189190

190191
#ifdef __cplusplus
191192
}

kernel/mem_domain.c

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ int k_mem_domain_init(struct k_mem_domain *domain, uint8_t num_parts,
139139
domain->partitions[i] = *parts[i];
140140
domain->num_partitions++;
141141
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
142-
arch_mem_domain_partition_add(domain, i);
142+
int ret2 = arch_mem_domain_partition_add(domain, i);
143+
144+
ARG_UNUSED(ret2);
145+
CHECKIF(ret2 != 0) {
146+
ret = ret2;
147+
}
143148
#endif
144149
}
145150
}
@@ -194,7 +199,7 @@ int k_mem_domain_add_partition(struct k_mem_domain *domain,
194199
domain->num_partitions++;
195200

196201
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
197-
arch_mem_domain_partition_add(domain, p_idx);
202+
ret = arch_mem_domain_partition_add(domain, p_idx);
198203
#endif
199204

200205
unlock_out:
@@ -236,7 +241,7 @@ int k_mem_domain_remove_partition(struct k_mem_domain *domain,
236241
part->start, part->size, domain);
237242

238243
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
239-
arch_mem_domain_partition_remove(domain, p_idx);
244+
ret = arch_mem_domain_partition_remove(domain, p_idx);
240245
#endif
241246

242247
/* A zero-sized partition denotes it's a free partition */
@@ -251,9 +256,11 @@ int k_mem_domain_remove_partition(struct k_mem_domain *domain,
251256
return ret;
252257
}
253258

254-
static void add_thread_locked(struct k_mem_domain *domain,
255-
k_tid_t thread)
259+
static int add_thread_locked(struct k_mem_domain *domain,
260+
k_tid_t thread)
256261
{
262+
int ret = 0;
263+
257264
__ASSERT_NO_MSG(domain != NULL);
258265
__ASSERT_NO_MSG(thread != NULL);
259266

@@ -263,50 +270,72 @@ static void add_thread_locked(struct k_mem_domain *domain,
263270
thread->mem_domain_info.mem_domain = domain;
264271

265272
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
266-
arch_mem_domain_thread_add(thread);
273+
ret = arch_mem_domain_thread_add(thread);
267274
#endif
275+
276+
return ret;
268277
}
269278

270-
static void remove_thread_locked(struct k_thread *thread)
279+
static int remove_thread_locked(struct k_thread *thread)
271280
{
281+
int ret = 0;
282+
272283
__ASSERT_NO_MSG(thread != NULL);
273284
LOG_DBG("remove thread %p from memory domain %p\n",
274285
thread, thread->mem_domain_info.mem_domain);
275286
sys_dlist_remove(&thread->mem_domain_info.mem_domain_q_node);
276287

277288
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
278-
arch_mem_domain_thread_remove(thread);
289+
ret = arch_mem_domain_thread_remove(thread);
279290
#endif
291+
292+
return ret;
280293
}
281294

282295
/* Called from thread object initialization */
283296
void z_mem_domain_init_thread(struct k_thread *thread)
284297
{
298+
int ret;
285299
k_spinlock_key_t key = k_spin_lock(&z_mem_domain_lock);
286300

287301
/* New threads inherit memory domain configuration from parent */
288-
add_thread_locked(_current->mem_domain_info.mem_domain, thread);
302+
ret = add_thread_locked(_current->mem_domain_info.mem_domain, thread);
303+
__ASSERT_NO_MSG(ret == 0);
304+
ARG_UNUSED(ret);
305+
289306
k_spin_unlock(&z_mem_domain_lock, key);
290307
}
291308

292309
/* Called when thread aborts during teardown tasks. sched_spinlock is held */
293310
void z_mem_domain_exit_thread(struct k_thread *thread)
294311
{
312+
int ret;
313+
295314
k_spinlock_key_t key = k_spin_lock(&z_mem_domain_lock);
296-
remove_thread_locked(thread);
315+
316+
ret = remove_thread_locked(thread);
317+
__ASSERT_NO_MSG(ret == 0);
318+
ARG_UNUSED(ret);
319+
297320
k_spin_unlock(&z_mem_domain_lock, key);
298321
}
299322

300-
void k_mem_domain_add_thread(struct k_mem_domain *domain, k_tid_t thread)
323+
int k_mem_domain_add_thread(struct k_mem_domain *domain, k_tid_t thread)
301324
{
325+
int ret = 0;
302326
k_spinlock_key_t key;
303327

304328
key = k_spin_lock(&z_mem_domain_lock);
305329
if (thread->mem_domain_info.mem_domain != domain) {
306-
remove_thread_locked(thread);
307-
add_thread_locked(domain, thread);
330+
ret = remove_thread_locked(thread);
331+
332+
if (ret == 0) {
333+
ret = add_thread_locked(domain, thread);
334+
}
308335
}
309336
k_spin_unlock(&z_mem_domain_lock, key);
337+
338+
return ret;
310339
}
311340

312341
static int init_mem_domain_module(const struct device *arg)

0 commit comments

Comments
 (0)