Skip to content

Commit cb01fad

Browse files
committed
kernel: mem_domain: keep track of threads only if needed
Adds a new kconfig CONFIG_MEM_DOMAIN_HAS_THREAD_LIST so that only the architectures requiring to keep track of threads in memory domains will have the necessary list struct inside the memory domain structs. Saves a few bytes for those arch not needing this. Also rename the struct fields to be most descriptive of what they are. Signed-off-by: Daniel Leung <[email protected]>
1 parent c77e5a6 commit cb01fad

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

arch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ config ARM64
5757
select ARCH_HAS_DEMAND_MAPPING
5858
select ARCH_SUPPORTS_EVICTION_TRACKING
5959
select EVICTION_TRACKING if DEMAND_PAGING
60+
select MEM_DOMAIN_HAS_THREAD_LIST if ARM_MPU
6061
help
6162
ARM64 (AArch64) architecture
6263

arch/arm64/core/cortex_r/arm_mpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ static int configure_domain_partitions(struct k_mem_domain *domain)
760760
struct k_thread *thread;
761761
int ret;
762762

763-
SYS_DLIST_FOR_EACH_CONTAINER(&domain->mem_domain_q, thread,
764-
mem_domain_info.mem_domain_q_node) {
763+
SYS_DLIST_FOR_EACH_CONTAINER(&domain->thread_mem_domain_list, thread,
764+
mem_domain_info.thread_mem_domain_node) {
765765
ret = configure_dynamic_mpu_regions(thread);
766766
if (ret != 0) {
767767
return ret;

include/zephyr/app_memory/mem_domain.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ struct k_mem_domain {
8383
#endif /* CONFIG_ARCH_MEM_DOMAIN_DATA */
8484
/** partitions in the domain */
8585
struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
86-
/** Doubly linked list of member threads */
87-
sys_dlist_t mem_domain_q;
86+
#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
87+
/** Doubly linked list of member threads,
88+
* pointer to the thread_mem_domain_node inside
89+
* each thread's memory domain info struct.
90+
*/
91+
sys_dlist_t thread_mem_domain_list;
92+
#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
8893
/** number of active partitions in the domain */
8994
uint8_t num_partitions;
9095
};

include/zephyr/kernel/thread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ typedef struct _thread_stack_info _thread_stack_info_t;
178178

179179
#if defined(CONFIG_USERSPACE)
180180
struct _mem_domain_info {
181+
#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
181182
/** memory domain queue node */
182-
sys_dnode_t mem_domain_q_node;
183+
sys_dnode_t thread_mem_domain_node;
184+
#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
183185
/** memory domain of the thread */
184186
struct k_mem_domain *mem_domain;
185187
};

kernel/Kconfig.mem_domain

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ config MEM_DOMAIN_ISOLATED_STACKS
7676
Regardless of this settings, threads cannot access the stacks of
7777
threads outside of their domains.
7878

79+
config MEM_DOMAIN_HAS_THREAD_LIST
80+
bool
81+
help
82+
If enabled, there is a doubly linked list in each memory domain
83+
struct to keep track of the threads associated with this
84+
particular memory domain.
85+
86+
This is selected by architecture needing this to function.
87+
7988
endmenu

kernel/mem_domain.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ int k_mem_domain_init(struct k_mem_domain *domain, uint8_t num_parts,
113113

114114
domain->num_partitions = 0U;
115115
(void)memset(domain->partitions, 0, sizeof(domain->partitions));
116-
sys_dlist_init(&domain->mem_domain_q);
116+
117+
#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
118+
sys_dlist_init(&domain->thread_mem_domain_list);
119+
#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
117120

118121
#ifdef CONFIG_ARCH_MEM_DOMAIN_DATA
119122
ret = arch_mem_domain_init(domain);
@@ -265,8 +268,12 @@ static int add_thread_locked(struct k_mem_domain *domain,
265268
__ASSERT_NO_MSG(thread != NULL);
266269

267270
LOG_DBG("add thread %p to domain %p\n", thread, domain);
268-
sys_dlist_append(&domain->mem_domain_q,
269-
&thread->mem_domain_info.mem_domain_q_node);
271+
272+
#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
273+
sys_dlist_append(&domain->thread_mem_domain_list,
274+
&thread->mem_domain_info.thread_mem_domain_node);
275+
#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
276+
270277
thread->mem_domain_info.mem_domain = domain;
271278

272279
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
@@ -283,7 +290,10 @@ static int remove_thread_locked(struct k_thread *thread)
283290
__ASSERT_NO_MSG(thread != NULL);
284291
LOG_DBG("remove thread %p from memory domain %p\n",
285292
thread, thread->mem_domain_info.mem_domain);
286-
sys_dlist_remove(&thread->mem_domain_info.mem_domain_q_node);
293+
294+
#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
295+
sys_dlist_remove(&thread->mem_domain_info.thread_mem_domain_node);
296+
#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
287297

288298
#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
289299
ret = arch_mem_domain_thread_remove(thread);

0 commit comments

Comments
 (0)