Subject: Code cleanup
To: None <tech-kern@netbsd.org>
From: Bang Jun-Young <junyoung@netbsd.org>
List: tech-kern
Date: 07/20/2005 11:40:14
--Boundary-00=_Omb3CzLI+8wu/po
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,

Currently, main() function in init_main.c is bloated with proc0 initialization.
The attached patch introduces proc0_init(), which does all the jobs needed to
set up proc0, and replaces existing proc0_insert() as well. As a result, code
is cleaner and easier to understand, IMHO.

Any comments?

Jun-Young

--Boundary-00=_Omb3CzLI+8wu/po
Content-Type: text/x-diff;
  charset="us-ascii";
  name="proc0.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="proc0.diff"

Index: sys/kern/init_main.c
===================================================================
--- sys/kern/init_main.c	(revision 9)
+++ sys/kern/init_main.c	(revision 10)
@@ -168,28 +168,15 @@
 #include <net/if.h>
 #include <net/raw_cb.h>
 
-/* Components of the first process -- never freed. */
-struct	session session0;
-struct	pgrp pgrp0;
-struct	proc proc0;
-struct	lwp lwp0;
-struct	pcred cred0;
-struct	filedesc0 filedesc0;
-struct	cwdinfo cwdi0;
-struct	plimit limit0;
-struct	pstats pstat0;
-struct	vmspace vmspace0;
-struct	sigacts sigacts0;
+extern struct proc proc0;
+extern struct lwp lwp0;
+extern struct cwdinfo cwdi0;
+
 #ifndef curlwp
 struct	lwp *curlwp = &lwp0;
 #endif
 struct	proc *initproc;
 
-int	nofile = NOFILE;
-int	maxuprc = MAXUPRC;
-int	cmask = CMASK;
-extern	struct user *proc0paddr;
-
 struct	vnode *rootvp, *swapdev_vp;
 int	boothowto;
 int	cold = 1;			/* still working on startup */
@@ -201,8 +188,6 @@
 static void start_init(void *);
 void main(void);
 
-extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
-
 /*
  * System startup; initialize the world, create process 0, mount root
  * filesystem, and fork to create init and pagedaemon.  Most of the
@@ -216,8 +201,6 @@
 	struct proc *p;
 	struct pdevinit *pdev;
 	int s, error;
-	u_int i;
-	rlim_t lim;
 	extern struct pdevinit pdevinit[];
 	extern void schedcpu(void *);
 #if defined(NFSSERVER) || defined(NFS)
@@ -300,82 +283,17 @@
 	lkm_init();
 #endif
 
-	/*
-	 * Create process 0 (the swapper).
-	 */
-	p = &proc0;
-	proc0_insert(p, l, &pgrp0, &session0);
-
-	/*
-	 * Set P_NOCLDWAIT so that kernel threads are reparented to
-	 * init(8) when they exit.  init(8) can easily wait them out
-	 * for us.
-	 */
-	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
-	p->p_stat = SACTIVE;
-	p->p_nice = NZERO;
-	p->p_emul = &emul_netbsd;
-#ifdef __HAVE_SYSCALL_INTERN
-	(*p->p_emul->e_syscall_intern)(p);
-#endif
-	strncpy(p->p_comm, "swapper", MAXCOMLEN);
-
-	l->l_flag = L_INMEM;
-	l->l_stat = LSONPROC;
-	p->p_nrlwps = 1;
-
-	callout_init(&l->l_tsleep_ch);
-
-	/* Create credentials. */
-	cred0.p_refcnt = 1;
-	p->p_cred = &cred0;
-	p->p_ucred = crget();
-	p->p_ucred->cr_ngroups = 1;	/* group 0 */
-
 	/* Create the file descriptor table. */
 	finit();
-	p->p_fd = &filedesc0.fd_fd;
-	fdinit1(&filedesc0);
 
-	/* Create the CWD info. */
-	p->p_cwdi = &cwdi0;
-	cwdi0.cwdi_cmask = cmask;
-	cwdi0.cwdi_refcnt = 1;
+	/* Initialize signal-related data structures. */
+	signal_init();
 
