From 4324693f80a00ff31812578c8c275169c26e6df8 Mon Sep 17 00:00:00 2001 From: Serhiy Katsyuba Date: Tue, 3 Nov 2026 12:32:40 +0100 Subject: [PATCH 1/3] audio: kpb: make KPB service usable from user space Extend the KPB service to support, in addition to the kernel-only notifier interface, the client queue (CONFIG_KPB_CLI_Q), which can also be used from user space. Replace the k_queue with a statically defined k_msgq that carries struct kpb_client by value, and replace kpb_notifier_init() and kpb_notifier_schedule() with a single kpb_notifier_schedule(id, drain_req) syscall taking only scalar arguments. No pointers are passed from user space to the kernel. As before, only a single KPB instance is supported. With the client queue, draining setup runs in the client queue thread instead of the LL thread, so lock the scheduler around kpb_init_draining() to keep kpb_copy() from preempting it. The thread is pinned to the KPB core, so this is sufficient. Signed-off-by: Serhiy Katsyuba --- src/audio/kpb.c | 72 ++++++++++--------- src/audio/module_adapter/Kconfig | 2 +- .../library/kpb_service_bridge.c | 34 +++++++-- src/include/sof/audio/kpb.h | 19 +++-- src/include/sof/userspace/README.md | 4 +- zephyr/CMakeLists.txt | 3 + zephyr/syscall/kpb.c | 24 +++++++ 7 files changed, 112 insertions(+), 46 deletions(-) create mode 100644 zephyr/syscall/kpb.c diff --git a/src/audio/kpb.c b/src/audio/kpb.c index b2542d1f1f54..e53fc2137123 100644 --- a/src/audio/kpb.c +++ b/src/audio/kpb.c @@ -121,7 +121,14 @@ struct comp_data { #if CONFIG_KPB_CLI_Q #include -static struct k_queue *kpb_q; +/* + * Only a single KPB instance is currently supported: the queue, thread and + * dev below are global. Requests are copied by value, so user space never + * passes pointers to the kernel. + */ +K_MSGQ_DEFINE(kpb_cli_msgq, sizeof(struct kpb_client), KPB_MAX_NO_OF_CLIENTS, + __alignof__(struct kpb_client)); +static struct comp_dev *kpb_cli_q_dev; static struct k_thread kpb_thread; K_KERNEL_STACK_DEFINE(kpb_stack, 4096); #endif @@ -481,40 +488,34 @@ static int kpb_params(struct comp_dev *dev, struct sof_ipc_stream_params *params #if CONFIG_KPB_CLI_Q static void kpb_thread_fn(void *p1, void *p2, void *p3) { - struct k_queue *q = p1; + struct comp_dev *dev = p1; + struct kpb_client cli; for (;;) { - struct kpb_client *cli = k_queue_get(q, K_FOREVER); - - kpb_init_draining(cli->dev, cli); + k_msgq_get(&kpb_cli_msgq, &cli, K_FOREVER); + /* keep the LL thread (kpb_copy()) from preempting draining setup */ + k_sched_lock(); + kpb_init_draining(dev, &cli); + k_sched_unlock(); } } -void kpb_notifier_schedule(struct kpb_client *cli) +int z_impl_kpb_notifier_schedule(uint32_t id, uint32_t drain_req) { - k_queue_alloc_append(cli->queue, cli); -} -EXPORT_SYMBOL(kpb_notifier_schedule); + struct kpb_client cli = { + .id = id, + .drain_req = drain_req, + }; -void z_impl_kpb_notifier_init(struct comp_dev *dev, struct kpb_client *cli) -{ - comp_dbg(dev, "adding new client to %p", kpb_q); - k_thread_access_grant(k_current_get(), kpb_q); - cli->queue = kpb_q; - cli->dev = dev; -} + if (id >= KPB_MAX_NO_OF_CLIENTS || drain_req > KPB_MAX_DRAINING_REQ) + return -EINVAL; -#include -#include -void z_vrfy_kpb_notifier_init(struct comp_dev *dev, struct kpb_client *cli) -{ - SOF_SYSCALL_CALLER_CHECK(SOF_SYSUSER_INFRA); + if (!kpb_cli_q_dev) + return -ENODEV; - K_OOPS(K_SYSCALL_MEMORY_WRITE(dev, sizeof(*dev))); - K_OOPS(K_SYSCALL_MEMORY_WRITE(cli, sizeof(*cli))); - z_impl_kpb_notifier_init(dev, cli); + return k_msgq_put(&kpb_cli_msgq, &cli, K_NO_WAIT); } -#include +EXPORT_SYMBOL(z_impl_kpb_notifier_schedule); #endif /* @@ -556,6 +557,13 @@ __cold static struct comp_dev *kpb_new(const struct comp_driver *drv, return NULL; } +#if CONFIG_KPB_CLI_Q + if (kpb_cli_q_dev) { + comp_cl_err(&comp_kpb, "only one KPB instance is supported with KPB_CLI_Q"); + return NULL; + } +#endif + dev = comp_alloc(drv, sizeof(*dev)); if (!dev) return NULL; @@ -613,18 +621,13 @@ __cold static struct comp_dev *kpb_new(const struct comp_driver *drv, #endif #if CONFIG_KPB_CLI_Q - kpb_q = k_object_alloc(K_OBJ_QUEUE); - if (!kpb_q) { - comp_free_device(dev); - return NULL; - } - - k_queue_init(kpb_q); - k_thread_create(&kpb_thread, kpb_stack, 4096, kpb_thread_fn, kpb_q, NULL, NULL, + k_msgq_purge(&kpb_cli_msgq); + k_thread_create(&kpb_thread, kpb_stack, 4096, kpb_thread_fn, dev, NULL, NULL, 1, 0, K_FOREVER); k_thread_cpu_pin(&kpb_thread, cpu_get_id()); k_thread_start(&kpb_thread); + kpb_cli_q_dev = dev; #endif return dev; @@ -775,8 +778,9 @@ __cold static void kpb_free(struct comp_dev *dev) comp_info(dev, "entry"); #if CONFIG_KPB_CLI_Q + kpb_cli_q_dev = NULL; k_thread_abort(&kpb_thread); - k_object_free(kpb_q); + k_msgq_purge(&kpb_cli_msgq); #elif CONFIG_AMS /* Unregister KPB as AMS consumer */ int ret; diff --git a/src/audio/module_adapter/Kconfig b/src/audio/module_adapter/Kconfig index f8e40a8cda15..04026dcb5b14 100644 --- a/src/audio/module_adapter/Kconfig +++ b/src/audio/module_adapter/Kconfig @@ -226,7 +226,7 @@ endif # Cadence config INTEL_MODULES bool "Intel modules" depends on LIBRARY_MANAGER - select KPB_NOTIFIER if COMP_KPB + select KPB_NOTIFIER if COMP_KPB && !KPB_CLI_Q help Select for a Intel modules API implementation. This will cause module adapter component to include IADK module diff --git a/src/audio/module_adapter/library/kpb_service_bridge.c b/src/audio/module_adapter/library/kpb_service_bridge.c index 574ce73431ce..b2d7c2f717f1 100644 --- a/src/audio/module_adapter/library/kpb_service_bridge.c +++ b/src/audio/module_adapter/library/kpb_service_bridge.c @@ -7,16 +7,21 @@ * do not provide extern "C" blocks; C++ is used as a wrapper. */ +#include #include #include #include #include #include #include +#if !CONFIG_KPB_CLI_Q #include +#endif struct kpb_service_client { +#if !CONFIG_KPB_CLI_Q struct kpb_event_data event; +#endif struct kpb_client client; size_t current_history_depth; size_t max_history_depth; @@ -37,15 +42,37 @@ static bool kpb_service_client_is_valid(const struct kpb_service_client *client) return false; } -static void kpb_service_send_event(struct kpb_service_client *client, - enum kpb_event event_id) +#if CONFIG_KPB_CLI_Q +static AdspErrorCode kpb_service_send_event(struct kpb_service_client *client, + enum kpb_event event_id) +{ + /* KPB draining does not depend on registration, so only draining is queued */ + if (event_id != KPB_EVENT_BEGIN_DRAINING) + return ADSP_NO_ERROR; + + switch (kpb_notifier_schedule(client->client.id, client->client.drain_req)) { + case 0: + return ADSP_NO_ERROR; + case -ENODEV: + return ADSP_SERVICE_UNAVAILABLE; + case -ENOMSG: + return ADSP_BUSY_RESOURCE; + default: + return ADSP_INVALID_PARAMETERS; + } +} +#else +static AdspErrorCode kpb_service_send_event(struct kpb_service_client *client, + enum kpb_event event_id) { client->event.event_id = event_id; client->event.client_data = &client->client; notifier_event(client, NOTIFIER_ID_KPB_CLIENT_EVT, NOTIFIER_TARGET_CORE_ALL_MASK, &client->event, sizeof(client->event)); + return ADSP_NO_ERROR; } +#endif AdspErrorCode kpb_service_register_client(size_t current_history_depth, size_t max_history_depth, uint32_t output_pin, @@ -86,8 +113,7 @@ AdspErrorCode kpb_service_signal_detection(struct kpb_service_client *client, client->client.drain_req = MIN(client->current_history_depth + phrase_length, client->max_history_depth); - kpb_service_send_event(client, KPB_EVENT_BEGIN_DRAINING); - return ADSP_NO_ERROR; + return kpb_service_send_event(client, KPB_EVENT_BEGIN_DRAINING); } AdspErrorCode kpb_service_unregister_client(struct kpb_service_client *client) diff --git a/src/include/sof/audio/kpb.h b/src/include/sof/audio/kpb.h index fcfd944897b8..74eeae964539 100644 --- a/src/include/sof/audio/kpb.h +++ b/src/include/sof/audio/kpb.h @@ -118,8 +118,6 @@ enum kpb_client_state { }; struct kpb_client { - struct comp_dev *dev; - struct k_queue *queue; uint8_t id; /**< id associated with output sink */ uint32_t drain_req; /**< normalized value of buffered bytes */ enum kpb_client_state state; /**< current state of a client */ @@ -261,13 +259,24 @@ struct kpb_fmt_dev_list { }; #if CONFIG_KPB_CLI_Q -void kpb_notifier_schedule(struct kpb_client *cli); #if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) #include -__syscall void kpb_notifier_init(struct comp_dev *dev, struct kpb_client *cli); +/** + * \brief Request draining of the KPB history buffer via the client queue. + * + * Callable from user space. The request is handled asynchronously by the + * KPB client queue thread. + * + * \param[in] id Client ID, i.e. the KPB output pin. + * \param[in] drain_req Requested draining length in milliseconds. + * \return 0 on success, -EINVAL on invalid arguments, -ENODEV if no KPB + * instance exists, -ENOMSG if the queue is full. + */ +__syscall int kpb_notifier_schedule(uint32_t id, uint32_t drain_req); #include #else -#define kpb_notifier_init z_impl_kpb_notifier_init +int z_impl_kpb_notifier_schedule(uint32_t id, uint32_t drain_req); +#define kpb_notifier_schedule z_impl_kpb_notifier_schedule #endif #endif diff --git a/src/include/sof/userspace/README.md b/src/include/sof/userspace/README.md index f73bc1b53e12..a2dc6743c1a1 100644 --- a/src/include/sof/userspace/README.md +++ b/src/include/sof/userspace/README.md @@ -124,7 +124,7 @@ syscall by subsystem: | Library manager | `lib_manager_free_module` | `sof/lib_manager.h` | scattered: `kernel/library_manager/lib_manager.c` | | Scheduling | `zephyr_ll_task_sem_alloc`, `zephyr_ll_task_sem_free`, `scheduler_dp_internal_free`, `scheduler_dp_ll_tick` | `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_init` | `sof/audio/kpb.h` | scattered: `audio/kpb.c` | +| KPB | `kpb_notifier_schedule` | `sof/audio/kpb.h` | `zephyr/syscall/kpb.c` / `audio/kpb.c` | ### Allowed callers @@ -137,7 +137,7 @@ from user-writable data such as `ipc_user->thread[]`. | Allowed callers | Syscalls | |-----------------|----------| -| Any user thread, including DP module threads | `cpu_get_id`, all module management syscalls (`mod_*`), `ipc_msg_send`, `ipc_msg_list_remove`, `send_resource_notif` | +| Any user thread, including DP module threads | `cpu_get_id`, all module management syscalls (`mod_*`), `ipc_msg_send`, `ipc_msg_list_remove`, `send_resource_notif`, `kpb_notifier_schedule` | | IPC user thread only | `ipc_compound_pre_start`, `ipc_compound_post_start`, `ipc_wait_for_compound_msg`, `lib_manager_free_module` | | LL user thread only | `scheduler_dp_ll_tick` | | LL or IPC user thread | all other SOF syscalls | diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 30f22bb659fb..026f64a8679b 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -612,6 +612,9 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE syscall/cpu.c) zephyr_library_sources_ifdef(CONFIG_SOF_USERSPACE_SYSCALL_CALLER_CHECK syscall/caller.c) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/audio/module_adapter/module/generic.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/audio/kpb.h) +if(CONFIG_USERSPACE AND CONFIG_KPB_CLI_Q) + zephyr_library_sources(syscall/kpb.c) +endif() zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/lib/fast-get.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/ipc/ipc_reply.h) zephyr_syscall_header(${SOF_SRC_PATH}/include/sof/schedule/dp_schedule.h) diff --git a/zephyr/syscall/kpb.c b/zephyr/syscall/kpb.c new file mode 100644 index 000000000000..11837baefcf2 --- /dev/null +++ b/zephyr/syscall/kpb.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright 2026 Intel Corporation. + +#include +#include +#include +#include + +/** + * \brief Userspace verification wrapper for kpb_notifier_schedule(). + * + * Only scalar arguments are passed, their ranges are checked by the + * implementation, so no memory access validation is required. + * + * \param[in] id Client ID, i.e. the KPB output pin. + * \param[in] drain_req Requested draining length in milliseconds. + * \return 0 on success, negative error code otherwise. + */ +static inline int z_vrfy_kpb_notifier_schedule(uint32_t id, uint32_t drain_req) +{ + return z_impl_kpb_notifier_schedule(id, drain_req); +} +#include From 5dac58983439f6494d21da2ec30d0425b59db7d1 Mon Sep 17 00:00:00 2001 From: Serhiy Katsyuba Date: Mon, 16 Nov 2026 11:34:22 +0100 Subject: [PATCH 2/3] audio: kpb: check sel_sink before starting a drain kpb_init_draining() pauses the selector sink component, but sel_sink is NULL before bind and after reset/unbind. As draining can potentially be requested at any time while the KPB instance exists, such a request would dereference a NULL sink. Treat the sink as not ready if sel_sink is missing. A missing host_sink is already handled as a notification-only configuration. Signed-off-by: Serhiy Katsyuba --- src/audio/kpb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/audio/kpb.c b/src/audio/kpb.c index e53fc2137123..92ac49ee0392 100644 --- a/src/audio/kpb.c +++ b/src/audio/kpb.c @@ -1808,7 +1808,9 @@ static void kpb_init_draining(struct comp_dev *dev, struct kpb_client *cli) } else if (cli->id > KPB_MAX_NO_OF_CLIENTS) { comp_err(dev, "wrong client id"); /* TODO: check also if client is registered */ - } else if (comp_buffer_get_sink_state(kpb->host_sink) != COMP_STATE_ACTIVE) { + /* sel_sink is NULL before bind and after reset/unbind */ + } else if (!kpb->sel_sink || + comp_buffer_get_sink_state(kpb->host_sink) != COMP_STATE_ACTIVE) { comp_err(dev, "sink not ready for draining"); } else if (kpb->hd.buffered < drain_req || cli->drain_req > KPB_MAX_DRAINING_REQ) { From 773aef35f37b30a04a34d8384ddc41a4b4d5b9bf Mon Sep 17 00:00:00 2001 From: Serhiy Katsyuba Date: Tue, 10 Nov 2026 13:53:00 +0100 Subject: [PATCH 3/3] audio: kpb: fix draining stuck on zero-length request A draining request of 0 ms, i.e. no history, only the real time stream, is accepted by kpb_init_draining(), which pauses the selector and moves KPB to KPB_STATE_DRAINING. However, kpb_draining_task() only picks up data buffered while draining and switches to KPB_STATE_HOST_COPY inside the drain_req > 0 block, so with a zero request it completes without ever leaving KPB_STATE_DRAINING, leaving KPB stuck with the detector paused and rejecting further requests. Move the drain completion handling out of that block so it also runs for a zero-length request. Signed-off-by: Serhiy Katsyuba --- src/audio/kpb.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/audio/kpb.c b/src/audio/kpb.c index 92ac49ee0392..4ec5d6131557 100644 --- a/src/audio/kpb.c +++ b/src/audio/kpb.c @@ -2096,28 +2096,29 @@ static enum task_state kpb_draining_task(void *arg) } else { draining_data->next_copy_time = 0; } + } - if (draining_data->drain_req == 0) { - /* We have finished draining of requested data however - * while we were draining real time stream could provided - * new data which needs to be copy to host. + /* also reached with a zero-length (no history) draining request */ + if (draining_data->drain_req == 0) { + /* We have finished draining of requested data however + * while we were draining real time stream could provided + * new data which needs to be copy to host. + */ + comp_cl_info(&comp_kpb, "kpb: update drain_req by %zu", + *rt_stream_update); + kpb_lock(kpb); + draining_data->drain_req += *rt_stream_update; + *rt_stream_update = 0; + if (!draining_data->drain_req && kpb->state == KPB_STATE_DRAINING) { + /* Draining is done. Now switch KPB to copy real time + * stream to client's sink. This state is called + * "draining on demand" + * Note! If KPB state changed during draining due to + * i.e reset request we should not change that state. */ - comp_cl_info(&comp_kpb, "kpb: update drain_req by %zu", - *rt_stream_update); - kpb_lock(kpb); - draining_data->drain_req += *rt_stream_update; - *rt_stream_update = 0; - if (!draining_data->drain_req && kpb->state == KPB_STATE_DRAINING) { - /* Draining is done. Now switch KPB to copy real time - * stream to client's sink. This state is called - * "draining on demand" - * Note! If KPB state changed during draining due to - * i.e reset request we should not change that state. - */ - kpb_change_state(kpb, KPB_STATE_HOST_COPY); - } - kpb_unlock(kpb); + kpb_change_state(kpb, KPB_STATE_HOST_COPY); } + kpb_unlock(kpb); } out: