Source-Changes-HG archive

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

[src/trunk]: src/sys/rump Use a different vmspace for rump kernel proc0 and l...



details:   https://anonhg.NetBSD.org/src/rev/f88e4badcc8a
branches:  trunk
changeset: 337124:f88e4badcc8a
user:      pooka <pooka%NetBSD.org@localhost>
date:      Fri Apr 03 16:40:55 2015 +0000

description:
Use a different vmspace for rump kernel proc0 and local clients.
While the rump kernel and local clients are by definition in the same
host vmspace, there are subtle differences in how in-kernel code works
in case accessing the kernel vmspace or a user process vmspace.

Problem discovered by riastradh's "read(fd, NULL, 1)" test.

diffstat:

 sys/rump/kern/lib/libsysproxy/sysproxy.c |  25 ++++++++++++-------------
 sys/rump/librump/rumpkern/lwproc.c       |  23 +++++++++++++++--------
 sys/rump/librump/rumpkern/rump_private.h |   7 +++++--
 sys/rump/librump/rumpkern/vm.c           |  13 ++++++++++---
 4 files changed, 42 insertions(+), 26 deletions(-)

diffs (223 lines):

diff -r 550768985ce3 -r f88e4badcc8a sys/rump/kern/lib/libsysproxy/sysproxy.c
--- a/sys/rump/kern/lib/libsysproxy/sysproxy.c  Fri Apr 03 16:37:02 2015 +0000
+++ b/sys/rump/kern/lib/libsysproxy/sysproxy.c  Fri Apr 03 16:40:55 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sysproxy.c,v 1.1 2015/01/07 22:24:04 pooka Exp $       */
+/*     $NetBSD: sysproxy.c,v 1.2 2015/04/03 16:40:55 pooka Exp $       */
 
 /*
  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sysproxy.c,v 1.1 2015/01/07 22:24:04 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sysproxy.c,v 1.2 2015/04/03 16:40:55 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/filedesc.h>
@@ -79,7 +79,7 @@
 static int
 hyp_rfork(void *priv, int flags, const char *comm)
 {
-       struct vmspace *newspace;
+       struct vmspace *vm;
        struct proc *p;
        struct lwp *l;
        int error;
@@ -96,8 +96,16 @@
                initfds = false;
        }
 
-       if ((error = rump_lwproc_rfork(flags)) != 0)
+       /*
+        * Since it's a proxy proc, we need create a vmspace for it.
+        */
+       vm = kmem_zalloc(sizeof(*vm), KM_SLEEP);
+       uvmspace_init(vm, priv, 0, 0, false);
+
+       if ((error = rump_lwproc_rfork_vmspace(vm, flags)) != 0) {
+               kmem_free(vm, sizeof(*vm));
                return error;
+       }
 
        /*
         * We forked in this routine, so cannot use curlwp (const)
@@ -105,15 +113,6 @@
        l = rump_lwproc_curlwp();
        p = l->l_proc;
 
-       /*
-        * Since it's a proxy proc, adjust the vmspace.
-        * Refcount will eternally be 1.
-        */
-       newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
-       newspace->vm_refcnt = 1;
-       newspace->vm_map.pmap = priv;
-       KASSERT(p->p_vmspace == vmspace_kernel());
-       p->p_vmspace = newspace;
        if (comm)
                strlcpy(p->p_comm, comm, sizeof(p->p_comm));
        if (initfds)
