Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys Overhaul the way LWP IDs are allocated. Instead of ...



details:   https://anonhg.NetBSD.org/src/rev/0e42d43c5b75
branches:  trunk
changeset: 971436:0e42d43c5b75
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Fri Apr 24 03:22:06 2020 +0000

description:
Overhaul the way LWP IDs are allocated.  Instead of each LWP having it's
own LWP ID space, LWP IDs came from the same number space as PIDs.  The
lead LWP of a process gets the PID as its LID.  If a multi-LWP process's
lead LWP exits, the PID persists for the process.

In addition to providing system-wide unique thread IDs, this also lets us
eliminate the per-process LWP radix tree, and some associated locks.

Remove the separate "global thread ID" map added previously; it is no longer
needed to provide this functionality.

Nudged in this direction by ad@ and chs@.

diffstat:

 sys/compat/linux/common/linux_exec.c  |    7 +-
 sys/compat/linux/common/linux_sched.c |    6 +-
 sys/kern/kern_exec.c                  |   19 +-
 sys/kern/kern_exit.c                  |   16 +-
 sys/kern/kern_fork.c                  |   16 +-
 sys/kern/kern_lwp.c                   |  387 +++++------------------
 sys/kern/kern_proc.c                  |  552 ++++++++++++++++++++++++++-------
 sys/kern/sys_lwp.c                    |   43 +-
 sys/sys/lwp.h                         |   29 +-
 sys/sys/proc.h                        |   13 +-
 10 files changed, 593 insertions(+), 495 deletions(-)

diffs (truncated from 1994 to 300 lines):

diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/compat/linux/common/linux_exec.c
--- a/sys/compat/linux/common/linux_exec.c      Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/compat/linux/common/linux_exec.c      Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux_exec.c,v 1.121 2020/02/15 17:13:55 ad Exp $      */
+/*     $NetBSD: linux_exec.c,v 1.122 2020/04/24 03:22:06 thorpej Exp $ */
 
 /*-
  * Copyright (c) 1994, 1995, 1998, 2000, 2007, 2008, 2020
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.121 2020/02/15 17:13:55 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.122 2020/04/24 03:22:06 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -130,8 +130,6 @@
        }
 
        KASSERT(p->p_nlwps == 1);
-       l = LIST_FIRST(&p->p_lwps);
-       lwp_renumber(l, p->p_pid);
 }
 
 void
@@ -152,7 +150,6 @@
 
        KASSERT(p2->p_nlwps == 1);
        l2 = LIST_FIRST(&p2->p_lwps);
-       lwp_renumber(l2, p2->p_pid);
        led1 = l1->l_emuldata;
        led2 = l2->l_emuldata;
        led2->led_child_tidptr = led1->led_child_tidptr;
diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/compat/linux/common/linux_sched.c
--- a/sys/compat/linux/common/linux_sched.c     Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/compat/linux/common/linux_sched.c     Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: linux_sched.c,v 1.74 2020/04/19 20:31:59 thorpej Exp $ */
+/*     $NetBSD: linux_sched.c,v 1.75 2020/04/24 03:22:06 thorpej Exp $ */
 
 /*-
  * Copyright (c) 1999, 2019 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.74 2020/04/19 20:31:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.75 2020/04/24 03:22:06 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/mount.h>
@@ -195,7 +195,7 @@
                return ENOMEM;
        }
 
-       error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
+       error = lwp_create(l, p, uaddr, LWP_DETACHED,
            SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class,
            &l->l_sigmask, &l->l_sigstk);
        if (__predict_false(error)) {
diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/kern/kern_exec.c
--- a/sys/kern/kern_exec.c      Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/kern/kern_exec.c      Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exec.c,v 1.498 2020/04/21 21:42:47 ad Exp $       */
+/*     $NetBSD: kern_exec.c,v 1.499 2020/04/24 03:22:06 thorpej Exp $  */
 
 /*-
  * Copyright (c) 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.498 2020/04/21 21:42:47 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.499 2020/04/24 03:22:06 thorpej Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -1148,10 +1148,6 @@
            && p->p_emul != epp->ep_esch->es_emul)
                (*p->p_emul->e_proc_exit)(p);
 
-       /* This is now LWP 1.  Re-number the LWP if needed. */
-       if (l->l_lid != 1)
-               lwp_renumber(l, 1);
-
        /*
         * Call exec hook. Emulation code may NOT store reference to anything
         * from &pack.
@@ -2495,10 +2491,18 @@
         * Allocate new proc. Borrow proc0 vmspace for it, we will
         * replace it with its own before returning to userland
         * in the child.
+        */
+       p2 = proc_alloc();
+       if (p2 == NULL) {
+               /* We were unable to allocate a process ID. */
+               error = EAGAIN;
+               goto error_exit;
+       }
+
+       /*
         * This is a point of no return, we will have to go through
         * the child proc to properly clean it up past this point.
         */
