Module Name:    src
Committed By:   martin
Date:           Thu Nov  2 10:31:55 UTC 2023

Modified Files:
        src/sys/ddb: db_command.c db_interface.h db_xxx.c
        src/sys/kern: kern_condvar.c kern_sleepq.c sys_pipe.c uipc_socket.c
            uipc_syscalls.c
        src/sys/rump/librump/rumpkern: locks.c locks_up.c
        src/sys/sys: condvar.h lwp.h pipe.h socketvar.h
        src/usr.bin/fstat: fstat.c

Log Message:
Back out the following revisions on behalf of core:

        sys/sys/lwp.h: revision 1.228
        sys/sys/pipe.h: revision 1.40
        sys/kern/uipc_socket.c: revision 1.306
        sys/kern/kern_sleepq.c: revision 1.84
        sys/rump/librump/rumpkern/locks_up.c: revision 1.13
        sys/kern/sys_pipe.c: revision 1.165
        usr.bin/fstat/fstat.c: revision 1.119
        sys/rump/librump/rumpkern/locks.c: revision 1.87
        sys/ddb/db_xxx.c: revision 1.78
        sys/ddb/db_command.c: revision 1.187
        sys/sys/condvar.h: revision 1.18
        sys/ddb/db_interface.h: revision 1.42
        sys/sys/socketvar.h: revision 1.166
        sys/kern/uipc_syscalls.c: revision 1.209
        sys/kern/kern_condvar.c: revision 1.60

  Add cv_fdrestart() [...]
  Use cv_fdrestart() to implement fo_restart.
  Simplify/streamline pipes a little bit [...]

This changes have caused regressions and need to be debugged.
The cv_fdrestart() addition needs more discussion.


To generate a diff of this commit:
cvs rdiff -u -r1.189 -r1.190 src/sys/ddb/db_command.c
cvs rdiff -u -r1.42 -r1.43 src/sys/ddb/db_interface.h
cvs rdiff -u -r1.79 -r1.80 src/sys/ddb/db_xxx.c
cvs rdiff -u -r1.62 -r1.63 src/sys/kern/kern_condvar.c
cvs rdiff -u -r1.86 -r1.87 src/sys/kern/kern_sleepq.c
cvs rdiff -u -r1.165 -r1.166 src/sys/kern/sys_pipe.c
cvs rdiff -u -r1.306 -r1.307 src/sys/kern/uipc_socket.c
cvs rdiff -u -r1.209 -r1.210 src/sys/kern/uipc_syscalls.c
cvs rdiff -u -r1.87 -r1.88 src/sys/rump/librump/rumpkern/locks.c
cvs rdiff -u -r1.13 -r1.14 src/sys/rump/librump/rumpkern/locks_up.c
cvs rdiff -u -r1.18 -r1.19 src/sys/sys/condvar.h
cvs rdiff -u -r1.230 -r1.231 src/sys/sys/lwp.h
cvs rdiff -u -r1.41 -r1.42 src/sys/sys/pipe.h
cvs rdiff -u -r1.166 -r1.167 src/sys/sys/socketvar.h
cvs rdiff -u -r1.119 -r1.120 src/usr.bin/fstat/fstat.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/ddb/db_command.c
diff -u src/sys/ddb/db_command.c:1.189 src/sys/ddb/db_command.c:1.190
--- src/sys/ddb/db_command.c:1.189	Sun Oct 15 10:27:33 2023
+++ src/sys/ddb/db_command.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_command.c,v 1.189 2023/10/15 10:27:33 riastradh Exp $	*/
+/*	$NetBSD: db_command.c,v 1.190 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2009, 2019
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.189 2023/10/15 10:27:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.190 2023/11/02 10:31:55 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_aio.h"
@@ -301,8 +301,6 @@ static const struct db_command db_show_c
 	    0 ,"List all used memory pages.",NULL,NULL) },
 	{ DDB_ADD_CMD("panic",	db_show_panic,	0,
 	    "Print the current panic string",NULL,NULL) },
-	{ DDB_ADD_CMD("pipe", db_show_pipe,
-	    0 ,"Show the contents of a pipe.",NULL,NULL) },
 	{ DDB_ADD_CMD("pool",	db_pool_print_cmd,	0,
 	    "Print the pool at address.", "[/clp] address",NULL) },
 	/* added from all sub cmds */

Index: src/sys/ddb/db_interface.h
diff -u src/sys/ddb/db_interface.h:1.42 src/sys/ddb/db_interface.h:1.43
--- src/sys/ddb/db_interface.h:1.42	Fri Oct 13 19:07:08 2023
+++ src/sys/ddb/db_interface.h	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_interface.h,v 1.42 2023/10/13 19:07:08 ad Exp $	*/
+/*	$NetBSD: db_interface.h,v 1.43 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 1995, 2023 The NetBSD Foundation, Inc.
@@ -86,9 +86,6 @@ void		db_show_sleepq(db_expr_t, bool, db
 /* kern/kern_condvar.c */
 void		db_show_condvar(db_expr_t, bool, db_expr_t, const char *);
 
-/* kern/sys_pipe.c */
-void		db_show_pipe(db_expr_t, bool, db_expr_t, const char *);
-
 /* kern/sys_select.c */
 void		db_show_selinfo(db_expr_t, bool, db_expr_t, const char *);
 

Index: src/sys/ddb/db_xxx.c
diff -u src/sys/ddb/db_xxx.c:1.79 src/sys/ddb/db_xxx.c:1.80
--- src/sys/ddb/db_xxx.c:1.79	Sun Oct 15 10:40:52 2023
+++ src/sys/ddb/db_xxx.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_xxx.c,v 1.79 2023/10/15 10:40:52 martin Exp $	*/
+/*	$NetBSD: db_xxx.c,v 1.80 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.79 2023/10/15 10:40:52 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.80 2023/11/02 10:31:55 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kgdb.h"
@@ -72,7 +72,6 @@ __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1
 #include <sys/condvar.h>
 #include <sys/sleepq.h>
 #include <sys/selinfo.h>
-#include <sys/pipe.h>
 
 #include <ddb/ddb.h>
 #include <ddb/db_user.h>
@@ -367,51 +366,6 @@ db_show_sleepq(db_expr_t addr, bool hadd
 }
 
 void
-db_show_pipe(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
-{
-	struct pipe pipe, *ppipe = (struct pipe *)(uintptr_t)addr;
-
-	db_read_bytes(addr, sizeof(pipe), (char *)&pipe);
-
-	db_printf("pipe_lock\t\t%p\n", pipe.pipe_lock);
-
-	db_printf("pipe_read\t\t");
-	db_show_condvar((db_addr_t)&ppipe->pipe_read, false, 0, modif);
-
-	db_printf("pipe_write\t\t");
-	db_show_condvar((db_addr_t)&ppipe->pipe_write, false, 0, modif);
-
-	db_printf("pipe_busy\t\t");
-	db_show_condvar((db_addr_t)&ppipe->pipe_busy, false, 0, modif);
-
-	db_printf("pipe_buffer.cnt\t\t%ld\n", (long)pipe.pipe_buffer.cnt);
-	db_printf("pipe_buffer.in\t\t%d\n", pipe.pipe_buffer.in);
-	db_printf("pipe_buffer.out\t\t%d\n", pipe.pipe_buffer.out);
-	db_printf("pipe_buffer.size\t%ld\n", (long)pipe.pipe_buffer.size);
-	db_printf("pipe_buffer.buffer\t%p\n", pipe.pipe_buffer.buffer);
-
-	db_printf("pipe_wrsel\t\t");
-	db_show_selinfo((db_addr_t)&ppipe->pipe_wrsel, false, 0, modif);
-	db_printf("pipe_rdsel\t\t");
-	db_show_selinfo((db_addr_t)&ppipe->pipe_rdsel, false, 0, modif);
-
-	db_printf("pipe_atime\t\t");
-	db_print_timespec(&pipe.pipe_atime);
-
-	db_printf("\npipe_mtime\t\t");
-	db_print_timespec(&pipe.pipe_mtime);
-
-	db_printf("\npipe_btime\t\t");
-	db_print_timespec(&pipe.pipe_btime);
-
-	db_printf("\npipe_kmem\t\t%lx\n", (long)pipe.pipe_kmem);
-	db_printf("pipe_owner\t\t%p\n", pipe.pipe_owner);
-	db_printf("pipe_wrpgid\t\t%d\n", pipe.pipe_wrpgid);
-	db_printf("pipe_rdpgid\t\t%d\n", pipe.pipe_rdpgid);
-	db_printf("pipe_state\t\t%#08x\n", pipe.pipe_state);
-}
-
-void
 db_show_selinfo(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
 {
 	struct selinfo sel;

Index: src/sys/kern/kern_condvar.c
diff -u src/sys/kern/kern_condvar.c:1.62 src/sys/kern/kern_condvar.c:1.63
--- src/sys/kern/kern_condvar.c:1.62	Sun Oct 15 10:28:00 2023
+++ src/sys/kern/kern_condvar.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_condvar.c,v 1.62 2023/10/15 10:28:00 riastradh Exp $	*/
+/*	$NetBSD: kern_condvar.c,v 1.63 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019, 2020, 2023
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.62 2023/10/15 10:28:00 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.63 2023/11/02 10:31:55 martin Exp $");
 
 #include <sys/param.h>
 
@@ -545,43 +545,6 @@ cv_wakeup_all(kcondvar_t *cv)
 }
 
 /*
- * cv_fdrestart:
- *
- *	Like cv_broadcast(), but make any LWPs that share the same file
- *	descriptor table as the caller return ERESTART when resuming.  Used
- *	to dislodge LWPs waiting for I/O that prevent a file descriptor from
- *	being closed, without upsetting access to the file (not descriptor)
- *	made from another direction.  Rarely used thus no fast path
- *	provided.
- */
-void
-cv_fdrestart(kcondvar_t *cv)
-{
-	sleepq_t *sq;
-	kmutex_t *mp;
-	lwp_t *l;
-
-	KASSERT(cv_is_valid(cv));
-
-	if (LIST_EMPTY(CV_SLEEPQ(cv)))
-		return;
-
-	mp = sleepq_hashlock(cv);
-	sq = CV_SLEEPQ(cv);
-	while ((l = LIST_FIRST(sq)) != NULL) {
-		KASSERT(l->l_sleepq == sq);
-		KASSERT(l->l_mutex == mp);
-		KASSERT(l->l_wchan == cv);
-		/* l_fd stable at this point so no special locking needed. */
-		if (l->l_fd == curlwp->l_fd) {
-			l->l_flag |= LW_RESTART;
-			sleepq_remove(sq, l, false);
-		}
-	}
-	mutex_spin_exit(mp);
-}
-
-/*
  * cv_has_waiters:
  *
  *	For diagnostic assertions: return non-zero if a condition

Index: src/sys/kern/kern_sleepq.c
diff -u src/sys/kern/kern_sleepq.c:1.86 src/sys/kern/kern_sleepq.c:1.87
--- src/sys/kern/kern_sleepq.c:1.86	Sun Oct 15 10:29:02 2023
+++ src/sys/kern/kern_sleepq.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sleepq.c,v 1.86 2023/10/15 10:29:02 riastradh Exp $	*/
+/*	$NetBSD: kern_sleepq.c,v 1.87 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2009, 2019, 2020, 2023
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.86 2023/10/15 10:29:02 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.87 2023/11/02 10:31:55 martin Exp $");
 
 #include <sys/param.h>
 
@@ -343,7 +343,7 @@ sleepq_uncatch(lwp_t *l)
 int
 sleepq_block(int timo, bool catch_p, syncobj_t *syncobj, int nlocks)
 {
-	const int mask = LW_CANCELLED|LW_WEXIT|LW_WCORE|LW_PENDSIG|LW_RESTART;
+	const int mask = LW_CANCELLED|LW_WEXIT|LW_WCORE|LW_PENDSIG;
 	int error = 0, sig, flag;
 	struct proc *p;
 	lwp_t *l = curlwp;
@@ -360,20 +360,16 @@ sleepq_block(int timo, bool catch_p, syn
 	 * while we are sleeping.  It is independent from LW_SINTR because
 	 * we don't want to leave LW_SINTR set when the LWP is not asleep.
 	 */
