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

From: Mateusz Guzik

Date: Thu Oct 15 2026 - 07:06:23 EST


On Thu, Oct 15, 2026 at 04:53:26PM +0800, Li Qiang wrote:
> 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% |
>

If you are looking to improve speed here, the real gains would come from
redoing the loop with scoped_user_write_access_size machinery to avoid
repeat SMAP trips.

But that may be quite a bit of work. The patch as is seems to help so
I'm certainly not protesting it.

> 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
>