Skip to content

Commit 2ba4746

Browse files
warthog618Bartosz Golaszewski
authored andcommitted
gpiolib: cdev: Cleanup kfifo_out() error handling
The handling of kfifo_out() errors in read functions obscures any error. The error condition should never occur but, while a ret is set to -EIO, it is subsequently ignored and the read functions instead return the number of bytes copied to that point, potentially masking the fact that any error occurred. Log a warning and return -EIO in the case of a kfifo_out() error to make it clear something very odd is going on here. Signed-off-by: Kent Gibson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 4ce5ca6 commit 2ba4746

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

drivers/gpio/gpiolib-cdev.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,16 +1642,15 @@ static ssize_t linereq_read(struct file *file, char __user *buf,
16421642
return ret;
16431643
}
16441644

1645-
ret = kfifo_out(&lr->events, &le, 1);
1646-
}
1647-
if (ret != 1) {
1648-
/*
1649-
* This should never happen - we were holding the
1650-
* lock from the moment we learned the fifo is no
1651-
* longer empty until now.
1652-
*/
1653-
ret = -EIO;
1654-
break;
1645+
if (kfifo_out(&lr->events, &le, 1) != 1) {
1646+
/*
1647+
* This should never happen - we hold the
1648+
* lock from the moment we learned the fifo
1649+
* is no longer empty until now.
1650+
*/
1651+
WARN(1, "failed to read from non-empty kfifo");
1652+
return -EIO;
1653+
}
16551654
}
16561655

16571656
if (copy_to_user(buf + bytes_read, &le, sizeof(le)))
@@ -1995,16 +1994,15 @@ static ssize_t lineevent_read(struct file *file, char __user *buf,
19951994
return ret;
19961995
}
19971996

1998-
ret = kfifo_out(&le->events, &ge, 1);
1999-
}
2000-
if (ret != 1) {
2001-
/*
2002-
* This should never happen - we were holding the lock
2003-
* from the moment we learned the fifo is no longer
2004-
* empty until now.
2005-
*/
2006-
ret = -EIO;
2007-
break;
1997+
if (kfifo_out(&le->events, &ge, 1) != 1) {
1998+
/*
1999+
* This should never happen - we hold the
2000+
* lock from the moment we learned the fifo
2001+
* is no longer empty until now.
2002+
*/
2003+
WARN(1, "failed to read from non-empty kfifo");
2004+
return -EIO;
2005+
}
20082006
}
20092007

20102008
if (copy_to_user(buf + bytes_read, &ge, ge_size))
@@ -2707,12 +2705,15 @@ static ssize_t lineinfo_watch_read(struct file *file, char __user *buf,
27072705
if (count < event_size)
27082706
return -EINVAL;
27092707
#endif
2710-
ret = kfifo_out(&cdev->events, &event, 1);
2711-
}
2712-
if (ret != 1) {
2713-
ret = -EIO;
2714-
break;
2715-
/* We should never get here. See lineevent_read(). */
2708+
if (kfifo_out(&cdev->events, &event, 1) != 1) {
2709+
/*
2710+
* This should never happen - we hold the
2711+
* lock from the moment we learned the fifo
2712+
* is no longer empty until now.
2713+
*/
2714+
WARN(1, "failed to read from non-empty kfifo");
2715+
return -EIO;
2716+
}
27162717
}
27172718

27182719
#ifdef CONFIG_GPIO_CDEV_V1

0 commit comments

Comments
 (0)