Module Name:    src
Committed By:   pooka
Date:           Fri Oct 29 15:32:24 UTC 2010

Modified Files:
        src/sys/kern: kern_descrip.c
        src/sys/rump/librump/rumpkern: lwproc.c rump.c rump_private.h
            scheduler.c threads.c

Log Message:
Attach implicit threads to initproc instead of proc0.  This way
applications which alter, by purpose or by accident, the uid in an
implicit thread are don't affect kernel threads.

from discussion with njoly


To generate a diff of this commit:
cvs rdiff -u -r1.206 -r1.207 src/sys/kern/kern_descrip.c
cvs rdiff -u -r1.2 -r1.3 src/sys/rump/librump/rumpkern/lwproc.c
cvs rdiff -u -r1.192 -r1.193 src/sys/rump/librump/rumpkern/rump.c
cvs rdiff -u -r1.59 -r1.60 src/sys/rump/librump/rumpkern/rump_private.h
cvs rdiff -u -r1.20 -r1.21 src/sys/rump/librump/rumpkern/scheduler.c
cvs rdiff -u -r1.12 -r1.13 src/sys/rump/librump/rumpkern/threads.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/kern/kern_descrip.c
diff -u src/sys/kern/kern_descrip.c:1.206 src/sys/kern/kern_descrip.c:1.207
--- src/sys/kern/kern_descrip.c:1.206	Wed Sep  1 15:15:18 2010
+++ src/sys/kern/kern_descrip.c	Fri Oct 29 15:32:23 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_descrip.c,v 1.206 2010/09/01 15:15:18 pooka Exp $	*/
+/*	$NetBSD: kern_descrip.c,v 1.207 2010/10/29 15:32:23 pooka Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.206 2010/09/01 15:15:18 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.207 2010/10/29 15:32:23 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1339,7 +1339,6 @@
 {
 	filedesc_t *fdp = l->l_fd;
 
-	KASSERT(fdp == curlwp->l_fd || fdp == lwp0.l_fd);
 	atomic_inc_uint(&fdp->fd_refcnt);
 }
 

Index: src/sys/rump/librump/rumpkern/lwproc.c
diff -u src/sys/rump/librump/rumpkern/lwproc.c:1.2 src/sys/rump/librump/rumpkern/lwproc.c:1.3
--- src/sys/rump/librump/rumpkern/lwproc.c:1.2	Wed Sep  1 21:16:56 2010
+++ src/sys/rump/librump/rumpkern/lwproc.c	Fri Oct 29 15:32:24 2010
@@ -1,4 +1,4 @@
-/*      $NetBSD: lwproc.c,v 1.2 2010/09/01 21:16:56 pooka Exp $	*/
+/*      $NetBSD: lwproc.c,v 1.3 2010/10/29 15:32:24 pooka Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.2 2010/09/01 21:16:56 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.3 2010/10/29 15:32:24 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -222,16 +222,20 @@
 }
 
 struct lwp *
-rump__lwproc_allockernlwp(void)
+rump__lwproc_alloclwp(struct proc *p)
 {
-	struct proc *p;
 	struct lwp *l;
+	bool newproc = false;
+
+	if (p == NULL) {
+		p = lwproc_newproc(&proc0);
+		newproc = true;
+	}
 
 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
 
-	p = &proc0;
 	mutex_enter(p->p_lock);
-	lwproc_makelwp(p, l, false, false);
+	lwproc_makelwp(p, l, false, newproc);
 
 	return l;
 }

Index: src/sys/rump/librump/rumpkern/rump.c
diff -u src/sys/rump/librump/rumpkern/rump.c:1.192 src/sys/rump/librump/rumpkern/rump.c:1.193
--- src/sys/rump/librump/rumpkern/rump.c:1.192	Thu Oct 28 11:30:07 2010
+++ src/sys/rump/librump/rumpkern/rump.c	Fri Oct 29 15:32:24 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 pooka Exp $	*/
+/*	$NetBSD: rump.c,v 1.193 2010/10/29 15:32:24 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.192 2010/10/28 11:30:07 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.193 2010/10/29 15:32:24 pooka Exp $");
 
 #include <sys/systm.h>
 #define ELFSIZE ARCH_ELFSIZE
@@ -86,8 +86,7 @@
 
 char machine[] = MACHINE;
 
-/* pretend the master rump proc is init */
-struct proc *initproc = &proc0;
+struct proc *initproc;
 
 struct rumpuser_mtx *rump_giantlock;
 
