linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Two cleanups to pipe
@ 2023-09-28 16:17 Kemeng Shi
  2023-09-28 16:17 ` [PATCH 1/2] pipe: remove pipe_full check with wrong head in pipe_write Kemeng Shi
  2023-09-28 16:17 ` [PATCH 2/2] pipe: avoid repeat check of pipe->readers with pipe lock held Kemeng Shi
  0 siblings, 2 replies; 4+ messages in thread
From: Kemeng Shi @ 2023-09-28 16:17 UTC (permalink / raw)
  To: viro, brauner, dhowells; +Cc: linux-fsdevel, linux-kernel

This series tries to remove some unnecessary check in pipe. More details
can be found in respective patches. Thanks!

Kemeng Shi (2):
  pipe: remove pipe_full check with wrong head in pipe_write
  pipe: avoid repeat check of pipe->readers under pipe_lock

 fs/pipe.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

-- 
2.30.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] pipe: remove pipe_full check with wrong head in pipe_write
  2023-09-28 16:17 [PATCH 0/2] Two cleanups to pipe Kemeng Shi
@ 2023-09-28 16:17 ` Kemeng Shi
  2023-09-28 16:17 ` [PATCH 2/2] pipe: avoid repeat check of pipe->readers with pipe lock held Kemeng Shi
  1 sibling, 0 replies; 4+ messages in thread
From: Kemeng Shi @ 2023-09-28 16:17 UTC (permalink / raw)
  To: viro, brauner, dhowells; +Cc: linux-fsdevel, linux-kernel

In pipe_write we have:
	head = pipe->head;
	if (!pipe_full(head, pipe->tail, pipe->max_usage))
		pipe->head = head + 1;
		/* write data to buffer at head */

	/* supposed to check if pipe is full after write */
	if (!pipe_full(head, pipe->tail, pipe->max_usage))
		continue

The second pipe_full expects head after write but head before write is
used. Luckily, we will call pipe_full with correct reloaded pipe->head
in new loop cycle and stop writing correctly. Remove wrong pipe_full
check and simply continue to first pipe_full check after write done to
avoid unnecessary check.

Fixes: a194dfe6e6f6 ("pipe: Rearrange sequence in pipe_write() to preallocate slot")
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 fs/pipe.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/pipe.c b/fs/pipe.c
index 2d88f73f585a..b19875720ff1 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -542,10 +542,9 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
 
 			if (!iov_iter_count(from))
 				break;
-		}
 
-		if (!pipe_full(head, pipe->tail, pipe->max_usage))
 			continue;
+		}
 
 		/* Wait for buffer space to become available. */
 		if ((filp->f_flags & O_NONBLOCK) ||
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] pipe: avoid repeat check of pipe->readers with pipe lock held
  2023-09-28 16:17 [PATCH 0/2] Two cleanups to pipe Kemeng Shi
  2023-09-28 16:17 ` [PATCH 1/2] pipe: remove pipe_full check with wrong head in pipe_write Kemeng Shi
@ 2023-09-28 16:17 ` Kemeng Shi
  1 sibling, 0 replies; 4+ messages in thread
From: Kemeng Shi @ 2023-09-28 16:17 UTC (permalink / raw)
  To: viro, brauner, dhowells; +Cc: linux-fsdevel, linux-kernel

Change to pipe->readers is protected by pipe_lock. Only recheck
pipe->reader after relock to avoid repeat check of pipe->readers with
pipe lock held.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 fs/pipe.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/fs/pipe.c b/fs/pipe.c
index b19875720ff1..e9c63f66d1c7 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -479,13 +479,6 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
 	}
 
 	for (;;) {
-		if (!pipe->readers) {
-			send_sig(SIGPIPE, current, 0);
-			if (!ret)
-				ret = -EPIPE;
-			break;
-		}
-
 		head = pipe->head;
 		if (!pipe_full(head, pipe->tail, pipe->max_usage)) {
 			unsigned int mask = pipe->ring_size - 1;
@@ -573,6 +566,12 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
 		__pipe_lock(pipe);
 		was_empty = pipe_empty(pipe->head, pipe->tail);
 		wake_next_writer = true;
+		if (!pipe->readers) {
+			send_sig(SIGPIPE, current, 0);
+			if (!ret)
+				ret = -EPIPE;
+			break;
+		}
 	}
 out:
 	if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 0/2] Two cleanups to pipe
@ 2023-10-08  9:47 Kemeng Shi
  0 siblings, 0 replies; 4+ messages in thread
From: Kemeng Shi @ 2023-10-08  9:47 UTC (permalink / raw)
  To: viro, brauner, dhowells; +Cc: linux-fsdevel, linux-kernel

This series tries to remove some unnecessary check in pipe. More details
can be found in respective patches. Thanks!

Kemeng Shi (2):
  pipe: remove pipe_full check with wrong head in pipe_write
  pipe: avoid repeat check of pipe->readers under pipe_lock

 fs/pipe.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

-- 
2.30.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-08  1:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-28 16:17 [PATCH 0/2] Two cleanups to pipe Kemeng Shi
2023-09-28 16:17 ` [PATCH 1/2] pipe: remove pipe_full check with wrong head in pipe_write Kemeng Shi
2023-09-28 16:17 ` [PATCH 2/2] pipe: avoid repeat check of pipe->readers with pipe lock held Kemeng Shi
  -- strict thread matches above, loose matches on Subject: below --
2023-10-08  9:47 [PATCH 0/2] Two cleanups to pipe Kemeng Shi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).