Subject: Re: Prototype kernel continuation-passing for NetBSD
To: Bill Studenmund <wrstuden@NetBSD.org>
From: Jonathan Stone <jonathan@dsg.stanford.edu>
List: tech-kern
Date: 03/27/2004 01:34:27
For what its worth: here's a patch to eliminate the curproc uses from
syscall handlers down into sosend/socreate; and (mostly) to make them
explicit arguments at lower levels.

I started trying to preserve the exact semantics we have now, with
sosend() and socreate() using curproc in their bodies.  OTOH, the
various local struct proc * values _should_ be correct...

Index: sys/socketvar.h
===================================================================
RCS file: /cvsroot/src/sys/sys/socketvar.h,v
retrieving revision 1.69
diff -w -u -r1.69 socketvar.h
--- sys/socketvar.h	17 Mar 2004 09:58:15 -0000	1.69
+++ sys/socketvar.h	27 Mar 2004 09:27:29 -0000
@@ -122,7 +122,7 @@
 	caddr_t		so_upcallarg;	/* Arg for above */
 	int		(*so_send) __P((struct socket *, struct mbuf *,
 					struct uio *, struct mbuf *,
-					struct mbuf *, int));
+					struct mbuf *, int, struct proc *));
 	int		(*so_receive) __P((struct socket *,
 					struct mbuf **,
 					struct uio *, struct mbuf **,
@@ -308,7 +308,7 @@
 int	soclose(struct socket *);
 int	soconnect(struct socket *, struct mbuf *);
 int	soconnect2(struct socket *, struct socket *);
-int	socreate(int, struct socket **, int, int);
+int	socreate(int, struct socket **, int, int, struct proc *);
 int	sodisconnect(struct socket *);
 void	sofree(struct socket *);
 int	sogetopt(struct socket *, int, int, struct mbuf **);
@@ -327,7 +327,7 @@
 int	soreserve(struct socket *, u_long, u_long);
 void	sorflush(struct socket *);
 int	sosend(struct socket *, struct mbuf *, struct uio *,
-	    struct mbuf *, struct mbuf *, int);
+	    struct mbuf *, struct mbuf *, int, struct proc *);
 int	sosetopt(struct socket *, int, int, struct mbuf *);
 int	soshutdown(struct socket *, int);
 void	sowakeup(struct socket *, struct sockbuf *, int);
Index: compat/svr4/svr4_net.c
===================================================================
RCS file: /cvsroot/src/sys/compat/svr4/svr4_net.c,v
retrieving revision 1.35
diff -w -u -r1.35 svr4_net.c
--- compat/svr4/svr4_net.c	13 Sep 2003 08:32:10 -0000	1.35
+++ compat/svr4/svr4_net.c	27 Mar 2004 09:27:08 -0000
@@ -203,7 +203,7 @@
 	if ((error = falloc(p, &fp, &fd)) != 0)
 		return error;
 
-	if ((error = socreate(family, &so, type, protocol)) != 0) {
+	if ((error = socreate(family, &so, type, protocol, p)) != 0) {
 		DPRINTF(("socreate error %d\n", error));
 		fdremove(p->p_fd, fd);
 		FILE_UNUSE(fp, NULL);
Index: kern/sys_socket.c
===================================================================
RCS file: /cvsroot/src/sys/kern/sys_socket.c,v
retrieving revision 1.39
diff -w -u -r1.39 sys_socket.c
--- kern/sys_socket.c	21 Sep 2003 19:17:08 -0000	1.39
+++ kern/sys_socket.c	27 Mar 2004 09:27:17 -0000
@@ -79,7 +79,7 @@
 {
 	struct socket *so = (struct socket *) fp->f_data;
 	return ((*so->so_send)(so, (struct mbuf *)0,
-		uio, (struct mbuf *)0, (struct mbuf *)0, 0));
+		uio, (struct mbuf *)0, (struct mbuf *)0, 0, curproc /*XXX*/));
 }
 
 int
Index: kern/uipc_socket.c
===================================================================
RCS file: /cvsroot/src/sys/kern/uipc_socket.c,v
retrieving revision 1.97
diff -w -u -r1.97 uipc_socket.c
--- kern/uipc_socket.c	24 Mar 2004 15:34:53 -0000	1.97
+++ kern/uipc_socket.c	27 Mar 2004 09:27:19 -0000
@@ -447,14 +447,12 @@
  */
 /*ARGSUSED*/
 int
-socreate(int dom, struct socket **aso, int type, int proto)
+socreate(int dom, struct socket **aso, int type, int proto, struct proc *p)
 {
-	struct proc	*p;
 	struct protosw	*prp;
 	struct socket	*so;
 	int		error, s;
 
-	p = curproc;		/* XXX */
 	if (proto)
 		prp = pffindproto(dom, proto, type);
 	else
@@ -721,16 +719,14 @@
  */
 int
 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
-	struct mbuf *control, int flags)
+	struct mbuf *control, int flags, struct proc *p)
 {
-	struct proc	*p;
 	struct mbuf	**mp, *m;
 	long		space, len, resid, clen, mlen;
 	int		error, s, dontroute, atomic;
 
 	sodopendfree(so);
 
-	p = curproc;		/* XXX */
 	clen = 0;
 	atomic = sosendallatonce(so) || top;
 	if (uio)
Index: kern/uipc_syscalls.c
===================================================================
RCS file: /cvsroot/src/sys/kern/uipc_syscalls.c,v
retrieving revision 1.86
diff -w -u -r1.86 uipc_syscalls.c
--- kern/uipc_syscalls.c	29 Nov 2003 10:02:42 -0000	1.86
+++ kern/uipc_syscalls.c	27 Mar 2004 09:27:19 -0000
@@ -90,7 +90,7 @@
 	fp->f_type = DTYPE_SOCKET;
 	fp->f_ops = &socketops;
 	error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
-			 SCARG(uap, protocol));
+			 SCARG(uap, protocol), p);
 	if (error) {
 		FILE_UNUSE(fp, p);
 		fdremove(fdp, fd);
@@ -335,11 +335,11 @@
 	p = l->l_proc;
 	fdp = p->p_fd;
 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
-			 SCARG(uap, protocol));
+			 SCARG(uap, protocol), p);
 	if (error)
 		return (error);
 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
