From: Michalis Pappas Date: Mon, 14 Apr 2025 19:32:50 +0000 (+0200) Subject: lib/posix-process: Add uk_posix_process_create_pthread() X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=361751a7ea2a53b9679755c87ae1bc30b97d13a4;p=unikraft%2Funikraft.git lib/posix-process: Add uk_posix_process_create_pthread() Provide a function to attach a thread to the current process. This is intended to be exclusively used by app-elfloader when configured with initrd, in order to assign the container thread it creates to the init process. Move uk_process_kill() to the internal API, and rename to avoid using the naming convention of public functions, and deprecate the unused uk_posix_process_create(). Checkpatch-Ignore: REPEATED_WORD Signed-off-by: Michalis Pappas Approved-by: Andrei Tatar Reviewed-by: Sergiu Moga Reviewed-by: Andrei Tatar GitHub-Closes: #1627 --- diff --git a/lib/posix-process/exportsyms.uk b/lib/posix-process/exportsyms.uk index f24e8b910..d4d6f6fc9 100644 --- a/lib/posix-process/exportsyms.uk +++ b/lib/posix-process/exportsyms.uk @@ -39,8 +39,6 @@ uk_sys_gettid gettid exit exit_group -uk_posix_process_create -uk_posix_process_kill clone vfork kill @@ -54,3 +52,4 @@ sigismember sigprocmask tgkill signalfd +uk_posix_process_create_pthread diff --git a/lib/posix-process/include/uk/process.h b/lib/posix-process/include/uk/process.h index d7968166f..df322fdb4 100644 --- a/lib/posix-process/include/uk/process.h +++ b/lib/posix-process/include/uk/process.h @@ -155,13 +155,6 @@ pid_t uk_sys_gettid(void); pid_t uk_sys_getppid(void); pid_t uk_sys_getpid(void); -#if CONFIG_LIBPOSIX_PROCESS_MULTIPROCESS -int uk_posix_process_create(struct uk_alloc *a, - struct uk_thread *thread, - struct uk_thread *parent); -void uk_posix_process_kill(struct uk_thread *thread); -#endif /* CONFIG_LIBPOSIX_PROCESS_MULTIPROCESS */ - #if CONFIG_LIBPOSIX_PROCESS_MULTITHREADING typedef int (*uk_posix_clone_init_func_t)(const struct clone_args *cl_args, size_t cl_args_len, @@ -222,6 +215,15 @@ struct uk_posix_clonetab_entry { _UK_POSIX_CLONETAB_ENTRY(flags_mask, presence_only, init_fn, term_fn, \ UK_PRIO_LATEST) +/* Creates a pthread and attaches it to the current process + * + * DO NOT USE. This is only necessary when loading an ELF via initrd, + * where we want to POSIXise the kernel thread, as initrd does not + * support paths and therefore we cannot execve(). For more details + * see the implementation of app-elfloader. + */ +int uk_posix_process_create_pthread(struct uk_thread *thread); + #endif /* CONFIG_LIBPOSIX_PROCESS_MULTITHREADING */ #if CONFIG_LIBPOSIX_PROCESS_EXECVE diff --git a/lib/posix-process/process.c b/lib/posix-process/process.c index 0e421b053..e03d4ab30 100644 --- a/lib/posix-process/process.c +++ b/lib/posix-process/process.c @@ -121,8 +121,8 @@ static void release_tid(pid_t tid) } /* Allocate a thread for a process */ -static struct posix_thread *pprocess_create_pthread( - struct posix_process *pprocess, struct uk_thread *th) +struct posix_thread *pprocess_create_pthread(struct posix_process *pprocess, + struct uk_thread *th) { struct posix_thread *pthread; struct uk_alloc *a; @@ -214,12 +214,26 @@ static void pprocess_release_pthread(struct posix_thread *pthread) uk_free(pthread->_a, pthread); } +int uk_posix_process_create_pthread(struct uk_thread *thread) +{ + struct posix_process *pprocess; + struct posix_thread *pthread; + + pprocess = uk_pprocess_current(); + UK_ASSERT(pprocess); + + pthread = pprocess_create_pthread(pprocess, thread); + if (unlikely(PTRISERR(pthread))) + return PTR2ERR(pthread); + + return 0; +} static void pprocess_release(struct posix_process *pprocess); /* Create a new posix process for a given thread */ -int uk_posix_process_create(struct uk_alloc *a, - struct uk_thread *thread, - struct uk_thread *parent) +int pprocess_create(struct uk_alloc *a, + struct uk_thread *thread, + struct uk_thread *parent) { struct posix_thread *parent_pthread = NULL; struct posix_process *parent_pprocess = NULL; @@ -422,7 +436,7 @@ void pprocess_kill_siblings(struct uk_thread *thread) } } -static void pprocess_kill(struct posix_process *pprocess) +void pprocess_kill(struct posix_process *pprocess) { struct posix_thread *pthread, *pthreadn, *pthread_self = NULL; @@ -469,20 +483,6 @@ static void pprocess_kill(struct posix_process *pprocess) } } -void uk_posix_process_kill(struct uk_thread *thread) -{ - struct posix_thread **pthread; - struct posix_process *pprocess; - - pthread = &uk_thread_uktls_var(thread, pthread_self); - - UK_ASSERT(*pthread); - UK_ASSERT((*pthread)->process); - - pprocess = (*pthread)->process; - pprocess_kill(pprocess); -} - static int posix_process_init(struct uk_init_ctx *ictx) { struct uk_thread *t; @@ -502,47 +502,11 @@ static int posix_process_init(struct uk_init_ctx *ictx) } /* Create a POSIX process without parent ("init" process) */ - return uk_posix_process_create(uk_alloc_get_default(), t, NULL); + return pprocess_create(uk_alloc_get_default(), t, NULL); } uk_late_initcall(posix_process_init, 0x0); -/* Thread initialization: Assign posix thread only if parent is part of a - * process - */ -static int posix_thread_init(struct uk_thread *child, struct uk_thread *parent) -{ - struct posix_thread *parent_pthread = NULL; - struct posix_thread *pthread; - - if (parent) { - parent_pthread = uk_thread_uktls_var(parent, - pthread_self); - } - if (!parent_pthread) { - /* parent has no posix thread, do not setup one for the child */ - uk_pr_debug("thread %p (%s): Parent %p (%s) without process context, skipping...\n", - child, child->name, parent, - parent ? parent->name : ""); - pthread_self = NULL; - return 0; - } - - UK_ASSERT(parent_pthread->process); - - pthread = pprocess_create_pthread(parent_pthread->process, - child); - if (PTRISERR(pthread)) - return PTR2ERR(pthread); - - pthread_self = pthread; - - uk_pr_debug("thread %p (%s): New thread with TID: %d (PID: %d)\n", - child, child->name, (int) pthread->tid, - (int) pthread->process->pid); - return 0; -} - /* Thread release: Release TID and posix_thread */ static void posix_thread_fini(struct uk_thread *child) { @@ -684,7 +648,7 @@ UK_LLSYSCALL_R_DEFINE(int, exit, int, status) static int pprocess_exit(int status __unused) { - uk_posix_process_kill(uk_thread_current()); /* won't return */ + pprocess_kill(uk_pprocess_current()); /* won't return */ UK_CRASH("sys_exit_group() unexpectedly returned\n"); return -EFAULT; } diff --git a/lib/posix-process/process.h b/lib/posix-process/process.h index 6b15de901..5c060ea21 100644 --- a/lib/posix-process/process.h +++ b/lib/posix-process/process.h @@ -120,8 +120,30 @@ struct posix_process *tid2pprocess(pid_t tid); pid_t ukthread2tid(struct uk_thread *thread); pid_t ukthread2pid(struct uk_thread *thread); +void pprocess_kill(struct posix_process *pprocess); + void pprocess_kill_siblings(struct uk_thread *thread); +/** + * INTERNAL. Create pthread + * + * @param pprocess process to assign thread to + * @param thread backing uk_thread to create pthread from + * @return pthread on success or negative value on failure + */ +struct posix_thread *pprocess_create_pthread(struct posix_process *pprocess, + struct uk_thread *thread); +/** + * INTERNAL. Create process + * + * @param alloc allocator to assign process + * @param thread backing uk_thread to create process from + * @param parent parent processs + * @return process on success or negative value on failure + */ +int pprocess_create(struct uk_alloc *a, struct uk_thread *thread, + struct uk_thread *parent); + int uk_clone(struct clone_args *cl_args, size_t cl_args_len, struct ukarch_execenv *execenv); #endif /* CONFIG_LIBPOSIX_PROCESS_MULTITHREADING */ diff --git a/lib/posix-process/signal/deliver.c b/lib/posix-process/signal/deliver.c index 4babee8f6..67f022691 100644 --- a/lib/posix-process/signal/deliver.c +++ b/lib/posix-process/signal/deliver.c @@ -21,7 +21,7 @@ void pprocess_signal_call_handler_with_stack(int signum, siginfo_t *si, static void uk_sigact_term(int __unused sig) { uk_pr_warn("tid %d terminated by signal\n", uk_sys_gettid()); - uk_posix_process_kill(uk_thread_current()); + pprocess_kill(uk_pprocess_current()); } static void uk_sigact_ign(int __unused sig)