From a4b763a44a3c75c89e65e78ac4ca99a1bafbf238 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Mon, 23 Nov 2026 14:49:27 +0200 Subject: [PATCH 1/5] zephyr: ll_user: add a fast_get area to the LL memory region LL modules get their fast_get() copies, such as SRC coefficients, as private copies in their own memory, since the fast_get() state is in kernel memory and the kernel must not allocate from the LL user heap. Copies of the same data are no longer shared, and LL threads can write them. Reserve a fast_get area in the LL memory region for these copies, 32 kB by default when fast_get() is enabled and the memory has mirrored cached and uncached aliases. Its allocator is a bitmap of 256-byte blocks in kernel memory. Only the cached alias of the area is mapped into the LL memory domain, read-only, so kernel code can write a copy through the uncached alias also in a system call from an LL thread, while LL threads only read it. The cache lines of the area are written back and invalidated at boot, so no dirty line can later overwrite a copy. The area is taken from the LL user heap, which shrinks by the same amount. The private copies that the area replaces came from that heap. Signed-off-by: Kai Vehmanen --- src/include/sof/schedule/ll_schedule_domain.h | 34 +++++ src/kernel/schedule/zephyr_ll_user.c | 131 ++++++++++++++++-- zephyr/Kconfig | 21 ++- 3 files changed, 177 insertions(+), 9 deletions(-) diff --git a/src/include/sof/schedule/ll_schedule_domain.h b/src/include/sof/schedule/ll_schedule_domain.h index 890d6a5d3e8f..b960c085920d 100644 --- a/src/include/sof/schedule/ll_schedule_domain.h +++ b/src/include/sof/schedule/ll_schedule_domain.h @@ -257,6 +257,40 @@ void *zephyr_ll_karea_alloc(enum zephyr_ll_karea area, uint32_t flags, size_t by */ bool zephyr_ll_karea_contains(enum zephyr_ll_karea area, const void *ptr, size_t bytes); +/** + * @brief Allocate from the fast_get area of the LL user memory region. + * + * For the fast_get() copies of the LL modules. The allocator state is + * kernel-only. LL user threads may only read the area, through the cached + * alias. Kernel code writes it through the returned uncached alias, which + * works also in system calls from LL threads. Kernel context only. + * + * @param[in] bytes Size in bytes. + * @return Uncached pointer to the memory, or NULL if the area is full. + */ +void *zephyr_ll_fg_alloc(size_t bytes); + +/** + * @brief Free memory from zephyr_ll_fg_alloc(). Kernel context only. + * @param[in] ptr Start of the memory, cached or uncached alias. + * @param[in] bytes Size in bytes, as allocated. + */ +void zephyr_ll_fg_free(void *ptr, size_t bytes); + +/** + * @brief Check that memory lies in the fast_get area of the LL region. + * @param[in] ptr Start of the memory, cached or uncached alias. + * @param[in] bytes Size in bytes. + * @return true if [ptr, ptr + bytes) lies in the area. + */ +bool zephyr_ll_fg_contains(const void *ptr, size_t bytes); + +/** + * @brief Get the allocated size of the fast_get area. Kernel context only. + * @return Allocated bytes, in whole allocation units. + */ +size_t zephyr_ll_fg_used(void); + /** * @brief Grant thread access to per-core Low-Latency mutexes. * diff --git a/src/kernel/schedule/zephyr_ll_user.c b/src/kernel/schedule/zephyr_ll_user.c index 5930a5da2cb1..fd679bc6fa1e 100644 --- a/src/kernel/schedule/zephyr_ll_user.c +++ b/src/kernel/schedule/zephyr_ll_user.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,7 @@ LOG_MODULE_DECLARE(ll_schedule, CONFIG_SOF_LOG_LEVEL); /* * Layout of the LL user memory region, each part page aligned: * - * | kernel RO area | kernel RW area | LL user heap | + * | kernel RO area | fast_get area | kernel RW area | LL user heap | * * The heap and the kernel RW area are mapped read-write into the LL memory * domain, the kernel RO area read-only. Kernel code allocates objects that @@ -27,17 +28,37 @@ LOG_MODULE_DECLARE(ll_schedule, CONFIG_SOF_LOG_LEVEL); * ll_mem_resources, which is kernel-only. The heap allocator runs in user * mode, see src/schedule/zephyr_ll_user_heap.c: the kernel initialises the * heap at boot and does not parse its metadata after that. + * + * The fast_get area holds the fast_get() copies of the LL modules, see + * zephyr_ll_fg_alloc(). Only its cached alias is mapped into the LL memory + * domain, read-only, so kernel code can write copies through the uncached + * alias also in system calls from LL threads. */ #define LL_REGION_SIZE CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE #define LL_KAREA_RO_SIZE CONFIG_SOF_ZEPHYR_LL_KAREA_RO_SIZE #define LL_KAREA_RW_SIZE CONFIG_SOF_ZEPHYR_LL_KAREA_RW_SIZE -#define LL_HEAP_SIZE (LL_REGION_SIZE - LL_KAREA_RO_SIZE - LL_KAREA_RW_SIZE) +#define LL_FG_SIZE CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE +#define LL_HEAP_SIZE (LL_REGION_SIZE - LL_KAREA_RO_SIZE - LL_FG_SIZE - \ + LL_KAREA_RW_SIZE) + +/* fast_get area allocation unit, a multiple of the cache line size */ +#define LL_FG_BLOCK_SIZE 256 +#define LL_FG_BLOCKS (LL_FG_SIZE / LL_FG_BLOCK_SIZE) BUILD_ASSERT(LL_REGION_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0); BUILD_ASSERT(LL_KAREA_RO_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0); BUILD_ASSERT(LL_KAREA_RW_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0); -BUILD_ASSERT(LL_KAREA_RO_SIZE + LL_KAREA_RW_SIZE < LL_REGION_SIZE, +BUILD_ASSERT(LL_FG_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0); +BUILD_ASSERT(LL_FG_BLOCK_SIZE % PLATFORM_DCACHE_ALIGN == 0); +BUILD_ASSERT(LL_KAREA_RO_SIZE + LL_FG_SIZE + LL_KAREA_RW_SIZE < LL_REGION_SIZE, "kernel areas do not leave room for the LL user heap"); +BUILD_ASSERT(!LL_FG_SIZE || IS_ENABLED(CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS), + "the kernel writes the fast_get area through the uncached alias"); + +#if LL_FG_SIZE +/* allocation bitmap of the fast_get area, a bit per block */ +SYS_BITARRAY_DEFINE_STATIC(ll_fg_blocks, LL_FG_BLOCKS); +#endif /* A kernel-managed area, addresses are the uncached alias */ struct zephyr_ll_karea_state { @@ -61,11 +82,12 @@ struct zephyr_ll_mem_resources { */ struct k_heap heap; struct zephyr_ll_karea_state karea[ZEPHYR_LL_KAREA_COUNT]; /**< Kernel-managed areas */ + uint8_t *fg_base; /**< fast_get area, uncached alias */ }; static struct zephyr_ll_mem_resources ll_mem_resources; -static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t attr) +static void ll_region_map_cached(uint8_t *start, size_t size, k_mem_partition_attr_t attr) { struct k_mem_partition part; int ret; @@ -78,9 +100,18 @@ static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t at tr_dbg(&ll_tr, "map %p size %#zx (cached), ret %d", (void *)part.start, size, ret); if (ret) k_panic(); +} + +static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t attr) +{ + ll_region_map_cached(start, size, attr); #ifdef CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS + struct k_mem_partition part; + int ret; + part.start = (uintptr_t)sys_cache_uncached_ptr_get(start); + part.size = size; part.attr = attr; ret = k_mem_domain_add_partition(&ll_mem_resources.mem_domain, &part); @@ -105,21 +136,32 @@ static void ll_region_init(void) ro->base = mem; ro->size = LL_KAREA_RO_SIZE; - rw->base = mem + LL_KAREA_RO_SIZE; + ll_mem_resources.fg_base = mem + LL_KAREA_RO_SIZE; + rw->base = ll_mem_resources.fg_base + LL_FG_SIZE; rw->size = LL_KAREA_RW_SIZE; - mem += LL_KAREA_RO_SIZE + LL_KAREA_RW_SIZE; + mem = rw->base + LL_KAREA_RW_SIZE; /* the only time the kernel writes the heap metadata */ zephyr_ll_user_heap_init(mem, LL_HEAP_SIZE); if (ro->size) ll_region_map(ro->base, ro->size, K_MEM_PARTITION_P_RO_U_RO); + if (LL_FG_SIZE) { + void __sparse_cache *fg = sys_cache_cached_ptr_get(ll_mem_resources.fg_base); + + /* no dirty lines may be written back over the copies */ + sys_cache_data_flush_and_invd_range((__sparse_force void *)fg, LL_FG_SIZE); + ll_region_map_cached(ll_mem_resources.fg_base, LL_FG_SIZE, + K_MEM_PARTITION_P_RO_U_RO); + } + /* the kernel RW area and the heap are adjacent, map them together */ ll_region_map(rw->base, rw->size + LL_HEAP_SIZE, K_MEM_PARTITION_P_RW_U_RW); - tr_info(&ll_tr, "LL region: RO area %p %#zx, RW area %p %#zx, heap %p %#zx", - (void *)ro->base, ro->size, (void *)rw->base, rw->size, (void *)mem, + tr_info(&ll_tr, "LL region: RO area %p %#zx, fast_get area %p %#zx, RW area %p %#zx, heap %p %#zx", + (void *)ro->base, ro->size, (void *)ll_mem_resources.fg_base, + (size_t)LL_FG_SIZE, (void *)rw->base, rw->size, (void *)mem, (size_t)LL_HEAP_SIZE); } @@ -190,6 +232,79 @@ bool zephyr_ll_karea_contains(enum zephyr_ll_karea area, const void *ptr, size_t start - (uintptr_t)ka->base <= ka->size - bytes; } +bool zephyr_ll_fg_contains(const void *ptr, size_t bytes) +{ + uintptr_t base = (uintptr_t)ll_mem_resources.fg_base; + uintptr_t start; + + start = (uintptr_t)sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr); + + return LL_FG_SIZE && start >= base && bytes <= LL_FG_SIZE && + start - base <= LL_FG_SIZE - bytes; +} + +#if LL_FG_SIZE +void *zephyr_ll_fg_alloc(size_t bytes) +{ + size_t offset; + int ret; + + __ASSERT(!k_is_user_context(), "kernel context only"); + + if (!bytes || bytes > LL_FG_SIZE) + return NULL; + + ret = sys_bitarray_alloc(&ll_fg_blocks, DIV_ROUND_UP(bytes, LL_FG_BLOCK_SIZE), &offset); + if (ret) { + tr_warn(&ll_tr, "fast_get area full: %zu bytes requested, %zu of %zu used", bytes, + zephyr_ll_fg_used(), (size_t)LL_FG_SIZE); + return NULL; + } + + return ll_mem_resources.fg_base + offset * LL_FG_BLOCK_SIZE; +} + +void zephyr_ll_fg_free(void *ptr, size_t bytes) +{ + uintptr_t start; + int ret = -EINVAL; + + __ASSERT(!k_is_user_context(), "kernel context only"); + + start = (uintptr_t)sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr); + start -= (uintptr_t)ll_mem_resources.fg_base; + + if (bytes && zephyr_ll_fg_contains(ptr, bytes) && !(start % LL_FG_BLOCK_SIZE)) + ret = sys_bitarray_free(&ll_fg_blocks, DIV_ROUND_UP(bytes, LL_FG_BLOCK_SIZE), + start / LL_FG_BLOCK_SIZE); + if (ret) + tr_err(&ll_tr, "bad fast_get area free of %zu bytes at %p: %d", bytes, ptr, ret); +} + +size_t zephyr_ll_fg_used(void) +{ + uint32_t count = 0; + + sys_bitarray_popcount_region(&ll_fg_blocks, LL_FG_BLOCKS, 0, &count); + + return (size_t)count * LL_FG_BLOCK_SIZE; +} +#else +void *zephyr_ll_fg_alloc(size_t bytes) +{ + return NULL; +} + +void zephyr_ll_fg_free(void *ptr, size_t bytes) +{ +} + +size_t zephyr_ll_fg_used(void) +{ + return 0; +} +#endif + void zephyr_ll_user_resources_init(void) { int ret; diff --git a/zephyr/Kconfig b/zephyr/Kconfig index e3446288d1e4..1cd530fc1d2c 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -171,7 +171,8 @@ config SOF_ZEPHYR_SYS_USER_HEAP_SIZE help The size of the memory region of the low-latency (LL) scheduler user-space threads: the shared LL user heap and the kernel-managed - areas SOF_ZEPHYR_LL_KAREA_RW_SIZE and SOF_ZEPHYR_LL_KAREA_RO_SIZE. + areas SOF_ZEPHYR_LL_KAREA_RW_SIZE, SOF_ZEPHYR_LL_KAREA_RO_SIZE and + SOF_ZEPHYR_LL_FG_AREA_SIZE. The heap is shared across all pipelines running on the LL thread and must be large enough to hold all host DMA buffers, chain DMA data, SG elements, and module adapter buffers @@ -204,6 +205,24 @@ config SOF_ZEPHYR_LL_KAREA_RO_SIZE SOF_ZEPHYR_SYS_USER_HEAP_SIZE. Must be a multiple of the MMU page size. +config SOF_ZEPHYR_LL_FG_AREA_SIZE + hex "Size of the fast_get area for LL user threads" + default 0x8000 if FAST_GET && COLD_STORE_EXECUTE_DRAM && CACHE_HAS_MIRRORED_MEMORY_REGIONS + default 0 + depends on SOF_USERSPACE_LL + help + Size of the area in the LL user memory region that holds the + fast_get() copies of the LL modules, such as SRC coefficients. + Modules that use the same data share a copy. Kernel code + allocates the copies, with the allocator state in its own memory, + and the area is mapped read-only into the LL memory domain. When + the area is full, LL modules get a private copy in their own + memory instead. Kernel code writes the copies through the uncached + alias of the area, so it needs mirrored cached and uncached memory + regions. Without COLD_STORE_EXECUTE_DRAM there are no copies and + the default is 0. The area is taken from SOF_ZEPHYR_SYS_USER_HEAP_SIZE. + Must be a multiple of the MMU page size. + config SOF_USERSPACE_PROXY bool "Use userspace proxy to support userspace modules" select SOF_USERSPACE_USE_DRIVER_HEAP From e4d4617ce58bf8482891eadd1ddfcd8de429a2a7 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Mon, 23 Nov 2026 15:00:33 +0200 Subject: [PATCH 2/5] fast_get: add system calls for shared copies in the LL fast_get area LL modules make their fast_get() copies in user mode, as private copies in their own memory, since the fast_get() state is in kernel memory. Add the fast_get_ll() and fast_put_ll() system calls for LL user threads instead, which share a copy between all users of the same data. The copies come from the fast_get area of the LL memory region, which LL threads can only read. Kernel code writes a copy through the uncached alias, and invalidates the cached alias on the calling core on every get, as the core may still hold lines of an earlier copy at the same address. The entries stay in kernel memory, marked as LL copies: fast_get() and fast_put() never use them, and the new calls use no other entries, so a user thread cannot make the kernel free a copy with the wrong allocator. Only the LL and IPC user threads may make the calls, and the data must be readable by the caller. Signed-off-by: Kai Vehmanen --- src/include/sof/lib/fast-get.h | 28 ++++++ zephyr/lib/fast-get.c | 153 ++++++++++++++++++++++++++++++++- 2 files changed, 179 insertions(+), 2 deletions(-) diff --git a/src/include/sof/lib/fast-get.h b/src/include/sof/lib/fast-get.h index dfea81410470..2ba4a9ed2363 100644 --- a/src/include/sof/lib/fast-get.h +++ b/src/include/sof/lib/fast-get.h @@ -53,6 +53,29 @@ const void *fast_get(struct mod_alloc_ctx *alloc, const void * const dram_ptr, s * @param[in,out] sram_ptr Pointer to staged SRAM memory to release. */ void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void *sram_ptr); + +#if CONFIG_SOF_USERSPACE_LL +/** + * @brief Get a read-only copy of DRAM data for an LL user thread. + * + * The copy is in the fast_get area of the LL memory region and shared by + * all users of the same data. Only threads in the LL memory domain may + * call this. + * @param[in] dram_ptr Pointer to source data resident in DRAM. + * @param[in] size Size in bytes to copy. + * @return Pointer to the cached alias of the copy, or NULL on failure, + * for example when the area is full. + */ +__syscall const void *fast_get_ll(const void *dram_ptr, size_t size); + +/** + * @brief Release a copy from fast_get_ll(). + * @param[in] sram_ptr Pointer returned by fast_get_ll(). + */ +__syscall void fast_put_ll(const void *sram_ptr); + +#include +#endif #else static inline const void *fast_get(struct mod_alloc_ctx *alloc, const void * const dram_ptr, size_t size) @@ -61,6 +84,11 @@ static inline const void *fast_get(struct mod_alloc_ctx *alloc, const void * con } static inline void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void *sram_ptr) {} +static inline const void *fast_get_ll(const void *dram_ptr, size_t size) +{ + return dram_ptr; +} +static inline void fast_put_ll(const void *sram_ptr) {} #endif /** @}*/ diff --git a/zephyr/lib/fast-get.c b/zephyr/lib/fast-get.c index 532552cb9f33..c7d8dbca6366 100644 --- a/zephyr/lib/fast-get.c +++ b/zephyr/lib/fast-get.c @@ -15,6 +15,9 @@ #if CONFIG_SOF_USERSPACE_LL && CONFIG_SOF_VREGIONS #include #endif +#if CONFIG_SOF_USERSPACE_LL +#include +#endif #include #include #include @@ -30,6 +33,7 @@ struct sof_fast_get_entry { void *sram_ptr; size_t size; unsigned int refcount; + bool ll; /* a copy for LL threads, see fast_get_ll() */ }; struct sof_fast_get_data { @@ -87,15 +91,17 @@ LOG_MODULE_REGISTER(fast_get, CONFIG_SOF_LOG_LEVEL); struct fast_get_find { const void *ptr; + bool ll; struct sof_fast_get_entry *entry; }; +/* finds an unused entry when find->ptr is NULL */ static bool fast_get_find_entry(void *data, void *arg) { struct sof_fast_get_entry *entry = data; struct fast_get_find *find = arg; - if (find->ptr != entry->dram_ptr) + if (find->ptr != entry->dram_ptr || find->ll != entry->ll) return false; find->entry = entry; @@ -389,7 +395,7 @@ static bool fast_put_find_entry(void *data, void *arg) struct sof_fast_get_entry *entry = data; struct fast_get_find *find = arg; - if (find->ptr != entry->sram_ptr) + if (find->ptr != entry->sram_ptr || find->ll != entry->ll) return false; find->entry = entry; @@ -471,3 +477,146 @@ void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void fast_get_unlock(key); } EXPORT_SYMBOL(fast_put); + +#if CONFIG_SOF_USERSPACE_LL +/* + * Copies for LL user threads, in the fast_get area of the LL memory region. + * The area is mapped read-only into the LL memory domain, so they cannot + * change a copy that other modules share. The entries and the allocator + * state are in kernel memory. Only the cached alias of the area is mapped, + * so the copy is written through the uncached alias, and the cached alias + * is invalidated on the calling core: the core may hold lines of an earlier + * copy at the same address. LL modules run on the core of their + * pipeline, where they also get their copies. + */ +const void *z_impl_fast_get_ll(const void *dram_ptr, size_t size) +{ + struct fast_get_find find = { + .ptr = dram_ptr, + .ll = true, + }; + struct sof_fast_get_entry *entry; + const void *ret = NULL; + fast_get_key_t key; + void *copy; + + key = fast_get_lock(); + + objpool_iterate(&fast_get_data.pool, fast_get_find_entry, &find); + entry = find.entry; + if (entry) { + if (entry->size != size) { + LOG_ERR("size %zu != %zu mismatch for %p", entry->size, size, dram_ptr); + goto out; + } + + entry->refcount++; + ret = entry->sram_ptr; + goto out; + } + + find = (struct fast_get_find){ .ptr = NULL }; + objpool_iterate(&fast_get_data.pool, fast_get_find_entry, &find); + entry = find.entry; + if (!entry) { + entry = objpool_alloc(&fast_get_data.pool, sizeof(*entry), + SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT); + if (!entry) + goto out; + } + + copy = zephyr_ll_fg_alloc(size); + if (!copy) + goto out; + + memcpy_s(copy, size, dram_ptr, size); + + entry->dram_ptr = dram_ptr; + entry->size = size; + entry->sram_ptr = (__sparse_force void *)sys_cache_cached_ptr_get(copy); + entry->refcount = 1; + entry->ll = true; + ret = entry->sram_ptr; +#ifdef CONFIG_SOF_TELEMETRY_MEMORY + fast_get_active_entries++; + fast_get_live_bytes += (uint32_t)size; + if (fast_get_live_bytes > fast_get_peak_bytes) + fast_get_peak_bytes = fast_get_live_bytes; +#endif +out: + if (ret) + sys_cache_data_invd_range((void *)ret, size); +#ifdef CONFIG_SOF_TELEMETRY_MEMORY + fast_get_total_calls++; + if (!ret) + fast_get_fail_count++; +#endif + fast_get_unlock(key); + LOG_DBG("get %p, %p, size %zu, refcnt %u", dram_ptr, ret, size, + ret ? entry->refcount : 0); + + return ret; +} + +void z_impl_fast_put_ll(const void *sram_ptr) +{ + struct fast_get_find find = { + .ptr = sram_ptr, + .ll = true, + }; + struct sof_fast_get_entry *entry; + fast_get_key_t key; + + key = fast_get_lock(); + + objpool_iterate(&fast_get_data.pool, fast_put_find_entry, &find); + entry = find.entry; + if (!entry) { + LOG_ERR("Put called to unknown address %p", sram_ptr); + goto out; + } + + if (--entry->refcount) + goto out; + +#ifdef CONFIG_SOF_TELEMETRY_MEMORY + if (fast_get_active_entries > 0) + fast_get_active_entries--; + if (fast_get_live_bytes >= (uint32_t)entry->size) + fast_get_live_bytes -= (uint32_t)entry->size; + else + fast_get_live_bytes = 0; +#endif + zephyr_ll_fg_free(entry->sram_ptr, entry->size); + memset(entry, 0, sizeof(*entry)); +out: + fast_get_unlock(key); +} + +#ifdef CONFIG_USERSPACE +#include +#include + +/* + * Only the LL and IPC user threads may use the fast_get area: the copies + * are read-only to the LL memory domain only, and the entries count the + * users of the LL modules only. + */ +const void *z_vrfy_fast_get_ll(const void *dram_ptr, size_t size) +{ + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + K_OOPS(K_SYSCALL_MEMORY_READ(dram_ptr, size)); + + return z_impl_fast_get_ll(dram_ptr, size); +} +#include + +void z_vrfy_fast_put_ll(const void *sram_ptr) +{ + SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + + z_impl_fast_put_ll(sram_ptr); +} +#include +#endif /* CONFIG_USERSPACE */ +#endif /* CONFIG_SOF_USERSPACE_LL */ From a1e6b01e6a7542bc3018199c6539fa19adb35f68 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Mon, 23 Nov 2026 15:00:33 +0200 Subject: [PATCH 3/5] module: share the fast_get copies of LL modules in the LL fast_get area LL modules in user mode get a private copy of their fast_get data, such as SRC coefficients, in the module memory. Every instance makes its own copy, and LL threads can write it. Get the copies with fast_get_ll() instead, and release them with fast_put_ll(), so instances share a read-only copy in the fast_get area of the LL memory region. The resource is tracked as before, in user mode. When the area is full, fall back to a private copy. Signed-off-by: Kai Vehmanen --- src/audio/module_adapter/module/generic.c | 27 ++++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index a2176be33f85..219054dbd1de 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -489,7 +489,13 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons return NULL; } - ptr = fast_get(res->alloc, dram_ptr, size); +#if CONFIG_SOF_USERSPACE_LL + /* only LL modules use their resources in user mode, see mod_res_user() */ + if (k_is_user_context()) + ptr = fast_get_ll(dram_ptr, size); + else +#endif + ptr = fast_get(res->alloc, dram_ptr, size); if (!ptr) { container_put(mod, container); mod_res_unlock(); @@ -525,6 +531,12 @@ static int free_contents(struct processing_module *mod, struct module_resource * #endif #if CONFIG_FAST_GET case MOD_RES_FAST_GET: +#if CONFIG_SOF_USERSPACE_LL + if (k_is_user_context()) { + fast_put_ll(container->sram_ptr); + return 0; + } +#endif #if CONFIG_USERSPACE mdom = mod->mdom; #else @@ -678,9 +690,9 @@ EXPORT_SYMBOL(mod_data_blob_set); #if CONFIG_FAST_GET #if CONFIG_SOF_USERSPACE_LL /* - * fast_get() keeps its state in kernel memory, so LL modules in user mode - * get a private copy in the module memory instead of a shared one. The - * copy is a heap resource, so mod_fast_put() frees it with mod_free(). + * When the fast_get area of the LL memory region is full, LL modules get a + * private copy in the module memory instead of a shared one. The copy is a + * heap resource, so mod_fast_put() frees it with mod_free(). */ static const void *mod_fast_get_copy(struct processing_module *mod, const void * const dram_ptr, size_t size) @@ -701,8 +713,11 @@ const void *mod_fast_get(struct processing_module *mod, const void * const dram_ size_t size) { #if CONFIG_SOF_USERSPACE_LL - if (mod_res_user(mod)) - return mod_fast_get_copy(mod, dram_ptr, size); + if (mod_res_user(mod)) { + const void *ptr = z_impl_mod_fast_get(mod, dram_ptr, size); + + return ptr ? ptr : mod_fast_get_copy(mod, dram_ptr, size); + } #endif return mod_dp_fast_get(mod, dram_ptr, size); From ebae6960866bb3830984998c6164fc5b4194a1b6 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Mon, 23 Nov 2026 15:21:44 +0200 Subject: [PATCH 4/5] zephyr: test: userspace: test the LL fast_get area Add a standalone suite for the fast_get area of the LL memory region. An IPC user thread gets a copy of the same data twice and both users share it, the copy is in the area and is freed with its last user, and kernel code does not release it with fast_put(). A freed copy is reused for other data without stale cache lines, and a user thread that writes a copy faults. A thread in the LL memory domain that is not the LL or IPC user thread cannot get copies, nor can the IPC user thread get a copy of memory it cannot read. An LL module gets its copy from the area, and a private copy when the area is full. The suite shares the user thread of the LL user heap suite, as the standalone build has no thread ID left for another one. Signed-off-by: Kai Vehmanen --- zephyr/test/userspace/README.md | 5 + zephyr/test/userspace/test_ll_user_heap.c | 243 +++++++++++++++++++++- 2 files changed, 246 insertions(+), 2 deletions(-) diff --git a/zephyr/test/userspace/README.md b/zephyr/test/userspace/README.md index 63a9d4716268..78cefadb76f9 100644 --- a/zephyr/test/userspace/README.md +++ b/zephyr/test/userspace/README.md @@ -24,6 +24,11 @@ Available tests: that kernel code refuses to use it, that a user thread corrupting its metadata only faults user threads, and that the DP module resource system calls reject LL modules and forged contexts. + - Also tests the fast_get area of the LL memory region: LL user + threads get shared read-only copies, a freed copy is reused + without stale data, only the LL and IPC user threads may get + copies of data they can read, and LL modules fall back to a + private copy when the area is full. Building for Intel Panther Lake: ./scripts/xtensa-build-zephyr.py --cmake-args=-DCONFIG_SOF_BOOT_TEST_STANDALONE=y \ diff --git a/zephyr/test/userspace/test_ll_user_heap.c b/zephyr/test/userspace/test_ll_user_heap.c index e622671a10c8..906337b5cef3 100644 --- a/zephyr/test/userspace/test_ll_user_heap.c +++ b/zephyr/test/userspace/test_ll_user_heap.c @@ -9,12 +9,20 @@ * use it, and a user thread that corrupts its metadata can only fault * itself. Also checks that the module resource system calls reject LL * modules and forged allocation contexts. + * + * Tests for the fast_get area of the LL memory region: LL user threads + * get shared read-only copies with fast_get_ll(), freed copies are reused + * without stale data, only the LL and IPC user threads may make the calls, + * and kernel code does not release LL copies with fast_put(). LL modules + * fall back to a private copy when the area is full. */ #include #include #include +#include #include +#include #include #include @@ -144,14 +152,17 @@ static void user_function(void *p1, void *p2, void *p3) zassert_unreachable("user thread did not fault"); } -static void run_case(enum user_heap_test_case test_case, bool fault_allowed) +/* @infra: run as the IPC user thread, otherwise as another LL domain thread */ +static void run_user(k_thread_entry_t entry, int test_case, bool infra, bool fault_allowed) { int ret; k_thread_create(&user_thread, user_stack, USER_STACKSIZE, - user_function, (void *)(uintptr_t)test_case, NULL, NULL, + entry, (void *)(uintptr_t)test_case, NULL, NULL, -1, K_USER, K_FOREVER); k_mem_domain_add_thread(zephyr_ll_mem_domain(), &user_thread); + if (infra) + sof_sysuser_thread_set(SOF_SYSUSER_IPC, PLATFORM_PRIMARY_CORE_ID, &user_thread); if (fault_allowed) sof_boot_test_set_fault_valid(&user_thread); @@ -160,10 +171,17 @@ static void run_case(enum user_heap_test_case test_case, bool fault_allowed) ret = k_thread_join(&user_thread, K_FOREVER); sof_boot_test_set_fault_valid(NULL); + if (infra) + sof_sysuser_thread_set(SOF_SYSUSER_IPC, PLATFORM_PRIMARY_CORE_ID, NULL); zassert_equal(ret, 0, "user thread join failed: %d", ret); } +static void run_case(enum user_heap_test_case test_case, bool fault_allowed) +{ + run_user(user_function, test_case, false, fault_allowed); +} + static void *ll_user_heap_setup(void) { k_mem_domain_add_partition(zephyr_ll_mem_domain(), &ll_user_heap_test_part); @@ -300,6 +318,224 @@ ZTEST(userspace_ll_user_heap, mod_user_rejects_forged_heap) ZTEST_SUITE(userspace_ll_user_heap, sof_boot_test_standalone_predicate, ll_user_heap_setup, NULL, NULL, ll_user_heap_teardown); +/* without DRAM execution fast_get_ll() is a passthrough and there are no copies */ +#define HAVE_LL_FAST_GET_COPIES (CONFIG_FAST_GET && CONFIG_COLD_STORE_EXECUTE_DRAM) + +#if HAVE_LL_FAST_GET_COPIES +#define DATA_SIZE 1000 +/* more than the fast_get area can hold */ +#define FG_MAX_BLOCKS (CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE / 256 + 1) + +/* source data and results, the user threads read and write them */ +K_APP_BMEM(ll_user_heap_test_part) static uint8_t data_a[DATA_SIZE]; +K_APP_BMEM(ll_user_heap_test_part) static uint8_t data_b[DATA_SIZE]; +K_APP_BMEM(ll_user_heap_test_part) static const void *fg_ptr[2]; + +/* kernel-only, a user thread must not get a copy of it */ +static uint8_t kernel_only_data[DATA_SIZE]; + +static void *fg_blocks[FG_MAX_BLOCKS]; + +enum fast_get_test_case { + /* get data_a twice, check the copies, keep them in fg_ptr */ + FG_GET_SHARED, + /* put the copies in fg_ptr */ + FG_PUT_KEPT, + /* get data_a, check it and put it, then the same for data_b */ + FG_GET_PUT_A_THEN_B, + /* write to a copy */ + FG_WRITE_COPY, + /* get a copy of kernel memory */ + FG_GET_KERNEL_DATA, + /* get data_a with the module interface, keep it in fg_ptr[0] */ + FG_MOD_GET, + /* put fg_ptr[0] with the module interface */ + FG_MOD_PUT, + /* free the module resources */ + FG_MOD_FREE_ALL, +}; + +static void check_copy(const void *copy, const uint8_t *data) +{ + zassert_not_null(copy); + zassert_not_equal(copy, data); + zassert_true(sys_cache_is_ptr_cached((void *)copy)); + zassert_mem_equal(copy, data, DATA_SIZE); +} + +static void fast_get_user_function(void *p1, void *p2, void *p3) +{ + enum fast_get_test_case test_case = (enum fast_get_test_case)(uintptr_t)p1; + const void *ptr; + + zassert_true(k_is_user_context(), "isn't user"); + + switch (test_case) { + case FG_GET_SHARED: + fg_ptr[0] = fast_get_ll(data_a, DATA_SIZE); + check_copy(fg_ptr[0], data_a); + fg_ptr[1] = fast_get_ll(data_a, DATA_SIZE); + zassert_equal(fg_ptr[1], fg_ptr[0], "copy not shared"); + /* a size that does not match the shared copy */ + zassert_is_null(fast_get_ll(data_a, DATA_SIZE / 2)); + return; + case FG_PUT_KEPT: + fast_put_ll(fg_ptr[0]); + fast_put_ll(fg_ptr[1]); + return; + case FG_GET_PUT_A_THEN_B: + /* reads data_a into the cache, a later copy may reuse the lines */ + fg_ptr[0] = fast_get_ll(data_a, DATA_SIZE); + check_copy(fg_ptr[0], data_a); + fast_put_ll(fg_ptr[0]); + fg_ptr[1] = fast_get_ll(data_b, DATA_SIZE); + check_copy(fg_ptr[1], data_b); + fast_put_ll(fg_ptr[1]); + return; + case FG_WRITE_COPY: + ptr = fast_get_ll(data_a, DATA_SIZE); + zassert_not_null(ptr); + fg_ptr[0] = ptr; + *(volatile uint8_t *)ptr = 0; + break; + case FG_GET_KERNEL_DATA: + (void)fast_get_ll(kernel_only_data, DATA_SIZE); + break; + case FG_MOD_GET: + fg_ptr[0] = mod_fast_get(&fake_mod, data_a, DATA_SIZE); + check_copy(fg_ptr[0], data_a); + return; + case FG_MOD_PUT: + mod_fast_put(&fake_mod, fg_ptr[0]); + return; + case FG_MOD_FREE_ALL: + mod_free_all(&fake_mod); + return; + default: + zassert_unreachable("unknown test case"); + } + + zassert_unreachable("user thread did not fault"); +} + +static void run_fg_case(enum fast_get_test_case test_case, bool infra, bool fault_allowed) +{ + run_user(fast_get_user_function, test_case, infra, fault_allowed); +} + +static void *ll_fast_get_setup(void) +{ + int i; + + for (i = 0; i < DATA_SIZE; i++) { + data_a[i] = i; + data_b[i] = ~i; + } + + return ll_user_heap_setup(); +} + +ZTEST(userspace_ll_fast_get, shared_copy) +{ + size_t used = zephyr_ll_fg_used(); + + run_fg_case(FG_GET_SHARED, true, false); + zassert_true(zephyr_ll_fg_contains(fg_ptr[0], DATA_SIZE)); + zassert_equal(zephyr_ll_fg_used(), used + ROUND_UP(DATA_SIZE, 256)); + + /* kernel code does not release LL copies with fast_put() */ + fast_put(NULL, NULL, fg_ptr[0]); + zassert_equal(zephyr_ll_fg_used(), used + ROUND_UP(DATA_SIZE, 256)); + + /* the copy is freed with its last user */ + run_fg_case(FG_PUT_KEPT, true, false); + zassert_equal(zephyr_ll_fg_used(), used); +} + +ZTEST(userspace_ll_fast_get, reuse_without_stale_data) +{ + size_t used = zephyr_ll_fg_used(); + + run_fg_case(FG_GET_PUT_A_THEN_B, true, false); + zassert_equal(fg_ptr[1], fg_ptr[0], "freed copy not reused"); + zassert_equal(zephyr_ll_fg_used(), used); +} + +ZTEST(userspace_ll_fast_get, copy_read_only) +{ + size_t used = zephyr_ll_fg_used(); + + run_fg_case(FG_WRITE_COPY, true, true); + zassert_mem_equal(fg_ptr[0], data_a, DATA_SIZE); + + /* the faulting thread kept its reference */ + fast_put_ll(fg_ptr[0]); + zassert_equal(zephyr_ll_fg_used(), used); +} + +ZTEST(userspace_ll_fast_get, caller_checks) +{ + size_t used = zephyr_ll_fg_used(); + + /* an LL domain thread that is not the LL or IPC user thread */ + if (IS_ENABLED(CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK)) + run_fg_case(FG_GET_SHARED, false, true); + /* memory that the caller cannot read */ + run_fg_case(FG_GET_KERNEL_DATA, true, true); + + zassert_equal(zephyr_ll_fg_used(), used); +} + +ZTEST(userspace_ll_fast_get, mod_fast_get_falls_back) +{ + size_t used = zephyr_ll_fg_used(); + int n; + + memset(&fake_mod, 0, sizeof(fake_mod)); + memset(&fake_dev, 0, sizeof(fake_dev)); + fake_mod.dev = &fake_dev; + fake_dev.mod = &fake_mod; + fake_mod.priv.resources.alloc = &fake_alloc; + fake_dev.ipc_config.proc_domain = COMP_PROCESSING_DOMAIN_LL; + fake_alloc.heap = zephyr_ll_user_heap(); + fake_alloc.vreg = NULL; + mod_resource_init(&fake_mod); + + run_fg_case(FG_MOD_GET, true, false); + zassert_true(zephyr_ll_fg_contains(fg_ptr[0], DATA_SIZE)); + run_fg_case(FG_MOD_PUT, true, false); + zassert_equal(zephyr_ll_fg_used(), used); + + /* fill the area */ + for (n = 0; n < FG_MAX_BLOCKS; n++) { + fg_blocks[n] = zephyr_ll_fg_alloc(256); + if (!fg_blocks[n]) + break; + } + zassert_true(n < FG_MAX_BLOCKS, "fast_get area not full"); + + run_fg_case(FG_MOD_GET, true, false); + zassert_false(zephyr_ll_fg_contains(fg_ptr[0], 1), "copy in the full area"); + run_fg_case(FG_MOD_PUT, true, false); + + while (n--) + zephyr_ll_fg_free(fg_blocks[n], 256); + zassert_equal(zephyr_ll_fg_used(), used); + + run_fg_case(FG_MOD_FREE_ALL, true, false); +} + +static bool ll_fast_get_predicate(const void *state) +{ + return CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE && sof_boot_test_standalone_predicate(state); +} + +ZTEST_SUITE(userspace_ll_fast_get, ll_fast_get_predicate, ll_fast_get_setup, + NULL, NULL, ll_user_heap_teardown); + +#endif /* HAVE_LL_FAST_GET_COPIES */ + + /** * SOF main has booted up and IPC handling is stopped. * Run test suites with ztest_run_all. @@ -307,6 +543,9 @@ ZTEST_SUITE(userspace_ll_user_heap, sof_boot_test_standalone_predicate, ll_user_ static int run_tests(void) { ztest_run_test_suite(userspace_ll_user_heap, false, 1, 1, NULL); +#if HAVE_LL_FAST_GET_COPIES && CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE + ztest_run_test_suite(userspace_ll_fast_get, false, 1, 1, NULL); +#endif return 0; } From 2ddbd332f5d360c23418c595ba686d5b6a13fd35 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Mon, 23 Nov 2026 15:21:44 +0200 Subject: [PATCH 5/5] userspace: doc: describe the LL fast_get area Describe the fast_get area of the LL memory region, how kernel code writes the shared copies of the LL modules and keeps their state, and the private copy when the area is full. Add the fast_get_ll() and fast_put_ll() system calls to the syscall map, and note where the copies of DP modules are. Signed-off-by: Kai Vehmanen --- src/include/sof/userspace/README.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/include/sof/userspace/README.md b/src/include/sof/userspace/README.md index 529e929bf155..5a53260dd034 100644 --- a/src/include/sof/userspace/README.md +++ b/src/include/sof/userspace/README.md @@ -124,6 +124,7 @@ syscall by subsystem: | Scheduling | `zephyr_ll_task_sem_alloc`, `zephyr_ll_task_sem_free`, `scheduler_dp_internal_free`, `scheduler_dp_ll_tick`, `scheduler_dp_kick` | `sof/schedule/ll_schedule_domain.h`, `sof/schedule/dp_schedule.h` | scattered: `kernel/schedule/zephyr_ll-kernel.c`, `kernel/schedule/zephyr_dp_schedule*-kernel.c` | | Debug | `debug_stream_slot_send_record` | `user/debug_stream_slot.h` | `kernel/debug/debug_stream/debug_stream_slot.c` | | KPB | `kpb_notifier_schedule` | `sof/audio/kpb.h` | `zephyr/syscall/kpb.c` / `audio/kpb.c` | +| fast_get | `fast_get_ll`, `fast_put_ll` | `sof/lib/fast-get.h` | scattered: `zephyr/lib/fast-get.c` | ### Allowed callers @@ -156,8 +157,9 @@ syscalls or to vregion, DMA, DAI or scheduler syscalls. Modules call the module resource functions without the `dp` prefix (`mod_alloc_ext()`, `mod_free()`, `mod_fast_get()` and so on), which are plain functions. For an LL module in the LL or IPC user thread they run in user mode, on the module's resource pool and the LL user -heap, under a `sys_mutex` in LL memory. `mod_fast_get()` then makes a private copy, as the -shared copies are kernel state. For DP modules, and in kernel code, they call the `mod_dp_*` +heap, under a `sys_mutex` in LL memory. `mod_fast_get()` then gets a shared read-only copy +with the `fast_get_ll()` syscall, see below, or makes a private copy when the fast_get area is +full. For DP modules, and in kernel code, they call the `mod_dp_*` syscalls. Those validators reject LL modules and DP modules whose allocation context has no live vregion, and pin the vregion during the call. They still read the context from DP memory, so a DP thread that rewrites it during the call is not stopped. @@ -186,6 +188,7 @@ With `CONFIG_SOF_USERSPACE_LL`, the memory of the LL and IPC user threads is one | Part | Mapping in the LL memory domain | Allocator | |------|---------------------------------|-----------| | Kernel RO area (`CONFIG_SOF_ZEPHYR_LL_KAREA_RO_SIZE`) | read-only | `zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RO, ...)` | +| fast_get area (`CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE`) | read-only, cached alias only | `zephyr_ll_fg_alloc()`, `zephyr_ll_fg_free()` | | Kernel RW area (`CONFIG_SOF_ZEPHYR_LL_KAREA_RW_SIZE`) | read-write | `zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RW, ...)` | | LL user heap (the rest) | read-write | `sof_heap_alloc(sof_sys_user_heap_get(), ...)`, in user mode only | @@ -237,10 +240,21 @@ heap. A user thread that corrupts the heap can only make LL and IPC user threads `zephyr_ll_user_heap()` returns a kernel-only `struct k_heap` that is never initialised: its address only selects the LL user heap. -Large `fast_get()` copies for DP modules are taken from `vpage_alloc()` instead of the heap of -the module allocation context, which is the LL user heap, and mapped read-only into the domain -of each user. Kernel code still follows some pointers stored in user-writable memory, for -example `task->sch` and the domain `ops`. +The fast_get area holds the `fast_get()` copies of the LL modules, such as SRC coefficients. +LL and IPC user threads get them with the `fast_get_ll()` and `fast_put_ll()` syscalls, and all +users of the same data share a copy. The allocator is a bitmap of 256-byte blocks in kernel +memory, and the entries of the copies are in kernel memory too, marked as LL copies, which +`fast_get()` and `fast_put()` never use. Only the cached alias of the area is mapped into the +LL memory domain, so kernel code writes a copy through the uncached alias, also in the +syscall, and invalidates the cached alias on the calling core at every get, as the core may +still hold lines of an earlier copy at the same address. LL threads cannot change a shared +copy. A user thread can release copies that other LL modules use, which only affects LL +modules. + +DP modules keep their copies in their own memory: small copies in the module vregion, and +large copies from `vpage_alloc()` instead of the heap of the module allocation context, which +is the LL user heap, mapped read-only into the domain of each user. Kernel code still follows +some pointers stored in user-writable memory, for example `task->sch` and the domain `ops`. ## Memory-partition markers