Skip to content
91 changes: 88 additions & 3 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,83 @@ int z_impl_mod_free(struct processing_module *mod, const void *ptr)
EXPORT_SYMBOL(z_impl_mod_free);

#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION)
#if CONFIG_SOF_USERSPACE_LL
/* an LL module whose resources the LL and IPC user threads use in user mode */
struct mod_res_user_entry {
struct list_item list;
struct processing_module *mod;
};

/* the LL modules registered with mod_res_user_add(), under mod_res_ulock */
static APP_SYSUSER_DATA struct list_item mod_res_user_list = LIST_INIT(mod_res_user_list);

int mod_res_user_add(struct processing_module *mod)
{
struct mod_res_user_entry *entry;

if (!k_is_user_context())
return -EPERM;

/* the IPC user threads of all cores walk the list */
entry = sof_heap_alloc(sof_sys_user_heap_get(), SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(*entry), 0);
if (!entry)
return -ENOMEM;

entry->mod = mod;

sys_mutex_lock(&mod_res_ulock, K_FOREVER);
list_item_append(&entry->list, &mod_res_user_list);
sys_mutex_unlock(&mod_res_ulock);

return 0;
}

void mod_res_user_remove(struct processing_module *mod)
{
struct mod_res_user_entry *entry = NULL;
struct list_item *item;

if (!k_is_user_context())
return;

sys_mutex_lock(&mod_res_ulock, K_FOREVER);
list_for_item(item, &mod_res_user_list) {
entry = container_of(item, struct mod_res_user_entry, list);
if (entry->mod == mod) {
list_item_del(item);
break;
}
entry = NULL;
}
sys_mutex_unlock(&mod_res_ulock);

sof_heap_free(sof_sys_user_heap_get(), entry);
}

static bool mod_res_user_listed(const struct processing_module *mod)
{
struct mod_res_user_entry *entry;
struct list_item *item;
bool found = false;

/* not an LL or IPC user thread */
if (sys_mutex_lock(&mod_res_ulock, K_FOREVER))
return false;

list_for_item(item, &mod_res_user_list) {
entry = container_of(item, struct mod_res_user_entry, list);
if (entry->mod == mod) {
found = true;
break;
}
}
sys_mutex_unlock(&mod_res_ulock);

return found;
}
#endif