@@ -322,9 +321,10 @@
 	lwp_initspecific(&lwp0);
 
 	rump_scheduler_init(numcpu);
-	/* revert temporary context and schedule a real context */
+	/* revert temporary context and schedule a semireal context */
 	l->l_cpu = NULL;
 	rumpuser_set_curlwp(NULL);
+	initproc = &proc0; /* borrow proc0 before we get initproc started */
 	rump_schedule();
 
 	percpu_init();
@@ -407,6 +407,18 @@
 	if (rump_threads)
 		vmem_rehash_start();
 
+	/*
+	 * Create init, used to attach implicit threads in rump.
+	 * (note: must be done after vfsinit to get cwdi)
+	 */
+	(void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
+	mutex_enter(proc_lock);
+	initproc = proc_find_raw(1);
+	mutex_exit(proc_lock);
+	if (initproc == NULL)
+		panic("where in the world is initproc?");
+
+	/* release cpu */
 	rump_unschedule();
 
 	return 0;

Index: src/sys/rump/librump/rumpkern/rump_private.h
diff -u src/sys/rump/librump/rumpkern/rump_private.h:1.59 src/sys/rump/librump/rumpkern/rump_private.h:1.60
--- src/sys/rump/librump/rumpkern/rump_private.h:1.59	Wed Oct 27 20:44:49 2010
+++ src/sys/rump/librump/rumpkern/rump_private.h	Fri Oct 29 15:32:24 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: rump_private.h,v 1.59 2010/10/27 20:44:49 pooka Exp $	*/
+/*	$NetBSD: rump_private.h,v 1.60 2010/10/29 15:32:24 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
@@ -105,7 +105,7 @@
 
 extern struct cpu_info *rump_cpu;
 
-struct lwp *	rump__lwproc_allockernlwp(void);
+struct lwp *	rump__lwproc_alloclwp(struct proc *);
 
 void	rump_cpus_bootstrap(int);
 void	rump_scheduler_init(int);

Index: src/sys/rump/librump/rumpkern/scheduler.c
diff -u src/sys/rump/librump/rumpkern/scheduler.c:1.20 src/sys/rump/librump/rumpkern/scheduler.c:1.21
--- src/sys/rump/librump/rumpkern/scheduler.c:1.20	Tue Sep  7 07:59:48 2010
+++ src/sys/rump/librump/rumpkern/scheduler.c	Fri Oct 29 15:32:24 2010
@@ -1,4 +1,4 @@
-/*      $NetBSD: scheduler.c,v 1.20 2010/09/07 07:59:48 pooka Exp $	*/
+/*      $NetBSD: scheduler.c,v 1.21 2010/10/29 15:32:24 pooka Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.20 2010/09/07 07:59:48 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.21 2010/10/29 15:32:24 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -236,7 +236,7 @@
 		rumpuser_set_curlwp(&lwp0);
 
 		/* allocate thread, switch to it, and release lwp0 */
-		l = rump__lwproc_allockernlwp();
+		l = rump__lwproc_alloclwp(initproc);
 		rump_lwproc_switch(l);
 		lwp0rele();
 

Index: src/sys/rump/librump/rumpkern/threads.c
diff -u src/sys/rump/librump/rumpkern/threads.c:1.12 src/sys/rump/librump/rumpkern/threads.c:1.13
--- src/sys/rump/librump/rumpkern/threads.c:1.12	Wed Sep  1 19:37:59 2010
+++ src/sys/rump/librump/rumpkern/threads.c	Fri Oct 29 15:32:24 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: threads.c,v 1.12 2010/09/01 19:37:59 pooka Exp $	*/
+/*	$NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $	*/
 
 /*
  * Copyright (c) 2007-2009 Antti Kantee.  All Rights Reserved.
@@ -29,7 +29,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.12 2010/09/01 19:37:59 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.13 2010/10/29 15:32:24 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -139,7 +139,7 @@
 	k = malloc(sizeof(*k), M_TEMP, M_WAITOK);
 	k->f = func;
 	k->arg = arg;
-	k->mylwp = l = rump__lwproc_allockernlwp();
+	k->mylwp = l = rump__lwproc_alloclwp(&proc0);
 	l->l_flag |= LW_SYSTEM;
 	if (flags & KTHREAD_MPSAFE)
 		l->l_pflag |= LP_MPSAFE;

Reply via email to