Subject: Initialising pools using link sets
To: None <tech-kern@netbsd.org>
From: Simon Burge <simonb@wasabisystems.com>
List: tech-kern
Date: 04/07/2004 13:13:46
--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Folks,

I'd like to commit changes to initialise pools from link sets instead of
having explicit pool_init calls sprinkled through the kernel, often from
a foo_init() function that does nothing else.

The motivation for doing this was to poolify a few more things (for
example lockf structures), and then having to a new lock_init() function
that was called from main() just to call pool_init().  Note that not all
calls to pool_init() are changed - the untouched ones are for pools that
are either in arch-specific code, or aren't called during initial system
startup.

The following abbreviated diff shows the changes to:

  kern/init_main.c
	Reduce the number of now needless foo_init() calls.

  kern/kern_prot.c
	A malloc-to-pool conversion without having to add a foo_init()
	function.

  kern/subr_pool.c
	Use link sets to initialise the pools.

  kern/sys_pipe.c
	Existing foo_init() style function converted to using a
	link set for pool initialisation.

  sys/pool.h
	New POOL_INIT macro that sets up the link sets.

A full diff showing all pool_init changes and new malloc-to-pool
conversions for session, cred and lockf is at:

	ftp://ftp.netbsd.org/pub/NetBSD/misc/simonb/pool.diff

Impact on overall kernel size is minimal, but there is a slight
saving:

	text     data    bss    dec      hex    filename
	6496637  144356  495740 7136733  6ce5dd netbsd.old
	6496590  144292  495740 7136622  6ce56e netbsd.new

Comments?

Simon.
--
Simon Burge                                   <simonb@wasabisystems.com>
NetBSD Development, Support and Service:   http://www.wasabisystems.com/

--cNdxnHkX5QqsyA0e
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="pool.diff"