-			 SCARG(uap, protocol));
+			 SCARG(uap, protocol), p);
 	if (error)
 		goto free1;
 	/* falloc() will use the descriptor for us */
@@ -530,7 +530,8 @@
 	}
 #endif
 	len = auio.uio_resid;
-	error = (*so->so_send)(so, to, &auio, NULL, control, flags);
+	error = (*so->so_send)(so, to, &auio, NULL, control, flags,
+	    curproc /*XXX p */);
 	if (error) {
 		if (auio.uio_resid != len && (error == ERESTART ||
 		    error == EINTR || error == EWOULDBLOCK))
@@ -902,9 +903,9 @@
 
 	p = l->l_proc;
 	fdp = p->p_fd;
-	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
+	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, p)) != 0)
 		return (error);
-	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
+	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, p)) != 0)
 		goto free1;
 	/* remember this socket pair implements a pipe */
 	wso->so_state |= SS_ISAPIPE;
Index: miscfs/fifofs/fifo_vnops.c
===================================================================
RCS file: /cvsroot/src/sys/miscfs/fifofs/fifo_vnops.c,v
retrieving revision 1.46
diff -w -u -r1.46 fifo_vnops.c
--- miscfs/fifofs/fifo_vnops.c	6 Mar 2004 00:38:29 -0000	1.46
+++ miscfs/fifofs/fifo_vnops.c	27 Mar 2004 09:27:21 -0000
@@ -162,13 +162,13 @@
 	if ((fip = vp->v_fifoinfo) == NULL) {
 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
 		vp->v_fifoinfo = fip;
-		if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
+		if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, p)) != 0) {
 			free(fip, M_VNODE);
 			vp->v_fifoinfo = NULL;
 			return (error);
 		}
 		fip->fi_readsock = rso;
-		if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
+		if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, p)) != 0) {
 			(void)soclose(rso);
 			free(fip, M_VNODE);
 			vp->v_fifoinfo = NULL;
@@ -310,7 +310,7 @@
 		wso->so_state |= SS_NBIO;
 	VOP_UNLOCK(ap->a_vp, 0);
 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
-	    (struct mbuf *)0, 0);
+	    (struct mbuf *)0, 0, curproc);
 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
 	if (ap->a_ioflag & IO_NDELAY)
 		wso->so_state &= ~SS_NBIO;
Index: miscfs/portal/portal_vnops.c
===================================================================
RCS file: /cvsroot/src/sys/miscfs/portal/portal_vnops.c,v
retrieving revision 1.52
diff -w -u -r1.52 portal_vnops.c
--- miscfs/portal/portal_vnops.c	29 Nov 2003 10:02:43 -0000	1.52
+++ miscfs/portal/portal_vnops.c	27 Mar 2004 09:27:22 -0000
@@ -345,7 +345,7 @@
 	/*
 	 * Create a new socket.
 	 */
-	error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0);
+	error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0, curproc /* XXX p*/);
 	if (error)
 		goto bad;
 
@@ -420,7 +420,8 @@
 	auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
 
 	error = (*so->so_send)(so, (struct mbuf *) 0, &auio,
-			(struct mbuf *) 0, (struct mbuf *) 0, 0);
+			(struct mbuf *) 0, (struct mbuf *) 0, 0,
+			curproc /*XXX p */);
 	if (error)
 		goto bad;
 
Index: netsmb/smb_trantcp.c
===================================================================
RCS file: /cvsroot/src/sys/netsmb/smb_trantcp.c,v
retrieving revision 1.15
diff -w -u -r1.15 smb_trantcp.c
--- netsmb/smb_trantcp.c	29 Jun 2003 22:32:11 -0000	1.15
+++ netsmb/smb_trantcp.c	27 Mar 2004 09:27:24 -0000
@@ -83,7 +83,7 @@
 				    so, NULL, 0, m, 0, flags, p)
 #else
 #define nb_sosend(so,m,flags,p) (*(so)->so_send)(so, NULL, (struct uio *)0, \
