]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-process: Add uk_posix_process_create_pthread()
authorMichalis Pappas <michalis@unikraft.io>
Mon, 14 Apr 2025 19:32:50 +0000 (21:32 +0200)
committerUnikraft Bot <monkey@unikraft.io>
Wed, 30 Apr 2025 09:42:51 +0000 (09:42 +0000)
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 <michalis@unikraft.io>
Approved-by: Andrei Tatar <andrei@unikraft.io>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Andrei Tatar <andrei@unikraft.io>
GitHub-Closes: #1627

lib/posix-process/exportsyms.uk
lib/posix-process/include/uk/process.h
lib/posix-process/process.c
lib/posix-process/process.h
lib/posix-process/signal/deliver.c

index f24e8b9100bc57464382fdbcd5735a91f2ac0276..d4d6f6fc94e3e98ff29b25801361e53703df3f45 100644 (file)
@@ -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
index d7968166ff7fb8771e55decd4fb1159504a4d869..df322fdb409690427f1b13853f7ac747143b9522 100644 (file)
@@ -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
index 0e421b0533d7b2aa797d227ef49619ff4b62a492..e03d4ab30b8fa241489af290bf70ecddeb71aa15 100644 (file)
@@ -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 : "<n/a>");
-               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;
 }
index 6b15de901c91c2821ded20a00b17b091a159f376..5c060ea21ab7549320e4ecd45f0bf48e296807e0 100644 (file)
@@ -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 */
index 4babee8f6b3c289c4157d6dfe558ce7360c8d1b6..67f022691e07c82bd14f7c41c491c60f5e000572 100644 (file)
@@ -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)