Index: kern/init_main.c
===================================================================
RCS file: /cvsroot/src/sys/kern/init_main.c,v
retrieving revision 1.235
diff -d -p -u -r1.235 init_main.c
--- kern/init_main.c	28 Mar 2004 22:43:56 -0000	1.235
+++ kern/init_main.c	5 Apr 2004 05:46:22 -0000
@@ -111,6 +111,7 @@ __KERNEL_RCSID(0, "$NetBSD: init_main.c,
 #include <sys/disk.h>
 #include <sys/exec.h>
 #include <sys/socketvar.h>
+#include <sys/lockf.h>
 #include <sys/protosw.h>
 #include <sys/reboot.h>
 #include <sys/user.h>
@@ -251,6 +252,9 @@ main(void)
 	/* Do machine-dependent initialization. */
 	cpu_startup();
 
+	/* Initialise pools. */
+	link_pool_init();
+
 	/* Initialize callouts. */
 	callout_startup();
 
@@ -263,9 +267,6 @@ main(void)
 	 */
 	mbinit();
 
-	/* Initialize kqueues. */
-	kqueue_init();
-
 	/* Initialize sockets. */
 	soinit();
 
@@ -290,9 +291,7 @@ main(void)
 	/* Initialize the sysctl subsystem. */
 	sysctl_init();
 
-	/*
-	 * Initialize process and pgrp structures.
-	 */
+	/* Initialize process and pgrp structures. */
 	procinit();
 
 #ifdef LKM
@@ -333,7 +332,6 @@ main(void)
 	p->p_ucred->cr_ngroups = 1;	/* group 0 */
 
 	/* Create the file descriptor table. */
-	finit();
 	p->p_fd = &filedesc0.fd_fd;
 	fdinit1(&filedesc0);
 
@@ -586,11 +584,6 @@ main(void)
 	/* Initialize exec structures */
 	exec_init(1);
 
-#ifndef PIPE_SOCKETPAIR
-	/* Initialize pipe structures */
-	pipe_init();
-#endif
-
 	/*
 	 * Okay, now we can let init(8) exec!  It's off to userland!
 	 */
Index: kern/kern_prot.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_prot.c,v
retrieving revision 1.80
diff -d -p -u -r1.80 kern_prot.c
--- kern/kern_prot.c	7 Aug 2003 16:31:47 -0000	1.80
+++ kern/kern_prot.c	5 Apr 2004 05:46:25 -0000
@@ -52,14 +52,15 @@ __KERNEL_RCSID(0, "$NetBSD: kern_prot.c,
 #include <sys/proc.h>
 #include <sys/timeb.h>
 #include <sys/times.h>
-#include <sys/malloc.h>
+#include <sys/pool.h>
 #include <sys/syslog.h>
 
 #include <sys/mount.h>
 #include <sys/sa.h>
 #include <sys/syscallargs.h>
 
-MALLOC_DEFINE(M_CRED, "cred", "credentials");
+POOL_INIT(cred_pool, sizeof(struct ucred), 0, 0, 0, "credpl", \
+    &pool_allocator_nointr);
 
 int	sys_getpid(struct lwp *, void *, register_t *);
 int	sys_getpid_with_ppid(struct lwp *, void *, register_t *);
@@ -616,8 +617,8 @@ crget(void)
 {
 	struct ucred *cr;
 
-	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
-	memset((caddr_t)cr, 0, sizeof(*cr));
+	cr = pool_get(&cred_pool, PR_WAITOK);
+	memset(cr, 0, sizeof(*cr));
 	cr->cr_ref = 1;
 	return (cr);
 }
@@ -631,7 +632,7 @@ crfree(struct ucred *cr)
 {
 
 	if (--cr->cr_ref == 0)
-		FREE((caddr_t)cr, M_CRED);
+		pool_put(&cred_pool, cr);
 }
 
 /*
Index: kern/subr_pool.c
===================================================================
RCS file: /cvsroot/src/sys/kern/subr_pool.c,v
retrieving revision 1.93
diff -d -p -u -r1.93 subr_pool.c
--- kern/subr_pool.c	8 Mar 2004 22:48:09 -0000	1.93
+++ kern/subr_pool.c	5 Apr 2004 05:46:28 -0000
@@ -357,6 +357,21 @@ pr_rmpage(struct pool *pp, struct pool_i
 }
 
 /*
+ * Initialize all the pools listed in the "pools" link set.
+ */
+void
+link_pool_init(void)
+{
+	__link_set_decl(pools, struct link_pool_init);
+	struct link_pool_init * const *pi;
+
+	__link_set_foreach(pi, pools)
+		pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
+		    (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
+		    (*pi)->palloc);
+}
+
+/*
  * Initialize the given pool resource structure.
  *
  * We export this routine to allow other kernel parts to declare
Index: kern/sys_pipe.c
===================================================================
RCS file: /cvsroot/src/sys/kern/sys_pipe.c,v
retrieving revision 1.55
diff -d -p -u -r1.55 sys_pipe.c
--- kern/sys_pipe.c	24 Mar 2004 20:25:28 -0000	1.55
+++ kern/sys_pipe.c	5 Apr 2004 05:46:37 -0000
@@ -202,7 +202,8 @@ static int pipe_loan_alloc(struct pipe *
 static void pipe_loan_free(struct pipe *);
 #endif /* PIPE_NODIRECT */
 
-static struct pool pipe_pool;
+static POOL_INIT(pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl", \
+    &pool_allocator_nointr);
 
 /*
  * The pipe system call for the DTYPE_PIPE type of pipes
@@ -1507,14 +1508,3 @@ SYSCTL_SETUP(sysctl_kern_pipe_setup, "sy
 		       NULL, 0, &amountpipekva, 0,
 		       CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
 }
-
-/*
- * Initialize pipe structs.
- */
-void
-pipe_init(void)
-{
-
-	pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl",
-	    &pool_allocator_nointr);
-}
Index: sys/pool.h
===================================================================
RCS file: /cvsroot/src/sys/sys/pool.h,v
retrieving revision 1.42
diff -d -p -u -r1.42 pool.h
--- sys/pool.h	9 Jan 2004 19:00:16 -0000	1.42
+++ sys/pool.h	5 Apr 2004 05:46:56 -0000
@@ -216,6 +216,24 @@ struct pool {
 extern struct pool_allocator pool_allocator_kmem;
 extern struct pool_allocator pool_allocator_nointr;
 
+struct link_pool_init {	/* same as args to pool_init() */
+	struct pool *pp;
+	size_t size;
+	u_int align;
+	u_int align_offset;
+	int flags;
+	char *wchan;
+	struct pool_allocator *palloc;
+};
+#define	POOL_INIT(pp, size, align, align_offset, flags, wchan, palloc)	\
+struct pool pp;								\
+static const struct link_pool_init _link_ ## pp[1] = {			\
+	{ &pp, size, align, align_offset, flags, wchan, palloc }	\
+};									\
+__link_set_add_data(pools, _link_ ## pp)
+
+void		link_pool_init(void);
+
 void		pool_init(struct pool *, size_t, u_int, u_int,
 		    int, const char *, struct pool_allocator *);
 void		pool_destroy(struct pool *);

--cNdxnHkX5QqsyA0e--