Module Name:    src
Committed By:   ad
Date:           Wed Oct  4 22:12:23 UTC 2023

Modified Files:
        src/sys/kern: sys_pipe.c

Log Message:
pipe_read(): try to skip locking the pipe if a non-blocking fd is used, as
is very often the case with BSD make (from FreeBSD/mjg@).


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/sys/kern/sys_pipe.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/sys_pipe.c
diff -u src/sys/kern/sys_pipe.c:1.160 src/sys/kern/sys_pipe.c:1.161
--- src/sys/kern/sys_pipe.c:1.160	Sat Apr 22 13:53:02 2023
+++ src/sys/kern/sys_pipe.c	Wed Oct  4 22:12:23 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_pipe.c,v 1.160 2023/04/22 13:53:02 riastradh Exp $	*/
+/*	$NetBSD: sys_pipe.c,v 1.161 2023/10/04 22:12:23 ad Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.160 2023/04/22 13:53:02 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.161 2023/10/04 22:12:23 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -434,6 +434,23 @@ pipe_read(file_t *fp, off_t *offset, str
 	size_t ocnt;
 	unsigned int wakeup_state = 0;
 
+	/*
+	 * Try to avoid locking the pipe if we have nothing to do.
+	 *
+	 * There are programs which share one pipe amongst multiple processes
+	 * and perform non-blocking reads in parallel, even if the pipe is
+	 * empty.  This in particular is the case with BSD make, which when
+	 * spawned with a high -j number can find itself with over half of the
+	 * calls failing to find anything.
+	 */
+	if ((fp->f_flag & FNONBLOCK) != 0) {
+		if (__predict_false(uio->uio_resid == 0))
+			return (0);
+		if (atomic_load_relaxed(&bp->cnt) == 0 &&
+		    (atomic_load_relaxed(&rpipe->pipe_state) & PIPE_EOF) == 0)
+			return (EAGAIN);
+	}
+
 	mutex_enter(lock);
 	++rpipe->pipe_busy;
 	ocnt = bp->cnt;

Reply via email to