Re: [PATCH] Input: serio_raw - ensure we don't block in non-blocking read

From: Che-liang Chiou
Date: Tue Apr 17 2012 - 05:15:24 EST


Reviewed-by: Che-Liang Chiou <clchiou@xxxxxxxxxxxx>

On Tue, Apr 17, 2012 at 4:56 PM, Che-liang Chiou <clchiou@xxxxxxxxxxxx> wrote:
> I didn't test the patch, but the logic looks fine to me.
>
> On Wed, Apr 4, 2012 at 4:24 AM, Dmitry Torokhov
> <dmitry.torokhov@xxxxxxxxx> wrote:
>> Avoid calling wait_event_interruptible() if client requested non-blocking
>> read, since it is not guaranteed that another thread will not consume
>> event after we checked if serio_raw->head != serio_raw->tail.
>>
>> Also ensure we do not return 0 but keep waiting instead in blocking case,
>> when another thread steals "our" byte.
>>
>> Signed-off-by: Dmitry Torokhov <dtor@xxxxxxx>
>> ---
>> Âdrivers/input/serio/serio_raw.c | Â 43 ++++++++++++++++++++++----------------
>> Â1 files changed, 25 insertions(+), 18 deletions(-)
>>
>> diff --git a/drivers/input/serio/serio_raw.c b/drivers/input/serio/serio_raw.c
>> index 4494233..094d6fb 100644
>> --- a/drivers/input/serio/serio_raw.c
>> +++ b/drivers/input/serio/serio_raw.c
>> @@ -165,31 +165,38 @@ static ssize_t serio_raw_read(struct file *file, char __user *buffer,
>> Â Â Â Âstruct serio_raw *serio_raw = client->serio_raw;
>> Â Â Â Âchar uninitialized_var(c);
>> Â Â Â Âssize_t read = 0;
>> - Â Â Â int retval;
>> + Â Â Â int error = 0;
>>
>> - Â Â Â if (serio_raw->dead)
>> - Â Â Â Â Â Â Â return -ENODEV;
>> + Â Â Â do {
>> + Â Â Â Â Â Â Â if (serio_raw->dead)
>> + Â Â Â Â Â Â Â Â Â Â Â return -ENODEV;
>>
>> - Â Â Â if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
>> - Â Â Â Â Â Â Â return -EAGAIN;
>> + Â Â Â Â Â Â Â if (serio_raw->head == serio_raw->tail &&
>> + Â Â Â Â Â Â Â Â Â (file->f_flags & O_NONBLOCK))
>> + Â Â Â Â Â Â Â Â Â Â Â return -EAGAIN;
>>
>> - Â Â Â retval = wait_event_interruptible(serio_raw->wait,
>> - Â Â Â Â Â Â Â Â Â Â Â serio_raw->head != serio_raw->tail || serio_raw->dead);
>> - Â Â Â if (retval)
>> - Â Â Â Â Â Â Â return retval;
>> + Â Â Â Â Â Â Â if (count == 0)
>> + Â Â Â Â Â Â Â Â Â Â Â break;
>>
>> - Â Â Â if (serio_raw->dead)
>> - Â Â Â Â Â Â Â return -ENODEV;
>> + Â Â Â Â Â Â Â while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
>> + Â Â Â Â Â Â Â Â Â Â Â if (put_user(c, buffer++)) {
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â error = -EFAULT;
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
>> + Â Â Â Â Â Â Â Â Â Â Â }
>> + Â Â Â Â Â Â Â Â Â Â Â read++;
>> + Â Â Â Â Â Â Â }
>>
>> - Â Â Â while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
>> - Â Â Â Â Â Â Â if (put_user(c, buffer++)) {
>> - Â Â Â Â Â Â Â Â Â Â Â retval = -EFAULT;
>> + Â Â Â Â Â Â Â if (read)
>> Â Â Â Â Â Â Â Â Â Â Â Âbreak;
>> - Â Â Â Â Â Â Â }
>> - Â Â Â Â Â Â Â read++;
>> - Â Â Â }
>>
>> - Â Â Â return read ?: retval;
>> + Â Â Â Â Â Â Â if (!(file->f_flags & O_NONBLOCK))
>> + Â Â Â Â Â Â Â Â Â Â Â error = wait_event_interruptible(serio_raw->wait,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â serio_raw->head != serio_raw->tail ||
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â serio_raw->dead);
>> + Â Â Â } while (!error);
>> +
>> +out:
>> + Â Â Â return read ?: error;
>> Â}
>>
>> Âstatic ssize_t serio_raw_write(struct file *file, const char __user *buffer,
>> --
>> 1.7.7.6
>>
>>
>> --
>> Dmitry
N‹§²æìr¸›yúèšØb²X¬¶ÇvØ^–)Þ{.nÇ+‰·¥Š{±‘êçzX§¶›¡Ü}©ž²ÆzÚ&j:+v‰¨¾«‘êçzZ+€Ê+zf£¢·hšˆ§~†­†Ûiÿûàz¹®w¥¢¸?™¨è­Ú&¢)ßf”ù^jÇy§m…á@A«a¶Úÿ 0¶ìh®å’i