/*
* With CONFIG_SOF_USERSPACE_LL, the LL and IPC user threads use the resources
* of LL modules in user mode: the module memory, its resource pool and the
Expand All @@ -614,14 +691,22 @@ EXPORT_SYMBOL(z_impl_mod_free);
* kernel structures behind their vregions. In kernel mode the system call
* wrappers call the implementation directly.
*
* The processing domain is user-writable, so a user thread can only pick the
* wrong path for itself: the system calls reject LL modules.
* The user-mode path is taken only for the LL modules registered in LL
* memory when they were created, not by the processing domain or other
* module data: the comp_dev and module objects of a DP module are in DP
* memory, so a DP thread could make them look like an LL module, or hand
* over a made-up module, whose resource pool the IPC user thread would then
* walk in user mode. Any other module goes to the system calls, which reject
* LL modules and modules without a live vregion. The processing domain is
* only checked first, so that DP threads, which have no access to the list,
* use the system calls for their own module.
*/
static bool mod_res_user(struct processing_module *mod)
{
#if CONFIG_SOF_USERSPACE_LL
return k_is_user_context() &&
mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP;
mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP &&
mod_res_user_listed(mod);
#else
return false;
#endif
Expand Down
31 changes: 28 additions & 3 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ __cold static
struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv,
const struct comp_ipc_config *config,
const struct module_ext_init_data *ext_init,
struct mod_alloc_ctx *ppl_alloc)
struct mod_alloc_ctx *ppl_alloc,
struct vregion **dp_vreg)
{
struct k_heap *mod_heap;
struct vregion *mod_vreg;
Expand Down Expand Up @@ -186,8 +187,27 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
mod->dev = dev;
dev->mod = mod;

#if CONFIG_SOF_USERSPACE_LL
/* an LL module in LL memory, its resources are used in user mode */
if (k_is_user_context() && config->proc_domain != COMP_PROCESSING_DOMAIN_DP &&
mod_res_user_add(mod) < 0) {
comp_cl_err(drv, "failed to register the module resources");
goto eres;
}
#endif

/* the region of a DP module, from here and not from module memory */
*dp_vreg = use_ppl_alloc ? NULL : mod_vreg;

return mod;

#if CONFIG_SOF_USERSPACE_LL
eres:
if (mod_vreg)
vregion_user_free(mod_vreg, dev);
else
sof_heap_free(mod_heap, dev);
#endif
edev:
if (!use_ppl_alloc && !mod_vreg)
sof_heap_free(mod_heap, alloc);
Expand All @@ -210,6 +230,9 @@ __cold static void module_adapter_mem_free(struct processing_module *mod)

assert_can_be_cold();

/* after the last module resource call */
mod_res_user_remove(mod);

/*
* In principle it shouldn't even be needed to free individual objects
* on the module heap since we're freeing the heap itself too
Expand Down Expand Up @@ -296,7 +319,9 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
ppl_alloc = ipc_pipe->pipeline->alloc;
#endif

struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc);
struct vregion *dp_vreg;
struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc,
&dp_vreg);
if (!mod)
return NULL;

Expand Down Expand Up @@ -338,7 +363,7 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
/* create a task for DP processing */
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP) {
/* All data allocated, create a thread */
ret = pipeline_comp_dp_task_init(dev);
ret = pipeline_comp_dp_task_init(dev, dp_vreg);
if (ret) {
comp_cl_err(drv, "DP task creation failed with error %d.", ret);
goto err;
Expand Down
16 changes: 14 additions & 2 deletions src/audio/pipeline/pipeline-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,22 @@ int pipeline_complete(struct pipeline *p, struct comp_dev *source,
p->sink_comp = sink;

if (p->alloc->vreg) {
int heap_ret;

vregion_set_interim(p->alloc->vreg);

/* like a region too small for a kernel interim heap, not fatal here */
if (vregion_user_heap_add(p->alloc->vreg) < 0)
/*
* Like a region too small for a kernel interim heap, a too small
* one is not fatal here. But without a free entry in the table of
* the user-mode heaps, the interim range of the region could not
* be used at all, also by the kernel: fail the completion.
*/
heap_ret = vregion_user_heap_add(p->alloc->vreg);
if (heap_ret == -ENOSPC) {
pipe_err(p, "no user-mode heap left for the region");
return heap_ret;
}
if (heap_ret < 0)
pipe_warn(p, "no interim heap");
}

Expand Down
4 changes: 2 additions & 2 deletions src/audio/pipeline/pipeline-schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ static enum task_state dp_task_run(void *data)
return SOF_TASK_STATE_RESCHEDULE;
}

int pipeline_comp_dp_task_init(struct comp_dev *comp)
int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg)
{
/* DP tasks are guaranteed to have a module_adapter */
struct processing_module *mod = comp_mod(comp);
Expand Down Expand Up @@ -620,7 +620,7 @@ int pipeline_comp_dp_task_init(struct comp_dev *comp)
CONFIG_ZEPHYR_DP_SCHEDULER_MIN_STACK_SIZE);
}

return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod,
return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod, vreg,
comp->ipc_config.core, stack_size, flags);
}
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
Expand Down
28 changes: 28 additions & 0 deletions src/include/sof/audio/module_adapter/module/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ void *z_impl_mod_balloc_align(struct processing_module *mod, size_t size, size_t
*/
void mod_resource_init(struct processing_module *mod);

#if CONFIG_SOF_USERSPACE_LL
/**
* @brief Let the LL and IPC user threads use the resources of an LL module.
*
* In user mode only, for a module whose memory and resource pool are in LL
* memory. The module resource functions then run in user mode for @p mod,
* see mod_res_user() in generic.c, and use the system calls for any other
* module.
*
* @param mod Pointer to processing module.
* @return 0 on success or a negative error code.
*/
int mod_res_user_add(struct processing_module *mod);

/**
* @brief Undo mod_res_user_add(), before the module memory is freed.
* @param mod Pointer to processing module.
*/
void mod_res_user_remove(struct processing_module *mod);
#else
static inline int mod_res_user_add(struct processing_module *mod)
{
return 0;
}

static inline void mod_res_user_remove(struct processing_module *mod) {}
#endif

/**
* @brief Query module memory heap boundary and size.
* @param mod Pointer to processing module.
Expand Down
4 changes: 3 additions & 1 deletion src/include/sof/audio/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
struct comp_buffer;
struct comp_dev;
struct ipc;
struct vregion;
struct ipc_msg;
struct k_heap;
struct mod_alloc_ctx;
Expand Down Expand Up @@ -411,9 +412,10 @@ void pipeline_comp_ll_task_free(struct pipeline *p);
/**
* \brief Initializes a data processing (DP) task for a component.
* \param[in,out] comp Component device for which the task is created.
* \param[in] vreg Region of the module created by the kernel, or NULL.
* \return 0 on success, or negative error code on failure.
*/
int pipeline_comp_dp_task_init(struct comp_dev *comp);
int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg);