diff -r 550768985ce3 -r f88e4badcc8a sys/rump/librump/rumpkern/lwproc.c
--- a/sys/rump/librump/rumpkern/lwproc.c        Fri Apr 03 16:37:02 2015 +0000
+++ b/sys/rump/librump/rumpkern/lwproc.c        Fri Apr 03 16:40:55 2015 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: lwproc.c,v 1.32 2015/01/21 14:39:37 pooka Exp $       */
+/*      $NetBSD: lwproc.c,v 1.33 2015/04/03 16:40:55 pooka Exp $       */
 
 /*
  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
@@ -28,7 +28,7 @@
 #define RUMP__CURLWP_PRIVATE
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.32 2015/01/21 14:39:37 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.33 2015/04/03 16:40:55 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -130,7 +130,7 @@
        cv_destroy(&p->p_waitcv);
        cv_destroy(&p->p_lwpcv);
 
-       /* non-kernel vmspaces are not shared */
+       /* non-local vmspaces are not shared */
        if (!RUMP_LOCALPROC_P(p)) {
                KASSERT(p->p_vmspace->vm_refcnt == 1);
                kmem_free(p->p_vmspace, sizeof(*p->p_vmspace));
@@ -147,7 +147,7 @@
  * Switch to the new lwp and return a pointer to it.
  */
 static struct proc *
-lwproc_newproc(struct proc *parent, int flags)
+lwproc_newproc(struct proc *parent, struct vmspace *vm, int flags)
 {
        uid_t uid = kauth_cred_getuid(parent->p_cred);
        struct proc *p;
@@ -176,7 +176,7 @@
 
        p->p_stats = pstatscopy(parent->p_stats);
 
-       p->p_vmspace = vmspace_kernel();
+       p->p_vmspace = vm;
        p->p_emul = emul_default;
 #ifdef __HAVE_SYSCALL_INTERN
        p->p_emul->e_syscall_intern(p);
@@ -327,7 +327,7 @@
        bool newproc = false;
 
        if (p == NULL) {
-               p = lwproc_newproc(&proc0, 0);
+               p = lwproc_newproc(&proc0, rump_vmspace_local, 0);
                newproc = true;
        }
 
@@ -368,7 +368,7 @@
 }
 
 int
-rump_lwproc_rfork(int flags)
+rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
 {
        struct proc *p;
        struct lwp *l;
@@ -377,7 +377,7 @@
            (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
                return EINVAL;
 
-       p = lwproc_newproc(curproc, flags);
+       p = lwproc_newproc(curproc, vm, flags);
        l = kmem_zalloc(sizeof(*l), KM_SLEEP);
        mutex_enter(p->p_lock);
        KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
@@ -386,6 +386,13 @@
        return 0;
 }
 
+int
+rump_lwproc_rfork(int flags)
+{
+
+       return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
+}
+
 /*
  * Switch to a new process/thread.  Release previous one if
  * deemed to be exiting.  This is considered a slow path for
diff -r 550768985ce3 -r f88e4badcc8a sys/rump/librump/rumpkern/rump_private.h
--- a/sys/rump/librump/rumpkern/rump_private.h  Fri Apr 03 16:37:02 2015 +0000
+++ b/sys/rump/librump/rumpkern/rump_private.h  Fri Apr 03 16:40:55 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rump_private.h,v 1.87 2015/01/07 22:24:04 pooka Exp $  */
+/*     $NetBSD: rump_private.h,v 1.88 2015/04/03 16:40:55 pooka Exp $  */
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -117,7 +117,9 @@
 #define RUMPMEM_UNLIMITED ((unsigned long)-1)
 extern unsigned long rump_physmemlimit;
 
-#define RUMP_LOCALPROC_P(p) (p->p_vmspace == vmspace_kernel())
+extern struct vmspace *rump_vmspace_local;
+#define RUMP_LOCALPROC_P(p) \
+    (p->p_vmspace == vmspace_kernel() || p->p_vmspace == rump_vmspace_local)
 
 void           rump_component_load(const struct rump_component *);
 void           rump_component_init(enum rump_component_type);
@@ -186,6 +188,7 @@
 void   rump_lwproc_init(void);
 void   rump_lwproc_curlwp_set(struct lwp *);
 void   rump_lwproc_curlwp_clear(struct lwp *);
+int    rump_lwproc_rfork_vmspace(struct vmspace *, int);
 
 /*
  * sysproxy is an optional component.  The interfaces with "hyp"
diff -r 550768985ce3 -r f88e4badcc8a sys/rump/librump/rumpkern/vm.c
--- a/sys/rump/librump/rumpkern/vm.c    Fri Apr 03 16:37:02 2015 +0000
+++ b/sys/rump/librump/rumpkern/vm.c    Fri Apr 03 16:40:55 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vm.c,v 1.161 2015/01/03 17:23:51 pooka Exp $   */
+/*     $NetBSD: vm.c,v 1.162 2015/04/03 16:40:55 pooka Exp $   */
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.161 2015/01/03 17:23:51 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.162 2015/04/03 16:40:55 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -95,6 +95,9 @@
 static kmutex_t pdaemonmtx;
 static kcondvar_t pdaemoncv, oomwait;
 
+/* all local non-proc0 processes share this vmspace */
+struct vmspace *rump_vmspace_local;
+
 unsigned long rump_physmemlimit = RUMPMEM_UNLIMITED;
 static unsigned long pdlimit = RUMPMEM_UNLIMITED; /* page daemon memlimit */
 static unsigned long curphysmem;
@@ -389,6 +392,10 @@
 
        pool_cache_bootstrap(&pagecache, sizeof(struct vm_page), 0, 0, 0,
            "page$", NULL, IPL_NONE, pgctor, pgdtor, NULL);
+
+       /* create vmspace used by local clients */
+       rump_vmspace_local = kmem_zalloc(sizeof(*rump_vmspace_local), KM_SLEEP);
+       uvmspace_init(rump_vmspace_local, (struct pmap *)-2, 0, 0, false);
 }
 
 void
@@ -396,7 +403,7 @@
     bool topdown)
 {
 
-       vm->vm_map.pmap = pmap_kernel();
+       vm->vm_map.pmap = pmap;
        vm->vm_refcnt = 1;
 }
 



Home | Main Index | Thread Index | Old Index