From dda0c3b738a8e4f1883e279636b5772fed21904c Mon Sep 17 00:00:00 2001
From: Kanonenvogel <kanonenvogel.87g@gmail.com>
Date: Thu, 16 Apr 2015 04:41:28 +0400
Subject: [PATCH 2/2] Pipe locks

Unnecessary KERNEL_LOCK()'s removed.
Missing KERNEL_LOCKS() added.
---
 kern/sys_pipe.c | 67 +++++++++------------------------------------------------
 1 file changed, 10 insertions(+), 57 deletions(-)

diff --git a/kern/sys_pipe.c b/kern/sys_pipe.c
index 096663d..5259829 100644
--- a/kern/sys_pipe.c
+++ b/kern/sys_pipe.c
@@ -174,14 +174,10 @@ dopipe(struct proc *p, int *ufds, int flags)
 	struct pipe *rpipe, *wpipe = NULL;
 	int fds[2], error;
 
-	KERNEL_LOCK();
-
 	lock = pool_get(&pipe_lock_pool, PR_WAITOK);
 	rpipe = pool_get(&pipe_pool, PR_WAITOK);
 	wpipe = pool_get(&pipe_pool, PR_WAITOK);
 
-	KERNEL_UNLOCK();
-
 	rw_init(lock, "pipe_lock");
 
 	error = pipe_create(rpipe, wpipe, lock);
@@ -304,9 +300,7 @@ pipe_create(struct pipe *cpipe, struct pipe *ppipe, struct rwlock *lock)
 	if (error != 0)
 		return (error);
 
-	KERNEL_LOCK();
 	getnanotime(&cpipe->pipe_ctime);
-	KERNEL_UNLOCK();
 
 	cpipe->pipe_atime = cpipe->pipe_ctime;
 	cpipe->pipe_mtime = cpipe->pipe_ctime;
@@ -333,13 +327,9 @@ pipelock(struct pipe *cpipe)
 
 	while (cpipe->pipe_state & PIPE_LOCK) {
 		cpipe->pipe_state |= PIPE_LWANT;
-
 		pipe_pair_unlock(cpipe);
 
-		KERNEL_LOCK();
 		error = tsleep(cpipe, PRIBIO|PCATCH, "pipelk", 0);
-		KERNEL_UNLOCK();
-
 		pipe_pair_lock(cpipe);
 
 		if (error)
@@ -360,9 +350,7 @@ pipeunlock(struct pipe *cpipe)
 	cpipe->pipe_state &= ~PIPE_LOCK;
 	if (cpipe->pipe_state & PIPE_LWANT) {
 		cpipe->pipe_state &= ~PIPE_LWANT;
-		KERNEL_LOCK();
 		wakeup(cpipe);
-		KERNEL_UNLOCK();
 	}
 }
 
@@ -455,9 +443,7 @@ pipe_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
 			 */
 			if (rpipe->pipe_state & PIPE_WANTW) {
 				rpipe->pipe_state &= ~PIPE_WANTW;
-				KERNEL_LOCK();
 				wakeup(rpipe);
-				KERNEL_UNLOCK();
 			}
 
 			/*
@@ -481,16 +467,10 @@ pipe_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
 				error = EAGAIN;
 			} else {
 				rpipe->pipe_state |= PIPE_WANTR;
-
 				pipe_pair_unlock(rpipe);
-
-				KERNEL_LOCK();
 				error = tsleep(rpipe, PRIBIO|PCATCH,
 				    "piperd", 0);
-				KERNEL_UNLOCK();
-
 				pipe_pair_lock(rpipe);
-
 				if (error == 0) {
 					error = pipelock(rpipe);
 				}
@@ -501,11 +481,8 @@ pipe_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
 	}
 	pipeunlock(rpipe);
 
-	if (error == 0) {
-		KERNEL_LOCK();
+	if (error == 0)
 		getnanotime(&rpipe->pipe_atime);
-		KERNEL_UNLOCK();
-	}
 unlocked_error:
 	--rpipe->pipe_busy;
 
@@ -514,18 +491,14 @@ unlocked_error:
 	 */
 	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
 		rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
-		KERNEL_LOCK();
 		wakeup(rpipe);
-		KERNEL_UNLOCK();
 	} else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
 		/*
 		 * Handle write blocking hysteresis.
 		 */
 		if (rpipe->pipe_state & PIPE_WANTW) {
 			rpipe->pipe_state &= ~PIPE_WANTW;
-			KERNEL_LOCK();
 			wakeup(rpipe);
-			KERNEL_UNLOCK();
 		}
 	}
 
@@ -591,9 +564,7 @@ pipe_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
 		if ((wpipe->pipe_busy == 0) &&
 		    (wpipe->pipe_state & PIPE_WANT)) {
 			wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
-			KERNEL_LOCK();
 			wakeup(wpipe);
-			KERNEL_UNLOCK();
 		}
 
 		goto out_unlock;