-	/* Create the limits structures. */
-	p->p_limit = &limit0;
-	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
-		limit0.pl_rlimit[i].rlim_cur =
-		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
-
-	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
-	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
-	    maxfiles < nofile ? maxfiles : nofile;
-
-	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
-	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
-	    maxproc < maxuprc ? maxproc : maxuprc;
-
-	lim = ptoa(uvmexp.free);
-	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
-	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
-	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
-	limit0.pl_corename = defcorename;
-	limit0.p_refcnt = 1;
-
 	/*
-	 * Initialize proc0's vmspace, which uses the kernel pmap.
-	 * All kernel processes (which never have user space mappings)
-	 * share proc0's vmspace, and thus, the kernel pmap.
+	 * Create process 0 (the swapper).
 	 */
-	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
-	    trunc_page(VM_MAX_ADDRESS));
-	p->p_vmspace = &vmspace0;
+	proc0_init();
 
-	l->l_addr = proc0paddr;				/* XXX */
-
-	p->p_stats = &pstat0;
-
 	/*
 	 * Charge root for one process.
 	 */
@@ -383,9 +301,6 @@
 
 	rqinit();
 
-	/* Configure virtual memory system, set vm rlimits. */
-	uvm_init_limits(p);
-
 	/* Initialize the file systems. */
 #if defined(NFSSERVER) || defined(NFS)
 	nfs_init();			/* initialize server/shared data */
@@ -459,13 +374,6 @@
 #ifdef SYSTRACE
 	systrace_init();
 #endif
-	/*
-	 * Initialize signal-related data structures, and signal state
-	 * for proc0.
-	 */
-	signal_init();
-	p->p_sigacts = &sigacts0;
-	siginit(p);
 
 	/* Kick off timeout driven events by calling first time. */
 	schedcpu(NULL);
Index: sys/kern/kern_proc.c
===================================================================
--- sys/kern/kern_proc.c	(revision 9)
+++ sys/kern/kern_proc.c	(revision 10)
@@ -93,7 +93,10 @@
 #include <sys/ras.h>
 #include <sys/sa.h>
 #include <sys/savar.h>
+#include <sys/filedesc.h>
 
+#include <uvm/uvm.h>
+
 /*
  * Structure associated with user caching.
  */
@@ -166,6 +169,27 @@
 static uint next_free_pt, last_free_pt;
 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
 
+/* Components of the first process -- never freed. */
+struct	session session0;
+struct	pgrp pgrp0;
+struct	proc proc0;
+struct	lwp lwp0;
+struct	pcred cred0;
+struct	filedesc0 filedesc0;
+struct	cwdinfo cwdi0;
+struct	plimit limit0;
+struct	pstats pstat0;
+struct	vmspace vmspace0;
+struct	sigacts sigacts0;
+
+extern	struct user *proc0paddr;
+
+extern const struct emul emul_netbsd;	/* defined in kern_exec.c */
+
+int	nofile = NOFILE;
+int	maxuprc = MAXUPRC;
+int	cmask = CMASK;
+
 struct pool proc_pool;
 struct pool lwp_pool;
 struct pool lwp_uc_pool;