/**
* \brief Schedules a pipeline copy operation to run after a delay.
Expand Down
12 changes: 12 additions & 0 deletions src/include/sof/debug/telemetry/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,18 @@ void telemetry_sample_ll_user_heap(void);
static inline void telemetry_sample_ll_user_heap(void) {}
#endif

#if defined(CONFIG_SOF_TELEMETRY_VREGION) && defined(CONFIG_SOF_VREGION_USER_HEAP)
/**
* \brief Copy the counters of the user-mode region heaps for telemetry.
*
* Must be called in a system call of an LL or IPC user thread: kernel threads
* must not access the region memory.
*/
void telemetry_sample_vregion_user_heaps(void);
#else
static inline void telemetry_sample_vregion_user_heaps(void) {}
#endif

#ifdef CONFIG_SOF_TELEMETRY_VREGION
void telemetry_collect_vpage_stats(struct sof_tel_vregion_section *vsec);
void telemetry_collect_vregion_stats(struct sof_tel_vregion_section *vsec);
Expand Down
60 changes: 60 additions & 0 deletions src/include/sof/lib/vregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
* @brief Virtual region descriptor managing lifetime and interim heap partitions.
*/
struct vregion;
struct k_thread;

/**
* @brief Memory allocation modes within a virtual memory region.
Expand Down Expand Up @@ -103,6 +104,18 @@ __syscall struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg
*/
__syscall void vregion_set_interim(struct vregion *vr);

/**
* @brief Record the user thread that owns a virtual region.
*
* Kernel code only. The owner is the DP thread of the module that the region
* belongs to. It may use the region through the vregion system calls, see
* vregion_owner_check(). The owner is kept in the kernel-only struct vregion.
*
* @param[in] vr Pointer to the virtual region instance, may be NULL.
* @param[in] thread The owner thread, or NULL to clear the owner.
*/
void vregion_owner_set(struct vregion *vr, struct k_thread *thread);

/**
* @brief Get the interim range of a virtual region with VREGION_FLAG_USER_HEAP.
*
Expand Down Expand Up @@ -222,6 +235,45 @@ void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t *start);
*/
bool vregion_verify_pin(struct vregion *vr);

/**
* @brief Check the caller of a vregion system call.
*
* For system call validators, on a vregion pinned with vregion_verify_pin().
* Passes for the LL and IPC user threads and for the owner thread of vr, see
* vregion_owner_set(). Unpins vr and kills any other caller.
*
* @param[in] vr Pinned virtual region instance.
* @return True for the LL and IPC user threads, false for the owner, which
* may only use the lifetime allocator.
*/
bool vregion_owner_check(struct vregion *vr);

/**
* @brief Allocate from the lifetime allocator of a virtual region only.
*
* Kernel code only, for the owner thread of the region. Never allocates from
* the interim heap, which the kernel may run on the region memory.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] size Size of memory to allocate in bytes.
* @param[in] alignment Alignment of memory to allocate in bytes.
* @param[in] coherent Return the uncached alias, as vregion_alloc_coherent_align().
* @return Pointer to the allocated memory, or NULL on failure.
*/
void *vregion_lifetime_alloc_align(struct vregion *vr, size_t size, size_t alignment,
bool coherent);

/**
* @brief Free to the lifetime allocator of a virtual region only.
*
* Kernel code only, for the owner thread of the region. Refuses pointers in
* the interim range.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] ptr Pointer to the memory to free.
*/
void vregion_lifetime_free(struct vregion *vr, void *ptr);

/**
* @brief Release a pin taken with vregion_verify_pin().
*
Expand Down Expand Up @@ -380,6 +432,7 @@ static inline struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *
return NULL;
}
static inline void vregion_set_interim(struct vregion *vr) {}
static inline void vregion_owner_set(struct vregion *vr, struct k_thread *thread) {}
static inline int vregion_interim_get(struct vregion *vr, uintptr_t *start, size_t *size)
{
return -EPERM;
Expand Down Expand Up @@ -437,6 +490,13 @@ static inline void *vregion_user_alloc_coherent_align(struct vregion *vr, size_t
}
static inline void vregion_user_free(struct vregion *vr, void *ptr) {}
static inline bool vregion_verify_pin(struct vregion *vr) { return false; }
static inline bool vregion_owner_check(struct vregion *vr) { return false; }
static inline void *vregion_lifetime_alloc_align(struct vregion *vr, size_t size,
size_t alignment, bool coherent)
{
return NULL;
}
static inline void vregion_lifetime_free(struct vregion *vr, void *ptr) {}
static inline void vregion_unpin(struct vregion *vr) {}
static inline bool vregion_pinned_get(struct vregion *vr) { return false; }
static inline int vregion_pinned_put(struct vregion *vr) { return -EINVAL; }
Expand Down
Loading
Loading