@@ -707,9 +678,7 @@ retrywrite:
 			 */
 			if (wpipe->pipe_state & PIPE_WANTR) {
 				wpipe->pipe_state &= ~PIPE_WANTR;
-				KERNEL_LOCK();
 				wakeup(wpipe);
-				KERNEL_UNLOCK();
 			}
 
 			/*
@@ -727,16 +696,10 @@ retrywrite:
 			pipeselwakeup(wpipe);
 
 			wpipe->pipe_state |= PIPE_WANTW;
-
 			pipe_pair_unlock(rpipe);
-
-			KERNEL_LOCK();
 			error = tsleep(wpipe, (PRIBIO + 1)|PCATCH,
 			    "pipewr", 0);
-			KERNEL_UNLOCK();
-
 			pipe_pair_lock(rpipe);
-
 			if (error)
 				break;
 			/*
@@ -754,9 +717,7 @@ retrywrite:
 
 	if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
 		wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
-		KERNEL_LOCK();
 		wakeup(wpipe);
-		KERNEL_UNLOCK();
 	} else if (wpipe->pipe_buffer.cnt > 0) {
 		/*
 		 * If we have put any characters in the buffer, we wake up
@@ -764,9 +725,7 @@ retrywrite:
 		 */
 		if (wpipe->pipe_state & PIPE_WANTR) {
 			wpipe->pipe_state &= ~PIPE_WANTR;
-			KERNEL_LOCK();
 			wakeup(wpipe);
-			KERNEL_UNLOCK();
 		}
 	}
 
@@ -779,11 +738,8 @@ retrywrite:
 		error = 0;
 	}
 
-	if (error == 0) {
-		KERNEL_LOCK();
+	if (error == 0)
 		getnanotime(&wpipe->pipe_mtime);
-		KERNEL_UNLOCK();
-	}
 	/*
 	 * We have something to offer, wake up select/poll.
 	 */
@@ -965,12 +921,10 @@ pipeclose(struct pipe *cpipe)
 	*/
 	cpipe->pipe_state |= PIPE_EOF;
 	while (cpipe->pipe_busy) {
-		KERNEL_LOCK();
 		wakeup(cpipe);
 		cpipe->pipe_state |= PIPE_WANT;
 		pipe_pair_unlock(cpipe);
 		tsleep(cpipe, PRIBIO, "pipecl", 0);
-		KERNEL_UNLOCK();
 		pipe_pair_lock(cpipe);
 	}
 
@@ -980,13 +934,8 @@ pipeclose(struct pipe *cpipe)
 	if ((ppipe = cpipe->pipe_peer) != NULL) {
 		pipeselwakeup(ppipe);
 		ppipe->pipe_state |= PIPE_EOF;
-
-		KERNEL_LOCK();
 		wakeup(ppipe);
-		KERNEL_UNLOCK();
-
 		ppipe->pipe_peer = NULL;
-
 		free_lock = 0;
 	} else {
 		free_lock = 1;
@@ -998,14 +947,11 @@ pipeclose(struct pipe *cpipe)
 	 * free resources
 	 */
 	pipe_free_kmem(cpipe);
-	KERNEL_LOCK();
 
 	if (free_lock) {
 		pool_put(&pipe_lock_pool, cpipe->pipe_lock);
 	}
 	pool_put(&pipe_pool, cpipe);
-
-	KERNEL_UNLOCK();
 }
 
 int
@@ -1021,7 +967,9 @@ pipe_kqfilter(struct file *fp, struct knote *kn)
 	switch (kn->kn_filter) {
 	case EVFILT_READ:
 		kn->kn_fop = &pipe_rfiltops;
+		KERNEL_LOCK();
 		SLIST_INSERT_HEAD(&rpipe->pipe_sel.si_note, kn, kn_selnext);
+		KERNEL_UNLOCK();
 		break;
 	case EVFILT_WRITE:
 		if (wpipe == NULL) {
@@ -1030,7 +978,9 @@ pipe_kqfilter(struct file *fp, struct knote *kn)
 			return (EPIPE);
 		}
 		kn->kn_fop = &pipe_wfiltops;
+		KERNEL_LOCK();
 		SLIST_INSERT_HEAD(&wpipe->pipe_sel.si_note, kn, kn_selnext);
+		KERNEL_UNLOCK();
 		break;
 	default:
 		pipe_pair_unlock(rpipe);
@@ -1054,15 +1004,18 @@ filt_pipedetach(struct knote *kn)
 
 	switch (kn->kn_filter) {
 	case EVFILT_READ:
+		KERNEL_LOCK();
 		SLIST_REMOVE(&rpipe->pipe_sel.si_note, kn, knote, kn_selnext);
+		KERNEL_UNLOCK();
 		break;
 	case EVFILT_WRITE:
 		if (wpipe == NULL) {
 			pipe_pair_unlock(rpipe);
 			return;
 		}
-
+		KERNEL_LOCK();
 		SLIST_REMOVE(&wpipe->pipe_sel.si_note, kn, knote, kn_selnext);
+		KERNEL_UNLOCK();
 		break;
 	}
 
-- 
1.9.3

