]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-process: Correct MAX_PID config option
authorFlorin Postolache <florin.postolache.of@gmail.com>
Thu, 11 May 2023 09:44:58 +0000 (12:44 +0300)
committerUnikraft <monkey@unikraft.io>
Fri, 12 May 2023 08:19:04 +0000 (08:19 +0000)
This commit changes the tid_map size to reflect the kernel
reservation of the 0 TID/PID value. Before, the first thread
would set the 0 position in all used structures. Now, because
it will be already set, we need to make room for another.

Signed-off-by: Florin Postolache <florin.postolache.of@gmail.com>
Reviewed-by: Simon Kuenzer <simon@unikraft.io>
Reviewed-by: Andra Paraschiv <andra@unikraft.org>
Approved-by: Simon Kuenzer <simon@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #885

lib/posix-process/Config.uk
lib/posix-process/process.c

index 73301c86fbf609007f3dc2766438463ece796879..c291b4353a33c0decb24f8e0029696433022ce9e 100644 (file)
@@ -12,8 +12,8 @@ if LIBPOSIX_PROCESS
 if LIBPOSIX_PROCESS_PIDS
                config LIBPOSIX_PROCESS_MAX_PID
                int "Largest PID"
-               range 1 32768
-               default 32
+               range 1 32767
+               default 31
 
                config LIBPOSIX_PROCESS_INIT_PIDS
                bool "Assign PID during boot"
index d45fa4ee230276397833a95d648dfa97c7e7a8aa..ddea9631ebbe8e59d4ba2243df79c570685e6e48 100644 (file)
@@ -40,6 +40,8 @@
 #include <uk/config.h>
 #include <uk/syscall.h>
 
+#define TIDMAP_SIZE (CONFIG_LIBPOSIX_PROCESS_MAX_PID + 1)
+
 #if CONFIG_LIBPOSIX_PROCESS_PIDS
 #include <uk/bitmap.h>
 #include <uk/list.h>
@@ -82,8 +84,8 @@ struct posix_thread {
 /**
  * System global lists
  */
-static struct posix_thread *tid_thread[CONFIG_LIBPOSIX_PROCESS_MAX_PID];
-static unsigned long tid_map[UK_BITS_TO_LONGS(CONFIG_LIBPOSIX_PROCESS_MAX_PID)];
+static struct posix_thread *tid_thread[TIDMAP_SIZE];
+static unsigned long tid_map[UK_BITS_TO_LONGS(TIDMAP_SIZE)];
 
 /**
  * Thread-local posix_thread reference
@@ -99,14 +101,12 @@ static inline pid_t find_free_tid(void)
        unsigned long found;
 
        /* search starting from last position */
-       found = uk_find_next_zero_bit(tid_map,
-                                     CONFIG_LIBPOSIX_PROCESS_MAX_PID, prev);
-       if (found == CONFIG_LIBPOSIX_PROCESS_MAX_PID) {
+       found = uk_find_next_zero_bit(tid_map, TIDMAP_SIZE, prev);
+       if (found == TIDMAP_SIZE) {
                /* search again starting from the beginning */
-               found = uk_find_first_zero_bit(tid_map,
-                                              CONFIG_LIBPOSIX_PROCESS_MAX_PID);
+               found = uk_find_first_zero_bit(tid_map, TIDMAP_SIZE);
        }
-       if (found == CONFIG_LIBPOSIX_PROCESS_MAX_PID) {
+       if (found == TIDMAP_SIZE) {
                /* no free PID */
                return -1;
        }
@@ -121,14 +121,14 @@ static pid_t find_and_reserve_tid(void)
 
        /* TODO: Mutex */
        tid = find_free_tid();
-       if (tid >= 0)
+       if (tid > 0)
                uk_set_bit(tid, tid_map);
        return tid;
 }
 
 static void release_tid(pid_t tid)
 {
-       UK_ASSERT(tid >= 0 && tid <= CONFIG_LIBPOSIX_PROCESS_MAX_PID);
+       UK_ASSERT(tid > 0 && tid <= CONFIG_LIBPOSIX_PROCESS_MAX_PID);
 
        /* TODO: Mutex */
        uk_clear_bit(tid, tid_map);
@@ -280,7 +280,7 @@ int uk_posix_process_create(struct uk_alloc *a,
 
        uk_pr_debug("Process PID %d created (parent PID: %d)\n",
                    (int) pprocess->pid,
-                   (int) ((pprocess->parent) ? pprocess->parent->pid : -1));
+                   (int) ((pprocess->parent) ? pprocess->parent->pid : 0));
        return 0;
 
 err_free_pprocess:
@@ -537,8 +537,8 @@ UK_SYSCALL_R_DEFINE(pid_t, getppid)
        UK_ASSERT(pthread_self->process);
 
        if (!pthread_self->process->parent) {
-                /* no parent, return own PID */
-               return pthread_self->process->pid;
+                /* no parent, return 0 */
+               return 0;
        }
 
        return pthread_self->process->parent->pid;
@@ -591,7 +591,7 @@ static int pprocess_parent_settid(const struct clone_args *cl_args,
 {
        pid_t child_tid = ukthread2tid(child);
 
-       UK_ASSERT(child_tid >= 0);
+       UK_ASSERT(child_tid > 0);
 
        if (!cl_args->parent_tid)
                return -EINVAL;
@@ -609,7 +609,7 @@ static int pprocess_child_settid(const struct clone_args *cl_args,
 {
        pid_t child_tid = ukthread2tid(child);
 
-       UK_ASSERT(child_tid >= 0);
+       UK_ASSERT(child_tid > 0);
 
        if (!cl_args->child_tid)
                return -EINVAL;