Skip to content

Commit 48fa2f6

Browse files
lyakhlgirdwood
authored andcommitted
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 1e9c28a commit 48fa2f6

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

posix/include/rtos/kernel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <rtos/wait.h>
1212

13+
#include <stdbool.h>
1314
#include <stdint.h>
1415

1516
#ifdef __ZEPHYR__
@@ -28,6 +29,13 @@ typedef struct {
2829

2930
#define Z_TIMEOUT_MS(t) ((k_timeout_t) { .ticks = clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK, t) })
3031

32+
struct k_thread;
33+
static inline bool thread_is_userspace(struct k_thread *thread)
34+
{
35+
(void)thread;
36+
return false;
37+
}
38+
3139
static inline void k_sleep(k_timeout_t timeout)
3240
{
3341
wait_delay(timeout.ticks);

zephyr/include/rtos/kernel.h

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

99
#include <zephyr/kernel.h>
1010

11+
#include <stdbool.h>
12+
13+
static inline bool thread_is_userspace(struct k_thread *thread)
14+
{
15+
return !!(thread->base.user_options & K_USER);
16+
}
17+
1118
#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)