-					m, (struct mbuf *)0, flags)
+					m, (struct mbuf *)0, flags, p)
 #endif
 
 static int  nbssn_recv(struct nbpcb *nbp, struct mbuf **mpp, int *lenp,
@@ -267,7 +267,8 @@
 	struct mbuf *m;
 #endif
 
-	error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP);
+	error = socreate(AF_INET, &so, SOCK_STREAM, IPPROTO_TCP,
+	    curproc /*XXX p */);
 	if (error)
 		return error;
 	nbp->nbp_tso = so;
Index: nfs/krpc_subr.c
===================================================================
RCS file: /cvsroot/src/sys/nfs/krpc_subr.c,v
retrieving revision 1.27
diff -w -u -r1.27 krpc_subr.c
--- nfs/krpc_subr.c	26 Feb 2003 06:31:18 -0000	1.27
+++ nfs/krpc_subr.c	27 Mar 2004 09:27:24 -0000
@@ -244,7 +244,7 @@
 	/*
 	 * Create socket and set its receive timeout.
 	 */
-	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
+	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, curproc /*XXX*/)))
 		goto out;
 
 	if ((error = nfs_boot_setrecvtimo(so)))
Index: nfs/nfs_boot.c
===================================================================
RCS file: /cvsroot/src/sys/nfs/nfs_boot.c,v
retrieving revision 1.60
diff -w -u -r1.60 nfs_boot.c
--- nfs/nfs_boot.c	11 Mar 2004 21:48:43 -0000	1.60
+++ nfs/nfs_boot.c	27 Mar 2004 09:27:25 -0000
@@ -197,7 +197,7 @@
 	 * Get a socket to use for various things in here.
 	 * After this, use "goto out" to cleanup and return.
 	 */
-	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
+	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, curproc /*XXX*/);
 	if (error) {
 		printf("ifupdown: socreate, error=%d\n", error);
 		return (error);
@@ -246,7 +246,7 @@
 	 * Get a socket to use for various things in here.
 	 * After this, use "goto out" to cleanup and return.
 	 */
-	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
+	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, curproc /*XXX*/);
 	if (error) {
 		printf("setaddress: socreate, error=%d\n", error);
 		return (error);
@@ -305,7 +305,7 @@
 	 * Get a socket to use for various things in here.
 	 * After this, use "goto out" to cleanup and return.
 	 */
-	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
+	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, curproc /*XXX*/);
 	if (error) {
 		printf("deladdress: socreate, error=%d\n", error);
 		return (error);
@@ -434,7 +434,7 @@
 		error = ENOBUFS;
 		goto out;
 	}
-	error = (*so->so_send)(so, nam, NULL, m, NULL, 0);
+	error = (*so->so_send)(so, nam, NULL, m, NULL, 0, curproc /*XXX*/);
 	if (error) {
 		printf("nfs_boot: sosend: %d\n", error);
 		goto out;
Index: nfs/nfs_bootdhcp.c
===================================================================
RCS file: /cvsroot/src/sys/nfs/nfs_bootdhcp.c,v
retrieving revision 1.24
diff -w -u -r1.24 nfs_bootdhcp.c
--- nfs/nfs_bootdhcp.c	29 Jun 2003 22:32:14 -0000	1.24
+++ nfs/nfs_bootdhcp.c	27 Mar 2004 09:27:26 -0000
@@ -451,7 +451,7 @@
 	int vcilen;
 #endif
 
-	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
+	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, curproc /* XXX procp */);
 	if (error) {
 		printf("bootp: socreate, error=%d\n", error);
 		return (error);
Index: nfs/nfs_socket.c
===================================================================
RCS file: /cvsroot/src/sys/nfs/nfs_socket.c,v
retrieving revision 1.102
diff -w -u -r1.102 nfs_socket.c
--- nfs/nfs_socket.c	17 Mar 2004 10:40:34 -0000	1.102
+++ nfs/nfs_socket.c	27 Mar 2004 09:27:28 -0000
@@ -169,8 +169,8 @@
 
 	nmp->nm_so = (struct socket *)0;
 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
-	error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype, 
-		nmp->nm_soproto);
+	error = socreate(saddr->sa_family, &nmp->nm_so,
+		nmp->nm_sotype, nmp->nm_soproto, curproc /*XXX*/);
 	if (error)
 		goto bad;
 	so = nmp->nm_so;
@@ -453,8 +453,8 @@
 	else
 		flags = 0;
 
-	error = (*so->so_send)(so, sendnam, (struct uio *)0, top,
-		(struct mbuf *)0, flags);
+	error = (*so->so_send)(so, sendnam, (struct uio *)0,
+		top, (struct mbuf *)0, flags,  curproc/*XXX*/);
 	if (error) {
 		if (rep) {
 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {