Module Name: src
Committed By: pooka
Date: Fri Apr 3 16:40:55 UTC 2015
Modified Files:
src/sys/rump/kern/lib/libsysproxy: sysproxy.c
src/sys/rump/librump/rumpkern: lwproc.c rump_private.h vm.c
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/rump/kern/lib/libsysproxy/sysproxy.c
cvs rdiff -u -r1.32 -r1.33 src/sys/rump/librump/rumpkern/lwproc.c
cvs rdiff -u -r1.87 -r1.88 src/sys/rump/librump/rumpkern/rump_private.h
cvs rdiff -u -r1.161 -r1.162 src/sys/rump/librump/rumpkern/vm.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/rump/kern/lib/libsysproxy/sysproxy.c
diff -u src/sys/rump/kern/lib/libsysproxy/sysproxy.c:1.1 src/sys/rump/kern/lib/libsysproxy/sysproxy.c:1.2
--- src/sys/rump/kern/lib/libsysproxy/sysproxy.c:1.1 Wed Jan 7 22:24:04 2015
+++ src/sys/rump/kern/lib/libsysproxy/sysproxy.c Fri Apr 3 16:40:55 2015
@@ -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 @@ hyp_syscall(int num, void *arg, long *re
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 @@ hyp_rfork(void *priv, int flags, const c
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 @@ hyp_rfork(void *priv, int flags, const c
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)
Index: src/sys/rump/librump/rumpkern/lwproc.c
diff -u src/sys/rump/librump/rumpkern/lwproc.c:1.32 src/sys/rump/librump/rumpkern/lwproc.c:1.33
--- src/sys/rump/librump/rumpkern/lwproc.c:1.32 Wed Jan 21 14:39:37 2015
+++ src/sys/rump/librump/rumpkern/lwproc.c Fri Apr 3 16:40:55 2015
@@ -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 @@ lwproc_proc_free(struct proc *p)
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 @@ lwproc_proc_free(struct proc *p)
* 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 @@ lwproc_newproc(struct proc *parent, int
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 @@ rump__lwproc_alloclwp(struct proc *p)
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 @@ rump_lwproc_newlwp(pid_t pid)
}
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 @@ rump_lwproc_rfork(int flags)
(~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 @@ rump_lwproc_rfork(int flags)
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
Index: src/sys/rump/librump/rumpkern/rump_private.h
diff -u src/sys/rump/librump/rumpkern/rump_private.h:1.87 src/sys/rump/librump/rumpkern/rump_private.h:1.88
--- src/sys/rump/librump/rumpkern/rump_private.h:1.87 Wed Jan 7 22:24:04 2015
+++ src/sys/rump/librump/rumpkern/rump_private.h Fri Apr 3 16:40:55 2015
@@ -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 @@ do { \
#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_hyperentropy_init(void);
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"
Index: src/sys/rump/librump/rumpkern/vm.c
diff -u src/sys/rump/librump/rumpkern/vm.c:1.161 src/sys/rump/librump/rumpkern/vm.c:1.162
--- src/sys/rump/librump/rumpkern/vm.c:1.161 Sat Jan 3 17:23:51 2015
+++ src/sys/rump/librump/rumpkern/vm.c Fri Apr 3 16:40:55 2015
@@ -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 unsigned int pdaemon_waiters;
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 @@ uvm_init(void)
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 @@ uvmspace_init(struct vmspace *vm, struct
bool topdown)
{
- vm->vm_map.pmap = pmap_kernel();
+ vm->vm_map.pmap = pmap;
vm->vm_refcnt = 1;
}