Skip to content

Commit af60c25

Browse files
committed
fast-get: fix userspace thread detection
Checking number of memory domain partitions in thread's domain to determine whether it's a userspace thread isn't reliable and is a layering violation. Instead check the K_USER flag which indicates exactly that. Also add a missing check when adding a partition for the newly allocated entry. Suggested-by: Adrian Warecki <adrian.warecki@intel.com> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 1f18231 commit af60c25

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

posix/include/rtos/kernel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ typedef struct {
2828

2929
#define Z_TIMEOUT_MS(t) ((k_timeout_t) { .ticks = clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, t) })
3030

31+
struct k_thread;
32+
static inline bool thread_is_userspace(struct k_thread *thread)
33+
{
34+
return false;
35+
}
36+
3137
static inline void k_sleep(k_timeout_t timeout)
3238
{
3339
wait_delay(timeout.ticks);

zephyr/include/rtos/kernel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88

99
#include <zephyr/kernel.h>
1010

11+
static inline bool thread_is_userspace(struct k_thread *thread)
12+
{
13+
return !!(thread->base.user_options & K_USER);
14+
}
15+
1116
#endif /* __ZEPHYR_RTOS_KERNEL_H__ */

zephyr/lib/fast-get.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ static int fast_get_access_grant(struct k_mem_domain *mdom, void *addr, size_t s
132132

133133
const void *fast_get(struct k_heap *heap, const void *dram_ptr, size_t size)
134134
{
135+
#if CONFIG_USERSPACE
136+
bool current_is_userspace = thread_is_userspace(k_current_get());
137+
#endif
135138
struct sof_fast_get_data *data = &fast_get_data;
136139
uint32_t alloc_flags = SOF_MEM_FLAG_USER;
137140
struct sof_fast_get_entry *entry;
@@ -189,8 +192,7 @@ const void *fast_get(struct k_heap *heap, const void *dram_ptr, size_t size)
189192
* We only get there for large buffers, since small buffers with
190193
* enabled userspace don't create fast-get entries
191194
*/
192-
if (mdom->num_partitions > 1) {
193-
/* A userspace thread makes the request */
195+
if (current_is_userspace) {
194196
if (mdom != entry->mdom &&
195197
!fast_get_partition_exists(k_current_get(), ret,
196198
ALIGN_UP(size, CONFIG_MM_DRV_PAGE_SIZE))) {
@@ -235,7 +237,7 @@ const void *fast_get(struct k_heap *heap, const void *dram_ptr, size_t size)
235237

236238
#if CONFIG_USERSPACE
237239
entry->mdom = k_current_get()->mem_domain_info.mem_domain;
238-
if (size > FAST_GET_MAX_COPY_SIZE) {
240+
if (size > FAST_GET_MAX_COPY_SIZE && current_is_userspace) {
239241
/* Otherwise we've allocated on thread's heap, so it already has access */
240242
int err = fast_get_access_grant(entry->mdom, ret, size);
241243

0 commit comments

Comments
 (0)