@@ -268,6 +292,127 @@
 }
 
 /*
+ * Initialize process 0.
+ */
+void
+proc0_init(void)
+{
+	struct proc *p;
+	struct pgrp *pg;
+	struct session *sess;
+	struct lwp *l;
+	int s;
+	u_int i;
+	rlim_t lim;
+
+	p = &proc0;
+	pg = &pgrp0;
+	sess = &session0;
+	l = &lwp0;
+
+	simple_lock_init(&p->p_lock);
+	LIST_INIT(&p->p_lwps);
+	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
+	p->p_nlwps = 1;
+	simple_lock_init(&p->p_sigctx.ps_silock);
+	CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
+
+	s = proclist_lock_write();
+
+	pid_table[0].pt_proc = p;
+	LIST_INSERT_HEAD(&allproc, p, p_list);
+	LIST_INSERT_HEAD(&alllwp, l, l_list);
+
+	p->p_pgrp = pg;
+	pid_table[0].pt_pgrp = pg;
+	LIST_INIT(&pg->pg_members);
+	LIST_INSERT_HEAD(&pg->pg_members, p, p_pglist);
+
+	pg->pg_session = sess;
+	sess->s_count = 1;
+	sess->s_sid = 0;
+	sess->s_leader = p;
+
+	proclist_unlock_write(s);
+
+	/*
+	 * Set P_NOCLDWAIT so that kernel threads are reparented to
+	 * init(8) when they exit.  init(8) can easily wait them out
+	 * for us.
+	 */
+	p->p_flag = P_SYSTEM | P_NOCLDWAIT;
+	p->p_stat = SACTIVE;
+	p->p_nice = NZERO;
+	p->p_emul = &emul_netbsd;
+#ifdef __HAVE_SYSCALL_INTERN
+	(*p->p_emul->e_syscall_intern)(p);
+#endif
+	strncpy(p->p_comm, "swapper", MAXCOMLEN);
+
+	l->l_flag = L_INMEM;
+	l->l_stat = LSONPROC;
+	p->p_nrlwps = 1;
+
+	callout_init(&l->l_tsleep_ch);
+
+	/* Create credentials. */
+	cred0.p_refcnt = 1;
+	p->p_cred = &cred0;
+	p->p_ucred = crget();
+	p->p_ucred->cr_ngroups = 1;	/* group 0 */
+
+	/* Create the CWD info. */
+	p->p_cwdi = &cwdi0;
+	cwdi0.cwdi_cmask = cmask;
+	cwdi0.cwdi_refcnt = 1;
+
+	/* Create the limits structures. */
+	p->p_limit = &limit0;
+	for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
+		limit0.pl_rlimit[i].rlim_cur =
+		    limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
+
+	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles;
+	limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur =
+	    maxfiles < nofile ? maxfiles : nofile;
+
+	limit0.pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc;
+	limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur =
+	    maxproc < maxuprc ? maxproc : maxuprc;
+
+	lim = ptoa(uvmexp.free);
+	limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
+	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
+	limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
+	limit0.pl_corename = defcorename;
+	limit0.p_refcnt = 1;
+
+	/* Configure virtual memory system, set vm rlimits. */
+	uvm_init_limits(p);
+
+	/* Initialize file descriptor table for proc0. */
+	p->p_fd = &filedesc0.fd_fd;
+	fdinit1(&filedesc0);
+
+	/*
+	 * Initialize proc0's vmspace, which uses the kernel pmap.
+	 * All kernel processes (which never have user space mappings)
+	 * share proc0's vmspace, and thus, the kernel pmap.
+	 */
+	uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
+	    trunc_page(VM_MAX_ADDRESS));
+	p->p_vmspace = &vmspace0;
+
+	l->l_addr = proc0paddr;				/* XXX */
+
+	p->p_stats = &pstat0;
+
+	/* Initialize signal state for proc0. */
+	p->p_sigacts = &sigacts0;
+	siginit(p);
+}
+
+/*
  * Acquire a read lock on the proclist.
  */
 void
@@ -449,41 +594,6 @@
 	return pg;
 }
 
-/*
- * Set entry for process 0
- */
-void
-proc0_insert(struct proc *p, struct lwp *l, struct pgrp *pgrp,
-	struct session *sess)
-{
-	int s;
-
-	simple_lock_init(&p->p_lock);
-	LIST_INIT(&p->p_lwps);
-	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
-	p->p_nlwps = 1;
-	simple_lock_init(&p->p_sigctx.ps_silock);
-	CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
-
-	s = proclist_lock_write();
-
-	pid_table[0].pt_proc = p;
-	LIST_INSERT_HEAD(&allproc, p, p_list);
-	LIST_INSERT_HEAD(&alllwp, l, l_list);
-
-	p->p_pgrp = pgrp;
-	pid_table[0].pt_pgrp = pgrp;
-	LIST_INIT(&pgrp->pg_members);
-	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
-
-	pgrp->pg_session = sess;
-	sess->s_count = 1;
-	sess->s_sid = 0;
-	sess->s_leader = p;
-
-	proclist_unlock_write(s);
-}
-
 static void
 expand_pid_table(void)
 {
Index: sys/sys/proc.h
===================================================================
--- sys/sys/proc.h	(revision 9)
+++ sys/sys/proc.h	(revision 10)
@@ -446,7 +446,7 @@
 void	exit1(struct lwp *, int);
 int	find_stopped_child(struct proc *, pid_t, int, struct proc **);
 struct proc *proc_alloc(void);
-void	proc0_insert(struct proc *, struct lwp *, struct pgrp *, struct session *);
+void	proc0_init(void);
 void	proc_free(struct proc *);
 void	proc_free_mem(struct proc *);
 void	exit_lwps(struct lwp *l);

--Boundary-00=_Omb3CzLI+8wu/po--