[Devel] [PATCH hci-8.2] fs/pipe: use kvfree() for the buffer arrays of a resized pipe
Pavel Tikhomirov
ptikhomirov at virtuozzo.com
Tue Dec 1 16:08:37 MSK 2026
Reviewed-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
On 12/1/26 13:41, Vasileios Almpanis wrote:
> Since commit c14599b9e5f7 ("fs/pipe: Use kvcalloc for pipe buffers")
> the buffer array of a pipe comes from kvcalloc(), but
> pipe_resize_ring() still frees it in two wrong ways.
>
> When the pipe holds more data than the new size, the EBUSY path frees
> the new array with kfree(). For a large pipe the array comes from
> vmalloc(), and kfree() of a vmalloc address crashes the node. Host root
> hits it with a filled 1 GiB pipe shrunk to 512 MiB:
>
> BUG: unable to handle page fault for address: ffffeb0200088008
> RIP: 0010:kfree+0x83/0x460
> pipe_resize_ring+0x546/0x5d0
> pipe_fcntl+0x2da/0x500
> do_fcntl+0x236/0x1560
>
> On success the old array is freed with kvfree() under
> pipe->rd_wait.lock with interrupts off, and vfree() can sleep:
>
> BUG: sleeping function called from invalid context at mm/vmalloc.c
>
> Use kvfree() on the EBUSY path, and free the old array after the
> unlock.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-158055
>
> Feature: mm: avoid high order allocations in all subsystems
> Fixes: c14599b9e5f7 ("fs/pipe: Use kvcalloc for pipe buffers")
> Reported-by: Artem Bulgakov <artem.bulgakov at virtuozzo.com>
> Signed-off-by: Vasileios Almpanis <vasileios.almpanis at virtuozzo.com>
> ---
> fs/pipe.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fs/pipe.c b/fs/pipe.c
> index aff1e06a9e2e..4d683870bbfd 100644
> --- a/fs/pipe.c
> +++ b/fs/pipe.c
> @@ -1266,7 +1266,7 @@ unsigned int round_pipe_size(unsigned int size)
> */
> int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
> {
> - struct pipe_buffer *bufs;
> + struct pipe_buffer *bufs, *old_bufs;
> unsigned int head, tail, mask, n;
>
> bufs = kvcalloc(nr_slots, sizeof(*bufs),
> @@ -1282,7 +1282,7 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
> n = pipe_occupancy(head, tail);
> if (nr_slots < n) {
> spin_unlock_irq(&pipe->rd_wait.lock);
> - kfree(bufs);
> + kvfree(bufs);
> return -EBUSY;
> }
>
> @@ -1309,7 +1309,7 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
> head = n;
> tail = 0;
>
> - kvfree(pipe->bufs);
> + old_bufs = pipe->bufs;
> pipe->bufs = bufs;
> pipe->ring_size = nr_slots;
> if (pipe->max_usage > nr_slots)
> @@ -1324,6 +1324,8 @@ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
>
> spin_unlock_irq(&pipe->rd_wait.lock);
>
> + kvfree(old_bufs);
> +
> /* This might have made more room for writers */
> wake_up_interruptible(&pipe->wr_wait);
> return 0;
>
> ---
> base-commit: beda1840d22b7707e3f8e18f413afbfffef93d25
> change-id: 20261201-pipe-resize-kvfree-ba7884e83579
>
--
Best regards, Pavel Tikhomirov
Senior Software Developer, Virtuozzo.
More information about the Devel
mailing list