[PATCH] pipe: bound new-page copies by remaining iterator bytes

From: Li Qiang

Date: Thu Oct 15 2026 - 04:58:13 EST


anon_pipe_write() requests PAGE_SIZE bytes even when fewer bytes
remain in the source iterator. This causes copy_page_from_iter()
to perform an unnecessary extra iteration after copying all data.

Limit the copy size to the remaining iterator bytes to avoid
this overhead without changing copy semantics.

UnixBench pipe results on Hygon C86 7490 (Hygon 4):

| Threads | Before | After | Improvement |
|---------|--------|-------|-------------|
| 1 | 835 | 854 | +2.28% |
| 256 | 179293 | 182895 | +2.01% |

Signed-off-by: Li Qiang <liqiang01@xxxxxxxxxx>
---
fs/pipe.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/pipe.c b/fs/pipe.c
index 292425a834dd..da5583f7f37d 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -595,7 +595,9 @@ anon_pipe_write(struct kiocb *iocb, struct iov_iter *from)
break;
}

- copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
+ copied = copy_page_from_iter(page, 0,
+ min_t(size_t, PAGE_SIZE,
+ iov_iter_count(from)), from);
if (unlikely(copied < PAGE_SIZE && iov_iter_count(from))) {
anon_pipe_put_page(pipe, page);
if (!ret)

base-commit: 47324d3a5b3abd781295044d01d92d09f184e872
--
2.43.0