Repository navigation
ipc: Safely aborting thread during SOF_IPC4_MOD_INIT_INSTANCE if module crashes #11485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9ec6da1
1e8d83b
914cb10
8ab6fc9
48f09a0
2195a3d
33556c1
7e6acbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1050,6 +1050,64 @@ __cold int ipc4_process_large_config_set(struct ipc4_message_request *ipc4) | |
| } | ||
|
|
||
| #ifdef CONFIG_SOF_USERSPACE_LL | ||
| /** | ||
| * @brief Helper thread entry performing SOF_IPC4_MOD_INIT_INSTANCE. | ||
| * | ||
| * Runs in user space on the same core as the IPC user thread that spawned | ||
| * it. The IPC message words and driver pointer are taken from the shared | ||
| * @c ipc_user context (@p p1); the result is stored in | ||
| * @c ipc_user->init_result. | ||
| * | ||
| * @param p1 struct ipc_user pointer | ||
| * @param p2 unused | ||
| * @param p3 unused | ||
| */ | ||
| static void ipc_user_init_instance_fn(void *p1, void *p2, void *p3) | ||
| { | ||
| struct ipc_user *ipc_user = p1; | ||
| struct ipc4_message_request msg; | ||
| const struct comp_driver *drv = ipc_user->init_drv; | ||
| struct comp_dev *dev; | ||
| int result; | ||
|
|
||
| ARG_UNUSED(p2); | ||
| ARG_UNUSED(p3); | ||
|
|
||
| msg.primary.dat = ipc_user->ipc_msg_pri; | ||
| msg.extension.dat = ipc_user->ipc_msg_ext; | ||
|
|
||
| /* User thread creates the component: drv->ops.create() runs in | ||
| * user-space so untrusted module code does not execute with kernel | ||
| * privileges. init_drv is the original kernel pointer. | ||
| */ | ||
| tr_info(&ipc_tr, "INIT_INSTANCE: driver received %p", drv); | ||
| ipc_user->init_drv = NULL; | ||
| if (!drv) { | ||
| tr_err(&ipc_tr, "INIT_INSTANCE: driver missing"); | ||
| ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; | ||
| return; | ||
| } | ||
|
|
||
| dev = comp_new_ipc4_user(&msg, drv); | ||
| if (!dev) { | ||
| tr_err(&ipc_tr, "INIT_INSTANCE: component creation failed"); | ||
| ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; | ||
| return; | ||
| } | ||
|
|
||
| result = ipc4_add_comp_dev(dev); | ||
| if (result != IPC4_SUCCESS) { | ||
| tr_err(&ipc_tr, "INIT_INSTANCE: component registration failed %d", result); | ||
| ipc_user->init_result = result; | ||
| return; | ||
| } | ||
|
|
||
| comp_update_ibs_obs_cpc(dev); | ||
|
|
||
| ipc_user->init_result = 0; | ||
| tr_info(&ipc_tr, "INIT_INSTANCE: completed"); | ||
| } | ||
|
|
||
| /* | ||
| * IPC4 implementation of the user-thread dispatch hook. Runs in the | ||
| * user-space IPC thread; reconstructs the IPC4 message from the forwarded | ||
|
|
@@ -1064,6 +1122,8 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user) | |
| msg.primary.dat = ipc_user->ipc_msg_pri; | ||
| msg.extension.dat = ipc_user->ipc_msg_ext; | ||
|
|
||
| tr_info(&ipc_tr, "user dispatch: message primary type %#x", msg.primary.r.type); | ||
|
|
||
| ipc_user->reply_ext = 0; | ||
|
|
||
| if (msg.primary.r.msg_tgt == SOF_IPC4_MESSAGE_TARGET_MODULE_MSG) { | ||
|
|
@@ -1097,34 +1157,45 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user) | |
| break; | ||
| } | ||
| case SOF_IPC4_MOD_INIT_INSTANCE: { | ||
| /* User thread creates the component — | ||
| * drv->ops.create() runs in user-space so untrusted | ||
| * module code does not execute with kernel privileges. | ||
| * | ||
| * init_drv = original kernel pointer | ||
| /* | ||
| * Run the instance creation in a helper thread pinned to | ||
| * the same core as this IPC user thread, then join it. | ||
| * Message words and init_drv are already in ipc_user. | ||
| */ | ||
| const struct comp_driver *drv = ipc_user->init_drv; | ||
| unsigned int core = ipc_user->cmd_core; | ||
| struct k_thread *thread; | ||
|
|
||
| ipc_user->init_drv = NULL; | ||
| if (!drv) { | ||
| if (core >= CONFIG_CORE_COUNT || !ipc_user->init_thread[core]) { | ||
| result = IPC4_MOD_NOT_INITIALIZED; | ||
| break; | ||
| } | ||
|
|
||
| struct comp_dev *dev = comp_new_ipc4_user(&msg, drv); | ||
|
|
||
| if (!dev) { | ||
| thread = ipc_user->init_thread[core]; | ||
| ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED; | ||
|
|
||
| k_thread_create(thread, ipc_user->init_stack[core], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, need to think about this. This does clearly isolate the places where we can handle crashes, but given the IPC code is already complex, wrapping each call with jump to another thread, makes this even more complex. I wonder should we take another look can we somehow make the IPC kernel-user forwarder function reliably detect if the thread it talks to, has crashed. We use semaphores now? Could we just use a timeout and report an error back that connection was lost fo IPC user thread. |
||
| CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE, | ||
| ipc_user_init_instance_fn, ipc_user, NULL, NULL, | ||
| -1, K_USER | K_INHERIT_PERMS, K_FOREVER); | ||
| result = sof_thread_cpu_pin(thread, core); | ||
| if (result) { | ||
| tr_err(&ipc_tr, "INIT_INSTANCE: cpu pin failed %d", result); | ||
| /* never started: abort so the objects can be reused */ | ||
| k_thread_abort(thread); | ||
| result = IPC4_MOD_NOT_INITIALIZED; | ||
| break; | ||
| } | ||
| k_thread_name_set(thread, "ipc_init"); | ||
| k_thread_start(thread); | ||
|
|
||
| result = ipc4_add_comp_dev(dev); | ||
| if (result != IPC4_SUCCESS) | ||
| result = k_thread_join(thread, K_FOREVER); | ||
| if (result) { | ||
| tr_err(&ipc_tr, "INIT_INSTANCE: join failed %d", result); | ||
| result = IPC4_MOD_NOT_INITIALIZED; | ||
| break; | ||
| } | ||
|
|
||
| comp_update_ibs_obs_cpc(dev); | ||
|
|
||
| result = 0; | ||
| result = ipc_user->init_result; | ||
| break; | ||
| } | ||
| case SOF_IPC4_MOD_DELETE_INSTANCE: { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit worried about the overhead of having additional threads. If we need a monitor thread for each thread, that is sizable memory usage and consumes precious local RAM we have. Ok for testing, but I wonder if we could use some kernel workqueue to monitor threads that should be running, or just have a sinlge watchdog thread.