Skip to content

Commit d810d4c

Browse files
committed
fs/pipe: do not open-code pipe head/tail logic in FIONREAD
Rasmus points out that we do indeed have other cases of breakage from the type changes that were introduced on 32-bit targets in order to read the pipe head and tail values atomically (commit 3d25216: "fs/pipe: Read pipe->{head,tail} atomically outside pipe->mutex"). Fix it up by using the proper helper functions that now deal with the pipe buffer index types properly. This makes the code simpler and more obvious. The compiler does the CSE and loop hoisting of the pipe ring size masking that we used to do manually, so open-coding this was never a good idea. Reported-by: Rasmus Villemoes <ravi@prevas.dk> Link: https://lore.kernel.org/all/87cyeu5zgk.fsf@prevas.dk/ Fixes: 3d25216 ("fs/pipe: Read pipe->{head,tail} atomically outside pipe->mutex")Cc: Oleg Nesterov <oleg@redhat.com> Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Swapnil Sapkal <swapnil.sapkal@amd.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 74d42bd commit d810d4c

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

fs/pipe.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,18 +614,17 @@ pipe_write(struct kiocb *iocb, struct iov_iter *from)
614614
static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
615615
{
616616
struct pipe_inode_info *pipe = filp->private_data;
617-
unsigned int count, head, tail, mask;
617+
unsigned int count, head, tail;
618618

619619
switch (cmd) {
620620
case FIONREAD:
621621
mutex_lock(&pipe->mutex);
622622
count = 0;
623623
head = pipe->head;
624624
tail = pipe->tail;
625-
mask = pipe->ring_size - 1;
626625

627-
while (tail != head) {
628-
count += pipe->bufs[tail & mask].len;
626+
while (!pipe_empty(head, tail)) {
627+
count += pipe_buf(pipe, tail)->len;
629628
tail++;
630629
}
631630
mutex_unlock(&pipe->mutex);

0 commit comments

Comments
 (0)