-	flag = l->l_flag;
 	if (catch_p) {
-		if ((flag & mask) != 0) {
-			if ((flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
-				l->l_flag = flag & ~LW_CANCELLED;
-				error = EINTR;
-				early = true;
-			} else if ((flag & LW_PENDSIG) != 0 &&
-			    sigispending(l, 0))
-				early = true;
-		}
-		l->l_flag = (flag | LW_CATCHINTR) & ~LW_RESTART;
+		if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
+			l->l_flag &= ~LW_CANCELLED;
+			error = EINTR;
+			early = true;
+		} else if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))
+			early = true;
+		l->l_flag |= LW_CATCHINTR;
 	} else
-		l->l_flag = flag & ~(LW_CATCHINTR | LW_RESTART);
+		l->l_flag &= ~LW_CATCHINTR;
 
 	if (early) {
 		/* lwp_unsleep() will release the lock */
@@ -441,8 +437,7 @@ sleepq_block(int timo, bool catch_p, syn
 			    (sig = issignal(l)) != 0)
 				error = sleepq_sigtoerror(l, sig);
 			mutex_exit(p->p_lock);
-		} else if ((flag & LW_RESTART) != 0)
-			error = ERESTART;
+		}
 	}
 
 	ktrcsw(0, 0, syncobj);

Index: src/sys/kern/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.165 src/sys/kern/sys_pipe.c:1.166
--- src/sys/kern/sys_pipe.c:1.165	Fri Oct 13 19:07:08 2023
+++ src/sys/kern/sys_pipe.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.165 2023/10/13 19:07:08 ad Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.166 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009, 2023 The NetBSD Foundation, Inc.
@@ -55,7 +55,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.165 2023/10/13 19:07:08 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.166 2023/11/02 10:31:55 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -126,42 +126,57 @@ static u_int	nbigpipe = 0;
  */
 static u_int	amountpipekva = 0;
 
-static bool	pipebusy(struct pipe *);
-static bool	pipeunbusy(struct pipe *);
-static void	pipeselwakeup(struct pipe *, int, int);
+static void	pipeclose(struct pipe *);
+static void	pipe_free_kmem(struct pipe *);
+static int	pipe_create(struct pipe **, pool_cache_t, struct timespec *);
+static int	pipelock(struct pipe *, bool);
+static inline void pipeunlock(struct pipe *);
+static void	pipeselwakeup(struct pipe *, struct pipe *, int);
+static int	pipespace(struct pipe *, int);
 static int	pipe_ctor(void *, void *, int);
 static void	pipe_dtor(void *, void *);
 
-static pool_cache_t	pipe_cache __read_mostly;
+static pool_cache_t	pipe_wr_cache;
+static pool_cache_t	pipe_rd_cache;
 
 void
 pipe_init(void)
 {
 
-	pipe_cache = pool_cache_init(sizeof(struct pipe), COHERENCY_UNIT, 0, 0,
-	    "pipe", NULL, IPL_NONE, pipe_ctor, pipe_dtor, NULL);
-	KASSERT(pipe_cache != NULL);
+	/* Writer side is not automatically allocated KVA. */
+	pipe_wr_cache = pool_cache_init(sizeof(struct pipe), 0, 0, 0, "pipewr",
+	    NULL, IPL_NONE, pipe_ctor, pipe_dtor, NULL);
+	KASSERT(pipe_wr_cache != NULL);
+
+	/* Reader side gets preallocated KVA. */
+	pipe_rd_cache = pool_cache_init(sizeof(struct pipe), 0, 0, 0, "piperd",
+	    NULL, IPL_NONE, pipe_ctor, pipe_dtor, (void *)1);
+	KASSERT(pipe_rd_cache != NULL);
 }
 
 static int
 pipe_ctor(void *arg, void *obj, int flags)
 {
-	struct pipe *pipe = obj;
+	struct pipe *pipe;
+	vaddr_t va;
+
+	pipe = obj;
 
 	memset(pipe, 0, sizeof(struct pipe));
-	pipe->pipe_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
-	cv_init(&pipe->pipe_read, "piperd");
-	cv_init(&pipe->pipe_write, "pipewr");
-	cv_init(&pipe->pipe_busy, "pipebusy");
-	selinit(&pipe->pipe_rdsel);
-	selinit(&pipe->pipe_wrsel);
-	pipe->pipe_kmem = uvm_km_alloc(kernel_map, PIPE_SIZE, 0,
-	    UVM_KMF_PAGEABLE | UVM_KMF_WAITVA);
-	pipe->pipe_state = PIPE_SIGNALR | PIPE_RDOPEN | PIPE_WROPEN;
-	pipe->pipe_buffer.buffer = (void *)pipe->pipe_kmem;
-	pipe->pipe_buffer.size = PIPE_SIZE;
-	KASSERT(pipe->pipe_kmem != 0);
-	atomic_add_int(&amountpipekva, PIPE_SIZE);
+	if (arg != NULL) {
+		/* Preallocate space. */
+		va = uvm_km_alloc(kernel_map, PIPE_SIZE, 0,
+		    UVM_KMF_PAGEABLE | UVM_KMF_WAITVA);
+		KASSERT(va != 0);
+		pipe->pipe_kmem = va;
+		atomic_add_int(&amountpipekva, PIPE_SIZE);
+	}
+	cv_init(&pipe->pipe_rcv, "pipe_rd");
+	cv_init(&pipe->pipe_wcv, "pipe_wr");
+	cv_init(&pipe->pipe_draincv, "pipe_drn");
+	cv_init(&pipe->pipe_lkcv, "pipe_lk");
+	selinit(&pipe->pipe_sel);
+	pipe->pipe_state = PIPE_SIGNALR;
 
 	return 0;
 }