-       p2 = proc_alloc();
        pid = p2->p_pid;
 
        /*
@@ -2533,7 +2537,6 @@
        mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
        mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
        rw_init(&p2->p_reflock);
-       rw_init(&p2->p_treelock);
        cv_init(&p2->p_waitcv, "wait");
        cv_init(&p2->p_lwpcv, "lwpwait");
 
diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/kern/kern_exit.c
--- a/sys/kern/kern_exit.c      Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/kern/kern_exit.c      Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_exit.c,v 1.288 2020/04/19 20:31:59 thorpej Exp $  */
+/*     $NetBSD: kern_exit.c,v 1.289 2020/04/24 03:22:06 thorpej Exp $  */
 
 /*-
  * Copyright (c) 1998, 1999, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.288 2020/04/19 20:31:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.289 2020/04/24 03:22:06 thorpej Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -202,7 +202,6 @@
        ksiginfo_t      ksi;
        ksiginfoq_t     kq;
        int             wakeinit;
-       struct lwp      *l2 __diagused;
 
        p = l->l_proc;
 
@@ -560,14 +559,8 @@
        pcu_discard_all(l);
 
        mutex_enter(p->p_lock);
-       /* Don't bother with p_treelock as no other LWPs remain. */
-       l2 = radix_tree_remove_node(&p->p_lwptree, (uint64_t)(l->l_lid - 1));
-       KASSERT(l2 == l);
-       KASSERT(radix_tree_empty_tree_p(&p->p_lwptree));
-       radix_tree_fini_tree(&p->p_lwptree);
-       /* Free the linux lwp id */
-       if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
-               proc_free_pid(l->l_lid);
+       /* Free the LWP ID */
+       proc_free_lwpid(p, l->l_lid);
        lwp_drainrefs(l);
        lwp_lock(l);
        l->l_prflag &= ~LPR_DETACHED;
@@ -1269,7 +1262,6 @@
        cv_destroy(&p->p_waitcv);
        cv_destroy(&p->p_lwpcv);
        rw_destroy(&p->p_reflock);
-       rw_destroy(&p->p_treelock);
 
        proc_free_mem(p);
 }
diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/kern/kern_fork.c
--- a/sys/kern/kern_fork.c      Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/kern/kern_fork.c      Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_fork.c,v 1.222 2020/04/14 22:42:18 kamil Exp $    */
+/*     $NetBSD: kern_fork.c,v 1.223 2020/04/24 03:22:06 thorpej Exp $  */
 
 /*-
  * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008, 2019
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.222 2020/04/14 22:42:18 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.223 2020/04/24 03:22:06 thorpej Exp $");
 
 #include "opt_ktrace.h"
 #include "opt_dtrace.h"
@@ -305,14 +305,18 @@
                return ENOMEM;
        }
 
+       /* Allocate new proc. */
+       p2 = proc_alloc();
+       if (p2 == NULL) {
+               /* We were unable to allocate a process ID. */
+               return EAGAIN;
+       }
+
        /*
         * We are now committed to the fork.  From here on, we may
         * block on resources, but resource allocation may NOT fail.
         */
 
-       /* Allocate new proc. */
-       p2 = proc_alloc();
-
        /*
         * Make a proc table entry for the new process.
         * Start by zeroing the section of proc that is zero-initialized,
@@ -327,7 +331,6 @@
 
        LIST_INIT(&p2->p_lwps);
        LIST_INIT(&p2->p_sigwaiters);
-       radix_tree_init_tree(&p2->p_lwptree);
 
        /*
         * Duplicate sub-structures as needed.
@@ -354,7 +357,6 @@
        mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
        mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
        rw_init(&p2->p_reflock);
-       rw_init(&p2->p_treelock);
        cv_init(&p2->p_waitcv, "wait");
        cv_init(&p2->p_lwpcv, "lwpwait");
 
diff -r c69ccdb6c891 -r 0e42d43c5b75 sys/kern/kern_lwp.c
--- a/sys/kern/kern_lwp.c       Fri Apr 24 02:27:59 2020 +0000
+++ b/sys/kern/kern_lwp.c       Fri Apr 24 03:22:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_lwp.c,v 1.234 2020/04/19 23:05:04 ad Exp $        */
+/*     $NetBSD: kern_lwp.c,v 1.235 2020/04/24 03:22:06 thorpej Exp $   */
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2019, 2020
@@ -83,6 +83,16 @@
  *     The LP_RUNNING flag in lwp::l_pflag indicates that an LWP is running.
  *     Importantly, it indicates that its state is tied to a CPU.
  *
+ *     LSLARVAL:
+ *
+ *             Born, but not fully mature: the LWP is in the process
+ *             of being constructed.  This state exists so that the
+ *             LWP can occupy a slot in the PID table, but without
+ *             having to worry about being touched; lookups of the
+ *             LWP will fail while in this state.  The LWP will become
+ *             visible in the PID table once its state transitions
+ *             to LSIDL.
+ *
  *     LSZOMB:
  *
  *             Dead or dying: the LWP has released most of its resources
@@ -120,6 +130,8 @@
  *
  *     LWPs may transition states in the following ways:
  *
+ *      LARVAL ----> IDL
+ *
  *      RUN -------> ONPROC            ONPROC -----> RUN
  *                                                 > SLEEP
  *                                                 > STOPPED
@@ -211,7 +223,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.234 2020/04/19 23:05:04 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.235 2020/04/24 03:22:06 thorpej Exp $");
 
 #include "opt_ddb.h"
 #include "opt_lockdebug.h"
@@ -245,7 +257,6 @@
 #include <sys/psref.h>
 #include <sys/msan.h>
 #include <sys/kcov.h>
-#include <sys/thmap.h>
 #include <sys/cprng.h>
 
 #include <uvm/uvm_extern.h>
@@ -258,59 +269,27 @@
  * Lookups by global thread ID operate outside of the normal LWP
  * locking protocol.
  *
- * We are using a thmap, which internally can perform lookups lock-free.
- * However, we still need to serialize lookups against LWP exit.  We
- * achieve this as follows:
- *
- * => Assignment of TID is performed lazily by the LWP itself, when it
- *    is first requested.  Insertion into the thmap is done completely
- *    lock-free (other than the internal locking performed by thmap itself).
- *    Once the TID is published in the map, the l___tid field in the LWP
- *    is protected by p_lock.
- *
- * => When we look up an LWP in the thmap, we take lwp_threadid_lock as
+ * => When we look up an LWP in the table, we take lwp_threadid_lock as
  *    a READER.  While still holding the lock, we add a reference to
  *    the LWP (using atomics).  After adding the reference, we drop the
  *    lwp_threadid_lock.  We now take p_lock and check the state of the
- *    LWP.  If the LWP is draining its references or if the l___tid field
- *    has been invalidated, we drop the reference we took and return NULL.
- *    Otherwise, the lookup has succeeded and the LWP is returned with a



Home | Main Index | Thread Index | Old Index