@@ -169,16 +184,20 @@ pipe_ctor(void *arg, void *obj, int flag
 static void
 pipe_dtor(void *arg, void *obj)
 {
-	struct pipe *pipe = obj;
+	struct pipe *pipe;
 
-	cv_destroy(&pipe->pipe_read);
-	cv_destroy(&pipe->pipe_write);
-	cv_destroy(&pipe->pipe_busy);
-	seldestroy(&pipe->pipe_rdsel);
-	seldestroy(&pipe->pipe_wrsel);
-	mutex_obj_free(pipe->pipe_lock);
-	uvm_km_free(kernel_map, pipe->pipe_kmem, PIPE_SIZE, UVM_KMF_PAGEABLE);
-	atomic_add_int(&amountpipekva, -PIPE_SIZE);
+	pipe = obj;
+
+	cv_destroy(&pipe->pipe_rcv);
+	cv_destroy(&pipe->pipe_wcv);
+	cv_destroy(&pipe->pipe_draincv);
+	cv_destroy(&pipe->pipe_lkcv);
+	seldestroy(&pipe->pipe_sel);
+	if (pipe->pipe_kmem != 0) {
+		uvm_km_free(kernel_map, pipe->pipe_kmem, PIPE_SIZE,
+		    UVM_KMF_PAGEABLE);
+		atomic_add_int(&amountpipekva, -PIPE_SIZE);
+	}
 }
 
 /*
@@ -187,7 +206,8 @@ pipe_dtor(void *arg, void *obj)
 int
 pipe1(struct lwp *l, int *fildes, int flags)
 {
-	struct pipe *pipe;
+	struct pipe *rpipe, *wpipe;
+	struct timespec nt;
 	file_t *rf, *wf;
 	int fd, error;
 	proc_t *p;
@@ -195,105 +215,163 @@ pipe1(struct lwp *l, int *fildes, int fl
 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
 		return EINVAL;
 	p = curproc;
-
-	pipe = pool_cache_get(pipe_cache, PR_WAITOK);
-	getnanotime(&pipe->pipe_atime);
-	pipe->pipe_mtime = pipe->pipe_atime;
-	pipe->pipe_btime = pipe->pipe_atime;
+	rpipe = wpipe = NULL;
+	getnanotime(&nt);
+	if ((error = pipe_create(&rpipe, pipe_rd_cache, &nt)) ||
+	    (error = pipe_create(&wpipe, pipe_wr_cache, &nt))) {
+		goto free2;
+	}
+	rpipe->pipe_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
+	wpipe->pipe_lock = rpipe->pipe_lock;
+	mutex_obj_hold(wpipe->pipe_lock);
 
 	error = fd_allocfile(&rf, &fd);
-	if (error) {
-		pool_cache_put(pipe_cache, pipe);
-		return error;
-	}
+	if (error)
+		goto free2;
 	fildes[0] = fd;
 
 	error = fd_allocfile(&wf, &fd);
-	if (error) {
-		fd_abort(p, rf, fildes[0]);
-		pool_cache_put(pipe_cache, pipe);
-		return error;
-	}
+	if (error)
+		goto free3;
 	fildes[1] = fd;
 
 	rf->f_flag = FREAD | flags;
 	rf->f_type = DTYPE_PIPE;
-	rf->f_pipe = pipe;
+	rf->f_pipe = rpipe;
 	rf->f_ops = &pipeops;
 	fd_set_exclose(l, fildes[0], (flags & O_CLOEXEC) != 0);
 
 	wf->f_flag = FWRITE | flags;
 	wf->f_type = DTYPE_PIPE;
-	wf->f_pipe = pipe;
+	wf->f_pipe = wpipe;
 	wf->f_ops = &pipeops;
 	fd_set_exclose(l, fildes[1], (flags & O_CLOEXEC) != 0);
 
+	rpipe->pipe_peer = wpipe;
+	wpipe->pipe_peer = rpipe;
+
 	fd_affix(p, rf, fildes[0]);
 	fd_affix(p, wf, fildes[1]);
-	return 0;
+	return (0);
+free3:
+	fd_abort(p, rf, fildes[0]);
+free2:
+	pipeclose(wpipe);
+	pipeclose(rpipe);
+
+	return (error);
 }
 
 /*
- * Busy a pipe for I/O, blocking other access.  Called with pipe lock held.
- * NB: curlwp may already hold the pipe busy.
+ * Allocate kva for pipe circular buffer, the space is pageable
+ * This routine will 'realloc' the size of a pipe safely, if it fails
+ * it will retain the old buffer.
+ * If it fails it will return ENOMEM.
  */
-static bool
-pipebusy(struct pipe *pipe)
+static int
+pipespace(struct pipe *pipe, int size)
 {
-	struct lwp *l = curlwp;
-	bool blocked = false;
+	void *buffer;
 
-	KASSERT(mutex_owned(pipe->pipe_lock));
-
-	if (pipe->pipe_owner != l) {
-		while (__predict_false(pipe->pipe_owner != NULL)) {
-			cv_wait(&pipe->pipe_busy, pipe->pipe_lock);
-			blocked = true;
-		}
-		pipe->pipe_owner = l;
+	/*
+	 * Allocate pageable virtual address space.  Physical memory is
+	 * allocated on demand.
+	 */
+	if (size == PIPE_SIZE && pipe->pipe_kmem != 0) {
+		buffer = (void *)pipe->pipe_kmem;
+	} else {
+		buffer = (void *)uvm_km_alloc(kernel_map, round_page(size),
+		    0, UVM_KMF_PAGEABLE);
+		if (buffer == NULL)
+			return (ENOMEM);
+		atomic_add_int(&amountpipekva, size);
 	}
 
-	return blocked;
+	/* free old resources if we're resizing */
+	pipe_free_kmem(pipe);
+	pipe->pipe_buffer.buffer = buffer;
+	pipe->pipe_buffer.size = size;
+	pipe->pipe_buffer.in = 0;
+	pipe->pipe_buffer.out = 0;
+	pipe->pipe_buffer.cnt = 0;
+	return (0);
 }
 
 /*
- * Unbusy a pipe for I/O, if held busy by curlwp.
+ * Initialize and allocate VM and memory for pipe.
  */
-static bool
-pipeunbusy(struct pipe *pipe)
+static int
+pipe_create(struct pipe **pipep, pool_cache_t cache, struct timespec *nt)
 {
+	struct pipe *pipe;
+	int error;
 
-	KASSERT(mutex_owned(pipe->pipe_lock));
-
-	if (pipe->pipe_owner == curlwp) {
-		pipe->pipe_owner = NULL;
-		return true;
-	} else
-		return false;
+	pipe = pool_cache_get(cache, PR_WAITOK);
+	KASSERT(pipe != NULL);
+	*pipep = pipe;
+	error = 0;
+	pipe->pipe_atime = pipe->pipe_mtime = pipe->pipe_btime = *nt;
+	pipe->pipe_lock = NULL;
+	if (cache == pipe_rd_cache) {
+		error = pipespace(pipe, PIPE_SIZE);
+	} else {
+		pipe->pipe_buffer.buffer = NULL;
+		pipe->pipe_buffer.size = 0;
+		pipe->pipe_buffer.in = 0;
+		pipe->pipe_buffer.out = 0;
+		pipe->pipe_buffer.cnt = 0;
+	}
+	return error;
 }
 
 /*
- * Select/poll wakeup.  This also sends SIGIO to peer.
+ * Lock a pipe for I/O, blocking other access
+ * Called with pipe spin lock held.
  */
-static void
-pipeselwakeup(struct pipe *pipe, int side, int code)
+static int
+pipelock(struct pipe *pipe, bool catch_p)
 {
-	struct selinfo *selp;
-	int band, flag;
-	pid_t pgid;
+	int error;
 
 	KASSERT(mutex_owned(pipe->pipe_lock));
 
-	if (side == FREAD) {
-		selp = &pipe->pipe_rdsel;
-		pgid = pipe->pipe_rdpgid;
-		flag = PIPE_RDASYNC;
-	} else {
-		selp = &pipe->pipe_wrsel;
-		pgid = pipe->pipe_wrpgid;
-		flag = PIPE_WRASYNC;
+	while (pipe->pipe_state & PIPE_LOCKFL) {
+		if (catch_p) {
+			error = cv_wait_sig(&pipe->pipe_lkcv, pipe->pipe_lock);
+			if (error != 0) {
+				return error;
+			}
+		} else
+			cv_wait(&pipe->pipe_lkcv, pipe->pipe_lock);
 	}
 
+	pipe->pipe_state |= PIPE_LOCKFL;
+
+	return 0;
+}
+
+/*
+ * unlock a pipe I/O lock
+ */
+static inline void
+pipeunlock(struct pipe *pipe)
+{
+
+	KASSERT(pipe->pipe_state & PIPE_LOCKFL);
+
+	pipe->pipe_state &= ~PIPE_LOCKFL;
+	cv_signal(&pipe->pipe_lkcv);
+}
+
+/*
+ * Select/poll wakup. This also sends SIGIO to peer connected to
+ * 'sigpipe' side of pipe.
+ */
+static void
+pipeselwakeup(struct pipe *selp, struct pipe *sigp, int code)
+{
+	int band;
+
 	switch (code) {
 	case POLL_IN:
 		band = POLLIN|POLLRDNORM;
@@ -315,22 +393,26 @@ pipeselwakeup(struct pipe *pipe, int sid
 		break;
 	}
 
-	selnotify(selp, band, NOTE_SUBMIT);
+	selnotify(&selp->pipe_sel, band, NOTE_SUBMIT);
+
+	if (sigp == NULL || (sigp->pipe_state & PIPE_ASYNC) == 0)
+		return;
 
-	if (pgid != 0 && (pipe->pipe_state & flag) != 0)
-		fownsignal(pgid, SIGIO, code, band, pipe);
+	fownsignal(sigp->pipe_pgid, SIGIO, code, band, selp);
 }
 
 static int
 pipe_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
     int flags)
 {
-	struct pipe *pipe = fp->f_pipe;
-	struct pipebuf *bp = &pipe->pipe_buffer;
-	size_t size, cnt, ocnt, nread = 0;
-	kmutex_t *lock = pipe->pipe_lock;
-	int error = 0;
-	bool unbusy;
+	struct pipe *rpipe = fp->f_pipe;
+	struct pipebuf *bp = &rpipe->pipe_buffer;
+	kmutex_t *lock = rpipe->pipe_lock;
+	int error;
+	size_t nread = 0;
+	size_t size;
+	size_t ocnt;
+	unsigned int wakeup_state = 0;
 
 	/*
 	 * Try to avoid locking the pipe if we have nothing to do.
@@ -343,30 +425,32 @@ pipe_read(file_t *fp, off_t *offset, str
 	 */
 	if ((fp->f_flag & FNONBLOCK) != 0) {
 		if (__predict_false(uio->uio_resid == 0))
-			return 0;
+			return (0);
 		if (atomic_load_relaxed(&bp->cnt) == 0 &&
-		    (atomic_load_relaxed(&pipe->pipe_state) & PIPE_EOF) == 0)
-			return EAGAIN;
+		    (atomic_load_relaxed(&rpipe->pipe_state) & PIPE_EOF) == 0)
+			return (EAGAIN);
 	}
 
 	mutex_enter(lock);
+	++rpipe->pipe_busy;
 	ocnt = bp->cnt;
 
+again:
+	error = pipelock(rpipe, true);
+	if (error)
+		goto unlocked_error;
+
 	while (uio->uio_resid) {
 		/*
 		 * Normal pipe buffer receive.
 		 */
 		if (bp->cnt > 0) {
-			/* If pipebusy() blocked then re-validate. */
-			if (pipebusy(pipe))
-				continue;
 			size = bp->size - bp->out;
 			if (size > bp->cnt)
 				size = bp->cnt;
 			if (size > uio->uio_resid)
 				size = uio->uio_resid;
 
-			KASSERT(pipe->pipe_owner == curlwp);
 			mutex_exit(lock);
 			error = uiomove((char *)bp->buffer + bp->out, size, uio);
 			mutex_enter(lock);
@@ -376,6 +460,7 @@ pipe_read(file_t *fp, off_t *offset, str
 			bp->out += size;
 			if (bp->out >= bp->size)
 				bp->out = 0;
+
 			bp->cnt -= size;
 
 			/*
@@ -401,117 +486,142 @@ pipe_read(file_t *fp, off_t *offset, str
 		 * Detect EOF condition.
 		 * Read returns 0 on EOF, no need to set error.
 		 */
-		if ((pipe->pipe_state & PIPE_EOF) != 0)
+		if (rpipe->pipe_state & PIPE_EOF)
 			break;
 
 		/*
 		 * Don't block on non-blocking I/O.
 		 */
-		if ((fp->f_flag & FNONBLOCK) != 0) {
+		if (fp->f_flag & FNONBLOCK) {
 			error = EAGAIN;
 			break;
 		}
 
 		/*
-		 * Awaken the other side (including select/poll/kqueue)
-		 * then sleep ASAP to minimise contention.
+		 * Unlock the pipe buffer for our remaining processing.
+		 * We will either break out with an error or we will
+		 * sleep and relock to loop.
 		 */
-		pipeselwakeup(pipe, FWRITE, POLL_OUT);
-		if (pipeunbusy(pipe))
-			cv_signal(&pipe->pipe_busy);
-		cv_broadcast(&pipe->pipe_write);
-		if ((error = cv_wait_sig(&pipe->pipe_read, lock)) != 0)
-			break;
+		pipeunlock(rpipe);
+
+#if 1   /* XXX (dsl) I'm sure these aren't needed here ... */
+		/*
+		 * We want to read more, wake up select/poll.
+		 */
+		pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_OUT);
+
+		/*
+		 * If the "write-side" is blocked, wake it up now.
+		 */
+		cv_broadcast(&rpipe->pipe_wcv);
+#endif
+
+		if (wakeup_state & PIPE_RESTART) {
+			error = ERESTART;
+			goto unlocked_error;
+		}
+
+		/* Now wait until the pipe is filled */
+		error = cv_wait_sig(&rpipe->pipe_rcv, lock);
+		if (error != 0)
+			goto unlocked_error;
+		wakeup_state = rpipe->pipe_state;
+		goto again;
 	}
 
-	/*
-	 * Update timestamp and drop the long term lock (if held).
-	 */
 	if (error == 0)
-		getnanotime(&pipe->pipe_atime);
-	unbusy = pipeunbusy(pipe);
+		getnanotime(&rpipe->pipe_atime);
+	pipeunlock(rpipe);
+
+unlocked_error:
+	--rpipe->pipe_busy;
+	if (rpipe->pipe_busy == 0) {
+		rpipe->pipe_state &= ~PIPE_RESTART;
+		cv_broadcast(&rpipe->pipe_draincv);
+	}
+	if (bp->cnt < MINPIPESIZE) {
+		cv_broadcast(&rpipe->pipe_wcv);
+	}
 
 	/*
 	 * If anything was read off the buffer, signal to the writer it's
 	 * possible to write more data. Also send signal if we are here for the
 	 * first time after last write.
 	 */
-	cnt = bp->cnt;
-	if (bp->size - cnt >= PIPE_BUF
-	    && (ocnt != cnt || (pipe->pipe_state & PIPE_SIGNALR) != 0)) {
-		pipe->pipe_state &= ~PIPE_SIGNALR;
-		pipeselwakeup(pipe, FWRITE, POLL_OUT);
+	if ((bp->size - bp->cnt) >= PIPE_BUF
+	    && (ocnt != bp->cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
+		pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_OUT);
+		rpipe->pipe_state &= ~PIPE_SIGNALR;
 	}
 
-	/*
-	 * Release the mutex and only then wake the other side, to minimise
-	 * contention.
-	 */
 	mutex_exit(lock);
-	if (unbusy)
-		cv_signal(&pipe->pipe_busy);
-	if (cnt < MINPIPESIZE)
-		cv_broadcast(&pipe->pipe_write);
-
-	return error;
+	return (error);
 }
 
 static int
 pipe_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
     int flags)
 {
-	struct pipe *pipe = fp->f_pipe;
-	struct pipebuf *bp = &pipe->pipe_buffer;
-	kmutex_t *lock = pipe->pipe_lock;
-	size_t cnt, space, orig_resid = uio->uio_resid;
-	bool unbusy;
+	struct pipe *wpipe, *rpipe;
+	struct pipebuf *bp;
+	kmutex_t *lock;
 	int error;
+	unsigned int wakeup_state = 0;
+
+	/* We want to write to our peer */
+	rpipe = fp->f_pipe;
+	lock = rpipe->pipe_lock;
+	error = 0;
+
+	mutex_enter(lock);
+	wpipe = rpipe->pipe_peer;
+
+	/*
+	 * Detect loss of pipe read side, issue SIGPIPE if lost.
+	 */
+	if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) != 0) {
+		mutex_exit(lock);
+		return EPIPE;
+	}
+	++wpipe->pipe_busy;
+
+	/* Acquire the long-term pipe lock */
+	if ((error = pipelock(wpipe, true)) != 0) {
+		--wpipe->pipe_busy;
+		if (wpipe->pipe_busy == 0) {
+			wpipe->pipe_state &= ~PIPE_RESTART;
+			cv_broadcast(&wpipe->pipe_draincv);
+		}
+		mutex_exit(lock);
+		return (error);
+	}
+
+	bp = &wpipe->pipe_buffer;
 
 	/*
 	 * If it is advantageous to resize the pipe buffer, do so.
 	 */
-	mutex_enter(lock);
-	if (uio->uio_resid > PIPE_SIZE &&
-	    (pipe->pipe_state & PIPE_RESIZED) == 0 &&
-	    nbigpipe < maxbigpipes && bp->cnt == 0) {
-	    	size_t size = round_page(BIG_PIPE_SIZE);
-		void *buffer = (void *)uvm_km_alloc(kernel_map, size,
-		    0, UVM_KMF_PAGEABLE);
-		if (buffer != NULL) {
-			atomic_add_int(&amountpipekva, size);
+	if ((uio->uio_resid > PIPE_SIZE) &&
+	    (nbigpipe < maxbigpipes) &&
+	    (bp->size <= PIPE_SIZE) && (bp->cnt == 0)) {
+
+		if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
 			atomic_inc_uint(&nbigpipe);
-			pipe->pipe_buffer.buffer = buffer;
-			pipe->pipe_buffer.size = size;
-			pipe->pipe_buffer.in = 0;
-			pipe->pipe_buffer.out = 0;
-			pipe->pipe_buffer.cnt = 0;
-		}
-		pipe->pipe_state |= PIPE_RESIZED;
 	}
 
-	while (uio->uio_resid > 0) {
-		/*
-		 * If read side has gone away, we just issue a signal to
-		 * ourselves.
-		 */
-		if ((pipe->pipe_state & PIPE_EOF) != 0) {
-			error = EPIPE;
-			break;
-		}
+	while (uio->uio_resid) {
+		size_t space;
 
-		/* Writes of size <= PIPE_BUF must be atomic. */
 		space = bp->size - bp->cnt;
-		if (space < uio->uio_resid && uio->uio_resid <= PIPE_BUF)
+
+		/* Writes of size <= PIPE_BUF must be atomic. */
+		if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
 			space = 0;
 
 		if (space > 0) {
 			int size;	/* Transfer size */
 			int segsize;	/* first segment to transfer */
 
-			/* If pipebusy() blocked then re-validate. */
-			if (pipebusy(pipe))
-				continue;
-
 			/*
 			 * Transfer size is minimum of uio transfer
 			 * and free space in pipe buffer.
@@ -532,7 +642,6 @@ pipe_write(file_t *fp, off_t *offset, st
 				segsize = size;
 
 			/* Transfer first segment */
-			KASSERT(pipe->pipe_owner == curlwp);
 			mutex_exit(lock);
 			error = uiomove((char *)bp->buffer + bp->in, segsize,
 			    uio);
@@ -559,63 +668,83 @@ pipe_write(file_t *fp, off_t *offset, st
 
 			bp->cnt += size;
 			KASSERT(bp->cnt <= bp->size);
-			continue;
-		}
+			wakeup_state = 0;
+		} else {
+			/*
+			 * If the "read-side" has been blocked, wake it up now.
+			 */
+			cv_broadcast(&wpipe->pipe_rcv);
 
-		/*
-		 * Don't block on non-blocking I/O.
-		 */
-		if ((fp->f_flag & FNONBLOCK) != 0) {
-			error = EAGAIN;
-			break;
+			/*
+			 * Don't block on non-blocking I/O.
+			 */
+			if (fp->f_flag & FNONBLOCK) {
+				error = EAGAIN;
+				break;
+			}
+
+			/*
+			 * We have no more space and have something to offer,
+			 * wake up select/poll.
+			 */
+			if (bp->cnt)
+				pipeselwakeup(wpipe, wpipe, POLL_IN);
+
+			if (wakeup_state & PIPE_RESTART) {
+				error = ERESTART;
+				break;
+			}
+
+			/*
+			 * If read side wants to go away, we just issue a signal
+			 * to ourselves.
+			 */
+			if (wpipe->pipe_state & PIPE_EOF) {
+				error = EPIPE;
+				break;
+			}
+
+			pipeunlock(wpipe);
+			error = cv_wait_sig(&wpipe->pipe_wcv, lock);
+			(void)pipelock(wpipe, false);
+			if (error != 0)
+				break;
+			wakeup_state = wpipe->pipe_state;
 		}
+	}
 
-		/*
-		 * Awaken the other side (including select/poll/kqueue) then
-		 * sleep ASAP to minimise contention.
-		 */
-		pipeselwakeup(pipe, FREAD, POLL_IN);
-		if (pipeunbusy(pipe))
-			cv_signal(&pipe->pipe_busy);
-		cv_broadcast(&pipe->pipe_read);
-		if ((error = cv_wait_sig(&pipe->pipe_write, lock)) != 0)
-			break;
+	--wpipe->pipe_busy;
+	if (wpipe->pipe_busy == 0) {
+		wpipe->pipe_state &= ~PIPE_RESTART;
+		cv_broadcast(&wpipe->pipe_draincv);
+	}
+	if (bp->cnt > 0) {
+		cv_broadcast(&wpipe->pipe_rcv);
 	}
 
 	/*
 	 * Don't return EPIPE if I/O was successful
 	 */
-	if (error == EPIPE && uio->uio_resid != orig_resid)
+	if (error == EPIPE && bp->cnt == 0 && uio->uio_resid == 0)
 		error = 0;
 
-	/*
-	 * Update timestamp and drop the long term lock (if held).
-	 */
 	if (error == 0)
-		getnanotime(&pipe->pipe_mtime);
-	unbusy = pipeunbusy(pipe);
-
-	/*
-	 * Arrange for next read(2) to do a signal.
-	 */
-	pipe->pipe_state |= PIPE_SIGNALR;
+		getnanotime(&wpipe->pipe_mtime);
 
 	/*
 	 * We have something to offer, wake up select/poll.
 	 */
-	if ((cnt = bp->cnt) > 0)
-		pipeselwakeup(pipe, FREAD, POLL_IN);
+	if (bp->cnt)
+		pipeselwakeup(wpipe, wpipe, POLL_IN);
 
 	/*
-	 * Release the mutex then wake other side, to minimise contention.
+	 * Arrange for next read(2) to do a signal.
 	 */
-	mutex_exit(lock);
-	if (unbusy)
-		cv_signal(&pipe->pipe_busy);
-	if (cnt > 0)
-		cv_broadcast(&pipe->pipe_read);
+	wpipe->pipe_state |= PIPE_SIGNALR;
 
-	return error;
+	pipeunlock(wpipe);
+	mutex_exit(lock);
+	return (error);
 }
 
 /*
@@ -626,130 +755,131 @@ pipe_ioctl(file_t *fp, u_long cmd, void 
 {
 	struct pipe *pipe = fp->f_pipe;
 	kmutex_t *lock = pipe->pipe_lock;
-	int flag;
 
 	switch (cmd) {
+
 	case FIONBIO:
-		return 0;
+		return (0);
 
 	case FIOASYNC:
-		flag = (fp->f_flag & FREAD) != 0 ? PIPE_RDASYNC : PIPE_WRASYNC;
 		mutex_enter(lock);
-		if (*(int *)data)
-			pipe->pipe_state |= flag;
-		else
-			pipe->pipe_state &= ~flag;
+		if (*(int *)data) {
+			pipe->pipe_state |= PIPE_ASYNC;
+		} else {
+			pipe->pipe_state &= ~PIPE_ASYNC;
+		}
 		mutex_exit(lock);
-		return 0;
+		return (0);
 
 	case FIONREAD:
-		if ((fp->f_flag & FREAD) != 0)
-			*(int *)data =
-			    atomic_load_relaxed(&pipe->pipe_buffer.cnt);
-		else
-			*(int *)data = 0;
-		return 0;
+		mutex_enter(lock);
+		*(int *)data = pipe->pipe_buffer.cnt;
+		mutex_exit(lock);
+		return (0);
 
 	case FIONWRITE:
-		if ((fp->f_flag & FWRITE) != 0)
-			*(int *)data =
-			    atomic_load_relaxed(&pipe->pipe_buffer.cnt);
-		else
+		/* Look at other side */
+		mutex_enter(lock);
+		pipe = pipe->pipe_peer;
+		if (pipe == NULL)
 			*(int *)data = 0;
+		else
+			*(int *)data = pipe->pipe_buffer.cnt;
+		mutex_exit(lock);
 		return (0);
 
 	case FIONSPACE:
-		if ((fp->f_flag & FWRITE) != 0) {
-			mutex_enter(lock);
+		/* Look at other side */
+		mutex_enter(lock);
+		pipe = pipe->pipe_peer;
+		if (pipe == NULL)
+			*(int *)data = 0;
+		else
 			*(int *)data = pipe->pipe_buffer.size -
 			    pipe->pipe_buffer.cnt;
-			mutex_exit(lock);
-		} else
-			*(int *)data = 0;
+		mutex_exit(lock);
 		return (0);
 
 	case TIOCSPGRP:
 	case FIOSETOWN:
-		return fsetown((fp->f_flag & FREAD) != 0 ?
-		    &pipe->pipe_rdpgid : &pipe->pipe_wrpgid, cmd, data);
+		return fsetown(&pipe->pipe_pgid, cmd, data);
 
 	case TIOCGPGRP:
 	case FIOGETOWN:
-		return fgetown((fp->f_flag & FREAD) != 0 ?
-		    pipe->pipe_rdpgid : pipe->pipe_wrpgid, cmd, data);
+		return fgetown(pipe->pipe_pgid, cmd, data);
 
-	default:
-		return EPASSTHROUGH;
 	}
+	return (EPASSTHROUGH);
 }
 
 int
 pipe_poll(file_t *fp, int events)
 {
-	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
+	struct pipe *rpipe = fp->f_pipe;
+	struct pipe *wpipe;
+	int eof = 0;
 	int revents = 0;
 
-	/* Unlocked fast path for make(1). */
-	if ((fp->f_flag & FREAD) != 0 &&
-	    atomic_load_relaxed(&pipe->pipe_buffer.cnt) != 0 &&
-	    (atomic_load_relaxed(&pipe->pipe_state) & PIPE_EOF) == 0 &&
-	    (events & (POLLIN | POLLRDNORM)) != 0 &&
-	    (events & (POLLOUT | POLLWRNORM)) == 0)
-		return events & (POLLIN | POLLRDNORM);
+	mutex_enter(rpipe->pipe_lock);
+	wpipe = rpipe->pipe_peer;
 
-	mutex_enter(lock);
+	if (events & (POLLIN | POLLRDNORM))
+		if ((rpipe->pipe_buffer.cnt > 0) ||
+		    (rpipe->pipe_state & PIPE_EOF))
+			revents |= events & (POLLIN | POLLRDNORM);
 
-	if ((fp->f_flag & FREAD) != 0) {
-		if ((events & (POLLIN | POLLRDNORM)) != 0) {
-			if (pipe->pipe_buffer.cnt > 0 ||
-			    (pipe->pipe_state & PIPE_EOF) != 0)
-				revents |= events & (POLLIN | POLLRDNORM);
-			selrecord(curlwp, &pipe->pipe_rdsel);
-		}
-	} else if ((events & (POLLOUT | POLLWRNORM)) != 0) {
-		KASSERT((fp->f_flag & FWRITE) != 0);
-		size_t space = pipe->pipe_buffer.size - pipe->pipe_buffer.cnt;
-		if ((pipe->pipe_state & PIPE_EOF) != 0)
-			revents |= events & (POLLOUT | POLLWRNORM);
-		if ((pipe->pipe_state & PIPE_EOF) || space >= PIPE_BUF)
-			revents |= events & (POLLOUT | POLLWRNORM);
-		selrecord(curlwp, &pipe->pipe_wrsel);
+	eof |= (rpipe->pipe_state & PIPE_EOF);
+
+	if (wpipe == NULL)
+		revents |= events & (POLLOUT | POLLWRNORM);
+	else {
+		if (events & (POLLOUT | POLLWRNORM))
+			if ((wpipe->pipe_state & PIPE_EOF) || (
+			     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
+				revents |= events & (POLLOUT | POLLWRNORM);
+
+		eof |= (wpipe->pipe_state & PIPE_EOF);
 	}
 
-	if ((pipe->pipe_state & PIPE_EOF) != 0)
+	if (wpipe == NULL || eof)
 		revents |= POLLHUP;
 
-	mutex_exit(lock);
+	if (revents == 0) {
+		if (events & (POLLIN | POLLRDNORM))
+			selrecord(curlwp, &rpipe->pipe_sel);
+
+		if (events & (POLLOUT | POLLWRNORM))
+			selrecord(curlwp, &wpipe->pipe_sel);
+	}
+	mutex_exit(rpipe->pipe_lock);
 
-	return revents;
+	return (revents);
 }
 
 static int
 pipe_stat(file_t *fp, struct stat *ub)
 {
 	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
 
+	mutex_enter(pipe->pipe_lock);
 	memset(ub, 0, sizeof(*ub));
-
-	mutex_enter(lock);
 	ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
 	ub->st_blksize = pipe->pipe_buffer.size;
+	if (ub->st_blksize == 0 && pipe->pipe_peer)
+		ub->st_blksize = pipe->pipe_peer->pipe_buffer.size;
 	ub->st_size = pipe->pipe_buffer.cnt;
 	ub->st_blocks = (ub->st_size) ? 1 : 0;
 	ub->st_atimespec = pipe->pipe_atime;
 	ub->st_mtimespec = pipe->pipe_mtime;
-	ub->st_ctimespec = pipe->pipe_btime;
-	ub->st_birthtimespec = pipe->pipe_btime;
+	ub->st_ctimespec = ub->st_birthtimespec = pipe->pipe_btime;
 	ub->st_uid = kauth_cred_geteuid(fp->f_cred);
 	ub->st_gid = kauth_cred_getegid(fp->f_cred);
-	mutex_exit(lock);
 
 	/*
 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
 	 * XXX (st_dev, st_ino) should be unique.
 	 */
+	mutex_exit(pipe->pipe_lock);
 	return 0;
 }
 
@@ -757,90 +887,29 @@ static int
 pipe_close(file_t *fp)
 {
 	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
-	u_int state;
-
-	KASSERT(cv_is_valid(&pipe->pipe_read));
-	KASSERT(cv_is_valid(&pipe->pipe_write));
-	KASSERT(cv_is_valid(&pipe->pipe_busy));
 
 	fp->f_pipe = NULL;
-
-	/*
-	 * If the other side is blocked, wake it up.
-	 *
-	 * Any knote objects still left in the list are the one attached by
-	 * peer.  Since no one will traverse this list, we just clear it.
-	 *
-	 * XXX Exposes select/kqueue internals.
-	 */
-	mutex_enter(lock);
-	pipebusy(pipe);
-	state = pipe->pipe_state | PIPE_EOF;
-	if ((fp->f_flag & FREAD) != 0) {
-		KASSERT((state & PIPE_RDOPEN) != 0);
-		SLIST_INIT(&pipe->pipe_rdsel.sel_klist);
-		pipe->pipe_rdpgid = 0;
-		state &= ~(PIPE_RDASYNC | PIPE_RDOPEN);
-		pipeselwakeup(pipe, FWRITE, POLL_HUP);
-		cv_broadcast(&pipe->pipe_write);
-	} else {
-		KASSERT((fp->f_flag & FWRITE) != 0);
-		KASSERT((state & PIPE_WROPEN) != 0);
-		SLIST_INIT(&pipe->pipe_wrsel.sel_klist);
-		pipe->pipe_wrpgid = 0;
-		state &= ~(PIPE_WRASYNC | PIPE_WROPEN);
-		pipeselwakeup(pipe, FREAD, POLL_HUP);
-		cv_broadcast(&pipe->pipe_read);
-	}
-	pipe->pipe_state = state;
-	pipeunbusy(pipe);
-	cv_signal(&pipe->pipe_busy);
-	mutex_exit(lock);
-
-	/*
-	 * NB: now that the mutex is released, we cannot touch "pipe" any
-	 * more unless we are the last guy out, since nothing else is
-	 * keeping the data structure around.  This also means we have to
-	 * wake the other side with the mutex held above.
-	 */
-	if ((state & (PIPE_RDOPEN | PIPE_WROPEN)) != 0)
-		return 0;
-
-	/* Both sides are closed, free resources. */
-	pipe->pipe_state = PIPE_SIGNALR | PIPE_RDOPEN | PIPE_WROPEN;
-	pipe->pipe_buffer.in = 0;
-	pipe->pipe_buffer.out = 0;
-	pipe->pipe_buffer.cnt = 0;
-	if (pipe->pipe_buffer.buffer != (void *)pipe->pipe_kmem) {
-		uvm_km_free(kernel_map, (vaddr_t)pipe->pipe_buffer.buffer,
-		    pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
-		atomic_add_int(&amountpipekva, -pipe->pipe_buffer.size);
-		atomic_dec_uint(&nbigpipe);
-		pipe->pipe_buffer.buffer = (void *)pipe->pipe_kmem;
-		pipe->pipe_buffer.size = PIPE_SIZE;
-	}
-	pool_cache_put(pipe_cache, pipe);
-
-	return 0;
+	pipeclose(pipe);
+	return (0);
 }
 
 static void
 pipe_restart(file_t *fp)
 {
 	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
 
 	/*
 	 * Unblock blocked reads/writes in order to allow close() to complete.
 	 * System calls return ERESTART so that the fd is revalidated.
 	 * (Partial writes return the transfer length.)
 	 */
-	mutex_enter(lock);
-	cv_fdrestart(&pipe->pipe_read);
-	cv_fdrestart(&pipe->pipe_write);
-	cv_fdrestart(&pipe->pipe_busy);
-	mutex_exit(lock);
+	mutex_enter(pipe->pipe_lock);
+	pipe->pipe_state |= PIPE_RESTART;
+	/* Wakeup both cvs, maybe we only need one, but maybe there are some
+	 * other paths where wakeup is needed, and it saves deciding which! */
+	cv_broadcast(&pipe->pipe_rcv);
+	cv_broadcast(&pipe->pipe_wcv);
+	mutex_exit(pipe->pipe_lock);
 }
 
 static int
@@ -864,41 +933,148 @@ pipe_posix_fadvise(struct file *fp, off_
 }
 
 static void
+pipe_free_kmem(struct pipe *pipe)
+{
+
+	if (pipe->pipe_buffer.buffer != NULL) {
+		if (pipe->pipe_buffer.size > PIPE_SIZE) {
+			atomic_dec_uint(&nbigpipe);
+		}
+		if (pipe->pipe_buffer.buffer != (void *)pipe->pipe_kmem) {
+			uvm_km_free(kernel_map,
+			    (vaddr_t)pipe->pipe_buffer.buffer,
+			    pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
+			atomic_add_int(&amountpipekva,
+			    -pipe->pipe_buffer.size);
+		}
+		pipe->pipe_buffer.buffer = NULL;
+	}
+}
+
+/*
+ * Shutdown the pipe.
+ */
+static void
+pipeclose(struct pipe *pipe)
+{
+	kmutex_t *lock;
+	struct pipe *ppipe;
+
+	if (pipe == NULL)
+		return;
+
+	KASSERT(cv_is_valid(&pipe->pipe_rcv));
+	KASSERT(cv_is_valid(&pipe->pipe_wcv));
+	KASSERT(cv_is_valid(&pipe->pipe_draincv));
+	KASSERT(cv_is_valid(&pipe->pipe_lkcv));
+
+	lock = pipe->pipe_lock;
+	if (lock == NULL)
+		/* Must have failed during create */
+		goto free_resources;
+
+	mutex_enter(lock);
+	pipeselwakeup(pipe, pipe, POLL_HUP);
+
+	/*
+	 * If the other side is blocked, wake it up saying that
+	 * we want to close it down.
+	 */
+	pipe->pipe_state |= PIPE_EOF;
+	if (pipe->pipe_busy) {
+		while (pipe->pipe_busy) {
+			cv_broadcast(&pipe->pipe_wcv);
+			cv_wait_sig(&pipe->pipe_draincv, lock);
+		}
+	}
+
+	/*
+	 * Disconnect from peer.
+	 */
+	if ((ppipe = pipe->pipe_peer) != NULL) {
+		pipeselwakeup(ppipe, ppipe, POLL_HUP);
+		ppipe->pipe_state |= PIPE_EOF;
+		cv_broadcast(&ppipe->pipe_rcv);
+		ppipe->pipe_peer = NULL;
+	}
+
+	/*
+	 * Any knote objects still left in the list are
+	 * the one attached by peer.  Since no one will
+	 * traverse this list, we just clear it.
+	 *
+	 * XXX Exposes select/kqueue internals.
+	 */
+	SLIST_INIT(&pipe->pipe_sel.sel_klist);
+
+	KASSERT((pipe->pipe_state & PIPE_LOCKFL) == 0);
+	mutex_exit(lock);
+	mutex_obj_free(lock);
+
+	/*
+	 * Free resources.
+	 */
+    free_resources:
+	pipe->pipe_pgid = 0;
+	pipe->pipe_state = PIPE_SIGNALR;
+	pipe->pipe_peer = NULL;
+	pipe->pipe_lock = NULL;
+	pipe_free_kmem(pipe);
+	if (pipe->pipe_kmem != 0) {
+		pool_cache_put(pipe_rd_cache, pipe);
+	} else {
+		pool_cache_put(pipe_wr_cache, pipe);
+	}
+}
+
+static void
 filt_pipedetach(struct knote *kn)
 {
-	struct file *fp = kn->kn_obj;
-	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
+	struct pipe *pipe;
+	kmutex_t *lock;
+
+	pipe = ((file_t *)kn->kn_obj)->f_pipe;
+	lock = pipe->pipe_lock;
 
 	mutex_enter(lock);
 
+	switch(kn->kn_filter) {
+	case EVFILT_WRITE:
+		/* Need the peer structure, not our own. */
+		pipe = pipe->pipe_peer;
+
+		/* If reader end already closed, just return. */
+		if (pipe == NULL) {
+			mutex_exit(lock);
+			return;
+		}
+
+		break;
+	default:
+		/* Nothing to do. */
+		break;
+	}
+
 	KASSERT(kn->kn_hook == pipe);
-	if ((fp->f_flag & FREAD) != 0) {
-		if ((pipe->pipe_state & PIPE_RDOPEN) != 0)
-			selremove_knote(&pipe->pipe_rdsel, kn);
-	} else if ((pipe->pipe_state & PIPE_WROPEN) != 0)
-		selremove_knote(&pipe->pipe_wrsel, kn);
+	selremove_knote(&pipe->pipe_sel, kn);
 	mutex_exit(lock);
 }
 
 static int
 filt_piperead(struct knote *kn, long hint)
 {
-	struct file *fp = kn->kn_obj;
-	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
+	struct pipe *rpipe = ((file_t *)kn->kn_obj)->f_pipe;
+	struct pipe *wpipe;
 	int rv;
 
 	if ((hint & NOTE_SUBMIT) == 0) {
-		mutex_enter(lock);
+		mutex_enter(rpipe->pipe_lock);
 	}
+	wpipe = rpipe->pipe_peer;
+	kn->kn_data = rpipe->pipe_buffer.cnt;
 
-	if ((fp->f_flag & FREAD) != 0)
-		kn->kn_data = pipe->pipe_buffer.cnt;
-	else
-		kn->kn_data = 0;
-
-	if ((pipe->pipe_state & PIPE_EOF) != 0) {
+	if ((rpipe->pipe_state & PIPE_EOF) ||
+	    (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
 		knote_set_eof(kn, 0);
 		rv = 1;
 	} else {
@@ -906,7 +1082,7 @@ filt_piperead(struct knote *kn, long hin
 	}
 
 	if ((hint & NOTE_SUBMIT) == 0) {
-		mutex_exit(lock);
+		mutex_exit(rpipe->pipe_lock);
 	}
 	return rv;
 }
@@ -914,29 +1090,26 @@ filt_piperead(struct knote *kn, long hin
 static int
 filt_pipewrite(struct knote *kn, long hint)
 {
-	struct file *fp = kn->kn_obj;
-	struct pipe *pipe = fp->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
+	struct pipe *rpipe = ((file_t *)kn->kn_obj)->f_pipe;
+	struct pipe *wpipe;
 	int rv;
 
 	if ((hint & NOTE_SUBMIT) == 0) {
-		mutex_enter(lock);
+		mutex_enter(rpipe->pipe_lock);
 	}
+	wpipe = rpipe->pipe_peer;
 
-	if ((pipe->pipe_state & PIPE_EOF)) {
+	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
 		kn->kn_data = 0;
 		knote_set_eof(kn, 0);
 		rv = 1;
-	} else if ((fp->f_flag & FWRITE) != 0) {
-		kn->kn_data = pipe->pipe_buffer.size - pipe->pipe_buffer.cnt;
-		rv = kn->kn_data >= PIPE_BUF;
 	} else {
-		kn->kn_data = 0;
-		rv = 0;
+		kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
+		rv = kn->kn_data >= PIPE_BUF;
 	}
 
 	if ((hint & NOTE_SUBMIT) == 0) {
-		mutex_exit(lock);
+		mutex_exit(rpipe->pipe_lock);
 	}
 	return rv;
 }
@@ -958,37 +1131,36 @@ static const struct filterops pipe_wfilt
 static int
 pipe_kqfilter(file_t *fp, struct knote *kn)
 {
-	struct pipe *pipe = ((file_t *)kn->kn_obj)->f_pipe;
-	kmutex_t *lock = pipe->pipe_lock;
+	struct pipe *pipe;
+	kmutex_t *lock;
+
+	pipe = ((file_t *)kn->kn_obj)->f_pipe;
+	lock = pipe->pipe_lock;
+
+	mutex_enter(lock);
 
 	switch (kn->kn_filter) {
 	case EVFILT_READ:
-		if ((fp->f_flag & FREAD) == 0)
-			return EINVAL;
-		mutex_enter(lock);
 		kn->kn_fop = &pipe_rfiltops;
-		kn->kn_hook = pipe;
-		selrecord_knote(&pipe->pipe_rdsel, kn);
-		mutex_exit(lock);
 		break;
 	case EVFILT_WRITE:
-		if ((fp->f_flag & FWRITE) == 0)
-			return EINVAL;
-		mutex_enter(lock);
 		kn->kn_fop = &pipe_wfiltops;
-		if ((pipe->pipe_state & PIPE_EOF) != 0) {
+		pipe = pipe->pipe_peer;
+		if (pipe == NULL) {
 			/* Other end of pipe has been closed. */
 			mutex_exit(lock);
-			return EBADF;
+			return (EBADF);
 		}
-		kn->kn_hook = pipe;
-		selrecord_knote(&pipe->pipe_wrsel, kn);
-		mutex_exit(lock);
 		break;
 	default:
-		return EINVAL;
+		mutex_exit(lock);
+		return (EINVAL);
 	}
 
+	kn->kn_hook = pipe;
+	selrecord_knote(&pipe->pipe_sel, kn);
+	mutex_exit(lock);
+
 	return (0);
 }
 

Index: src/sys/kern/uipc_socket.c
diff -u src/sys/kern/uipc_socket.c:1.306 src/sys/kern/uipc_socket.c:1.307
--- src/sys/kern/uipc_socket.c:1.306	Fri Oct 13 18:50:39 2023
+++ src/sys/kern/uipc_socket.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_socket.c,v 1.306 2023/10/13 18:50:39 ad Exp $	*/
+/*	$NetBSD: uipc_socket.c,v 1.307 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 2002, 2007, 2008, 2009, 2023 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.306 2023/10/13 18:50:39 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.307 2023/11/02 10:31:55 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -895,6 +895,7 @@ sosend(struct socket *so, struct sockadd
 	struct mbuf **mp, *m;
 	long space, len, resid, clen, mlen;
 	int error, s, dontroute, atomic;
+	short wakeup_state = 0;
 
 	clen = 0;
 
@@ -967,11 +968,17 @@ sosend(struct socket *so, struct sockadd
 				goto release;
 			}
 			sbunlock(&so->so_snd);
+			if (wakeup_state & SS_RESTARTSYS) {
+				error = ERESTART;
+				goto out;
+			}
 			error = sbwait(&so->so_snd);
 			if (error)
 				goto out;
+			wakeup_state = so->so_state;
 			goto restart;
 		}
+		wakeup_state = 0;
 		mp = &top;
 		space -= clen;
 		do {
@@ -1153,6 +1160,7 @@ soreceive(struct socket *so, struct mbuf
 	struct mbuf *nextrecord;
 	int mbuf_removed = 0;
 	const struct domain *dom;
+	short wakeup_state = 0;
 
 	pr = so->so_proto;
 	atomic = pr->pr_flags & PR_ATOMIC;
@@ -1263,12 +1271,16 @@ restart:
 		SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
 		SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
 		sbunlock(&so->so_rcv);
-		error = sbwait(&so->so_rcv);
+		if (wakeup_state & SS_RESTARTSYS)
+			error = ERESTART;
+		else
+			error = sbwait(&so->so_rcv);
 		if (error != 0) {
 			sounlock(so);
 			splx(s);
 			return error;
 		}
+		wakeup_state = so->so_state;
 		goto restart;
 	}
 
@@ -1445,6 +1457,7 @@ dontblock:
 #endif
 
 		so->so_state &= ~SS_RCVATMARK;
+		wakeup_state = 0;
 		len = uio->uio_resid;
 		if (so->so_oobmark && len > so->so_oobmark - offset)
 			len = so->so_oobmark - offset;
@@ -1587,7 +1600,10 @@ dontblock:
 				(*pr->pr_usrreqs->pr_rcvd)(so, flags, l);
 			SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
 			SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
-			error = sbwait(&so->so_rcv);
+			if (wakeup_state & SS_RESTARTSYS)
+				error = ERESTART;
+			else
+				error = sbwait(&so->so_rcv);
 			if (error != 0) {
 				sbunlock(&so->so_rcv);
 				sounlock(so);
@@ -1596,6 +1612,7 @@ dontblock:
 			}
 			if ((m = so->so_rcv.sb_mb) != NULL)
 				nextrecord = m->m_nextpkt;
+			wakeup_state = so->so_state;
 		}
 	}
 
@@ -1663,7 +1680,6 @@ soshutdown(struct socket *so, int how)
 void
 sorestart(struct socket *so)
 {
-
 	/*
 	 * An application has called close() on an fd on which another
 	 * of its threads has called a socket system call.
@@ -1673,9 +1689,10 @@ sorestart(struct socket *so)
 	 * Any other fd will block again on the 2nd syscall.
 	 */
 	solock(so);
-	cv_fdrestart(&so->so_cv);
-	cv_fdrestart(&so->so_snd.sb_cv);
-	cv_fdrestart(&so->so_rcv.sb_cv);
+	so->so_state |= SS_RESTARTSYS;
+	cv_broadcast(&so->so_cv);
+	cv_broadcast(&so->so_snd.sb_cv);
+	cv_broadcast(&so->so_rcv.sb_cv);
 	sounlock(so);
 }
 

Index: src/sys/kern/uipc_syscalls.c
diff -u src/sys/kern/uipc_syscalls.c:1.209 src/sys/kern/uipc_syscalls.c:1.210
--- src/sys/kern/uipc_syscalls.c:1.209	Fri Oct 13 18:50:39 2023
+++ src/sys/kern/uipc_syscalls.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.209 2023/10/13 18:50:39 ad Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.210 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2023 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.209 2023/10/13 18:50:39 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.210 2023/11/02 10:31:55 martin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pipe.h"
@@ -175,6 +175,7 @@ do_sys_accept(struct lwp *l, int sock, s
 	file_t		*fp, *fp2;
 	int		error, fd;
 	struct socket	*so, *so2;
+	short		wakeup_state = 0;
 
 	if ((fp = fd_getfile(sock)) == NULL)
 		return EBADF;
@@ -210,10 +211,15 @@ do_sys_accept(struct lwp *l, int sock, s
 			so->so_error = ECONNABORTED;
 			break;
 		}
+		if (wakeup_state & SS_RESTARTSYS) {
+			error = ERESTART;
+			goto bad;
+		}
 		error = sowait(so, true, 0);
 		if (error) {
 			goto bad;
 		}
+		wakeup_state = so->so_state;
 	}
 	if (so->so_error) {
 		error = so->so_error;

Index: src/sys/rump/librump/rumpkern/locks.c
diff -u src/sys/rump/librump/rumpkern/locks.c:1.87 src/sys/rump/librump/rumpkern/locks.c:1.88
--- src/sys/rump/librump/rumpkern/locks.c:1.87	Fri Oct 13 18:48:56 2023
+++ src/sys/rump/librump/rumpkern/locks.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: locks.c,v 1.87 2023/10/13 18:48:56 ad Exp $	*/
+/*	$NetBSD: locks.c,v 1.88 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.87 2023/10/13 18:48:56 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.88 2023/11/02 10:31:55 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
@@ -486,13 +486,6 @@ cv_broadcast(kcondvar_t *cv)
 	rumpuser_cv_broadcast(RUMPCV(cv));
 }
 
-void
-cv_fdrestart(kcondvar_t *cv)
-{
-
-	rumpuser_cv_broadcast(RUMPCV(cv));
-}
-
 bool
 cv_has_waiters(kcondvar_t *cv)
 {

Index: src/sys/rump/librump/rumpkern/locks_up.c
diff -u src/sys/rump/librump/rumpkern/locks_up.c:1.13 src/sys/rump/librump/rumpkern/locks_up.c:1.14
--- src/sys/rump/librump/rumpkern/locks_up.c:1.13	Fri Oct 13 18:48:56 2023
+++ src/sys/rump/librump/rumpkern/locks_up.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: locks_up.c,v 1.13 2023/10/13 18:48:56 ad Exp $	*/
+/*	$NetBSD: locks_up.c,v 1.14 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks_up.c,v 1.13 2023/10/13 18:48:56 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks_up.c,v 1.14 2023/11/02 10:31:55 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -421,14 +421,6 @@ cv_broadcast(kcondvar_t *cv)
 	rumpuser_cv_broadcast(RUMPCV(cv));
 }
 
-void
-cv_fdrestart(kcondvar_t *cv)
-{
-
-	/* CPU == interlock */
-	rumpuser_cv_broadcast(RUMPCV(cv));
-}
-
 bool
 cv_has_waiters(kcondvar_t *cv)
 {

Index: src/sys/sys/condvar.h
diff -u src/sys/sys/condvar.h:1.18 src/sys/sys/condvar.h:1.19
--- src/sys/sys/condvar.h:1.18	Fri Oct 13 18:48:56 2023
+++ src/sys/sys/condvar.h	Thu Nov  2 10:31:55 2023
@@ -1,7 +1,7 @@
-/*	$NetBSD: condvar.h,v 1.18 2023/10/13 18:48:56 ad Exp $	*/
+/*	$NetBSD: condvar.h,v 1.19 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
- * Copyright (c) 2006, 2007, 2008, 2020, 2023 The NetBSD Foundation, Inc.
+ * Copyright (c) 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -56,7 +56,6 @@ int	cv_timedwaitbt_sig(kcondvar_t *, str
 
 void	cv_signal(kcondvar_t *);
 void	cv_broadcast(kcondvar_t *);
-void	cv_fdrestart(kcondvar_t *);
 
 bool	cv_has_waiters(kcondvar_t *);
 bool	cv_is_valid(kcondvar_t *);

Index: src/sys/sys/lwp.h
diff -u src/sys/sys/lwp.h:1.230 src/sys/sys/lwp.h:1.231
--- src/sys/sys/lwp.h:1.230	Sun Oct 15 10:29:44 2023
+++ src/sys/sys/lwp.h	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lwp.h,v 1.230 2023/10/15 10:29:44 riastradh Exp $	*/
+/*	$NetBSD: lwp.h,v 1.231 2023/11/02 10:31:55 martin Exp $	*/
 
 /*
  * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2010, 2019, 2020, 2023
@@ -267,7 +267,6 @@ extern int		maxlwp __read_mostly;	/* max
 #define	LW_CACHECRED	0x04000000 /* Cache new process credential */
 #define	LW_WREBOOT	0x08000000 /* System is rebooting, please suspend */
 #define	LW_UNPARKED	0x10000000 /* Unpark op pending */
-#define	LW_RESTART	0x20000000 /* Return ERESTART after waking */
 #define	LW_RUMP_CLEAR	0x40000000 /* Clear curlwp in RUMP scheduler */
 #define	LW_RUMP_QEXIT	0x80000000 /* LWP should exit ASAP */
 

Index: src/sys/sys/pipe.h
diff -u src/sys/sys/pipe.h:1.41 src/sys/sys/pipe.h:1.42
--- src/sys/sys/pipe.h:1.41	Tue Oct 17 10:28:06 2023
+++ src/sys/sys/pipe.h	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pipe.h,v 1.41 2023/10/17 10:28:06 riastradh Exp $	*/
+/* $NetBSD: pipe.h,v 1.42 2023/11/02 10:31:55 martin Exp $ */
 
 /*
  * Copyright (c) 1996 John S. Dyson
@@ -75,34 +75,34 @@ struct pipebuf {
 /*
  * Bits in pipe_state.
  */
-#define	PIPE_RDASYNC	0x001	/* Async I/O on reader side */
-#define	PIPE_WRASYNC	0x002	/* Async I/O on writer side */
-#define	PIPE_RDOPEN	0x010	/* Reader side open */
-#define	PIPE_WROPEN	0x020	/* Writer side open */
-#define	PIPE_EOF	0x100	/* Pipe is in EOF condition */
-#define	PIPE_SIGNALR	0x200	/* Do selwakeup() on read(2) */
-#define	PIPE_RESIZED	0x400	/* Attempted to resize */
+#define	PIPE_ASYNC	0x001	/* Async I/O */
+#define	PIPE_EOF	0x010	/* Pipe is in EOF condition */
+#define	PIPE_SIGNALR	0x020	/* Do selwakeup() on read(2) */
+#define	PIPE_LOCKFL	0x100	/* Process has exclusive access to
+				   pointers/data. */
+/*	unused  	0x200	*/
+#define	PIPE_RESTART	0x400	/* Return ERESTART to blocked syscalls */
 
 /*
  * Per-pipe data structure.
  * Two of these are linked together to produce bi-directional pipes.
  */
 struct pipe {
-	kmutex_t	*pipe_lock;		/* pipe mutex */
-	struct lwp	*pipe_owner;		/* who holds the pipe busy */
-	u_int		pipe_state;		/* pipe status info */
-	struct pipebuf	pipe_buffer;		/* data storage */
-	kcondvar_t	pipe_read;		/* cv for readers */
-	kcondvar_t	pipe_write;		/* cv for writers */
-	kcondvar_t	pipe_busy;		/* cv for locking */
-	struct selinfo	pipe_wrsel;		/* for compat with select */
-	struct selinfo	pipe_rdsel;		/* for compat with select */
-	struct timespec	pipe_atime;		/* time of last access */
-	struct timespec	pipe_mtime;		/* time of last modify */
-	struct timespec	pipe_btime;		/* time of creation */
-	pid_t		pipe_wrpgid;		/* process group for sigio */
-	pid_t		pipe_rdpgid;		/* process group for sigio */
-	vaddr_t		pipe_kmem;		/* preallocated PIPE_SIZE buffer */
+	kmutex_t *pipe_lock;		/* pipe mutex */
+	kcondvar_t pipe_rcv;		/* cv for readers */
+	kcondvar_t pipe_wcv;		/* cv for writers */
+	kcondvar_t pipe_draincv;	/* cv for close */
+	kcondvar_t pipe_lkcv;		/* locking */
+	struct	pipebuf pipe_buffer;	/* data storage */
+	struct	selinfo pipe_sel;	/* for compat with select */
+	struct	timespec pipe_atime;	/* time of last access */
+	struct	timespec pipe_mtime;	/* time of last modify */
+	struct	timespec pipe_btime;	/* time of creation */
+	struct	pipe *pipe_peer;	/* link with other direction */
+	pid_t	pipe_pgid;		/* process group for sigio */
+	u_int	pipe_state;		/* pipe status info */
+	int	pipe_busy;		/* busy flag, to handle rundown */
+	vaddr_t	pipe_kmem;		/* preallocated PIPE_SIZE buffer */
 };
 
 /*

Index: src/sys/sys/socketvar.h
diff -u src/sys/sys/socketvar.h:1.166 src/sys/sys/socketvar.h:1.167
--- src/sys/sys/socketvar.h:1.166	Fri Oct 13 18:50:39 2023
+++ src/sys/sys/socketvar.h	Thu Nov  2 10:31:55 2023
@@ -1,7 +1,7 @@
-/*	$NetBSD: socketvar.h,v 1.166 2023/10/13 18:50:39 ad Exp $	*/
+/*	$NetBSD: socketvar.h,v 1.167 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
- * Copyright (c) 2008, 2009, 2023 The NetBSD Foundation, Inc.
+ * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -197,6 +197,7 @@ struct socket {
 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
 #define	SS_RCVATMARK		0x040	/* at mark on input */
 #define	SS_ISABORTING		0x080	/* aborting fd references - close() */
+#define	SS_RESTARTSYS		0x100	/* restart blocked system calls */
 #define	SS_POLLRDBAND		0x200	/* poll should return POLLRDBAND */
 #define	SS_MORETOCOME		0x400	/*
 					 * hint from sosend to lower layer;

Index: src/usr.bin/fstat/fstat.c
diff -u src/usr.bin/fstat/fstat.c:1.119 src/usr.bin/fstat/fstat.c:1.120
--- src/usr.bin/fstat/fstat.c:1.119	Fri Oct 13 19:07:09 2023
+++ src/usr.bin/fstat/fstat.c	Thu Nov  2 10:31:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstat.c,v 1.119 2023/10/13 19:07:09 ad Exp $	*/
+/*	$NetBSD: fstat.c,v 1.120 2023/11/02 10:31:55 martin Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
 #else
-__RCSID("$NetBSD: fstat.c,v 1.119 2023/10/13 19:07:09 ad Exp $");
+__RCSID("$NetBSD: fstat.c,v 1.120 2023/11/02 10:31:55 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -1271,7 +1271,6 @@ static void
 ptrans(struct file *fp, struct pipe *cpipe, int i)
 {
 	struct pipe cp;
-	int flag;
 
 	PREFIX(i);
 	
@@ -1282,12 +1281,12 @@ ptrans(struct file *fp, struct pipe *cpi
 	}
 
 	/* pipe descriptor is either read or write, never both */
-	flag = (fp->f_flag & FWRITE) ? PIPE_WRASYNC : PIPE_RDASYNC;
-	(void)printf("* pipe %p %s %s%s%s", cpipe,
+	(void)printf("* pipe %p %s %p %s%s%s", cpipe,
 		(fp->f_flag & FWRITE) ? "->" : "<-",
+		cp.pipe_peer,
 		(fp->f_flag & FWRITE) ? "w" : "r",
 		(fp->f_flag & FNONBLOCK) ? "n" : "",
-		(cp.pipe_state & flag) ? "a" : "");
+		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
 	oprint(fp, "\n");
 	return;
 bad:

Reply via email to