All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tty: serdev: fix serdev_device_write return value
@ 2017-05-02  0:17 Rob Herring
  2017-05-02  9:25 ` Johan Hovold
  0 siblings, 1 reply; 5+ messages in thread
From: Rob Herring @ 2017-05-02  0:17 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-serial, linux-kernel, Andrey Smirnov

Commit 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
provides a compatibility wrapper for the existing
serdev_device_write_buf, but it fails to return the number of bytes
written causing users to timeout.

Fixes: 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Rob Herring <robh@kernel.org>
---
 drivers/tty/serdev/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
index 433de5ea9b02..ccfe56355c4f 100644
--- a/drivers/tty/serdev/core.c
+++ b/drivers/tty/serdev/core.c
@@ -127,7 +127,7 @@ int serdev_device_write(struct serdev_device *serdev,
 			unsigned long timeout)
 {
 	struct serdev_controller *ctrl = serdev->ctrl;
-	int ret;
+	int ret, wr_cnt = 0;
 
 	if (!ctrl || !ctrl->ops->write_buf ||
 	    (timeout && !serdev->ops->write_wakeup))
@@ -143,12 +143,13 @@ int serdev_device_write(struct serdev_device *serdev,
 
 		buf += ret;
 		count -= ret;
+		wr_cnt += ret;
 
 	} while (count &&
 		 (timeout = wait_for_completion_timeout(&serdev->write_comp,
 							timeout)));
 	mutex_unlock(&serdev->write_lock);
-	return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
+	return ret < 0 ? ret : (count ? -ETIMEDOUT : wr_cnt);
 }
 EXPORT_SYMBOL_GPL(serdev_device_write);
 
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: serdev: fix serdev_device_write return value
  2017-05-02  0:17 [PATCH] tty: serdev: fix serdev_device_write return value Rob Herring
@ 2017-05-02  9:25 ` Johan Hovold
  2017-05-02 12:30   ` Rob Herring
  2017-05-03 17:44   ` Andy Shevchenko
  0 siblings, 2 replies; 5+ messages in thread
From: Johan Hovold @ 2017-05-02  9:25 UTC (permalink / raw)
  To: Rob Herring
  Cc: Greg Kroah-Hartman, linux-serial, linux-kernel, Andrey Smirnov

On Mon, May 01, 2017 at 07:17:14PM -0500, Rob Herring wrote:
> Commit 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
> provides a compatibility wrapper for the existing
> serdev_device_write_buf, but it fails to return the number of bytes
> written causing users to timeout.

So this would also be fixed for serdev_device_write_buf() by Stefan
Wahren's patch restoring that function implementation, but returning the
amount written is perhaps desirable also for blocking writes for
consistency reasons.

> Fixes: 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
> Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Rob Herring <robh@kernel.org>
> ---
>  drivers/tty/serdev/core.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
> index 433de5ea9b02..ccfe56355c4f 100644
> --- a/drivers/tty/serdev/core.c
> +++ b/drivers/tty/serdev/core.c
> @@ -127,7 +127,7 @@ int serdev_device_write(struct serdev_device *serdev,
>  			unsigned long timeout)
>  {
>  	struct serdev_controller *ctrl = serdev->ctrl;
> -	int ret;
> +	int ret, wr_cnt = 0;
>  
>  	if (!ctrl || !ctrl->ops->write_buf ||
>  	    (timeout && !serdev->ops->write_wakeup))
> @@ -143,12 +143,13 @@ int serdev_device_write(struct serdev_device *serdev,
>  
>  		buf += ret;
>  		count -= ret;
> +		wr_cnt += ret;
>  
>  	} while (count &&
>  		 (timeout = wait_for_completion_timeout(&serdev->write_comp,
>  							timeout)));
>
>  	mutex_unlock(&serdev->write_lock);
> -	return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
> +	return ret < 0 ? ret : (count ? -ETIMEDOUT : wr_cnt);

That's some nasty use of the ternary operator. Ditching it completely
would be more readable.

	if (ret < 0)
		return ret;

	if (count)
		return -ETIMEDOUT;

	return wr_count;

and here wr_count is the value of count passed to the function (and
could just be stored on entry instead).

>  }
>  EXPORT_SYMBOL_GPL(serdev_device_write);

Thanks,
Johan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: serdev: fix serdev_device_write return value
  2017-05-02  9:25 ` Johan Hovold
@ 2017-05-02 12:30   ` Rob Herring
  2017-05-03 17:44   ` Andy Shevchenko
  1 sibling, 0 replies; 5+ messages in thread
From: Rob Herring @ 2017-05-02 12:30 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Greg Kroah-Hartman, linux-serial, linux-kernel, Andrey Smirnov

On Tue, May 2, 2017 at 4:25 AM, Johan Hovold <johan@kernel.org> wrote:
> On Mon, May 01, 2017 at 07:17:14PM -0500, Rob Herring wrote:
>> Commit 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
>> provides a compatibility wrapper for the existing
>> serdev_device_write_buf, but it fails to return the number of bytes
>> written causing users to timeout.
>
> So this would also be fixed for serdev_device_write_buf() by Stefan
> Wahren's patch restoring that function implementation, but returning the
> amount written is perhaps desirable also for blocking writes for
> consistency reasons.

Yes, I saw it after I wrote this. We should apply both IMO.

>> Fixes: 6fe729c4bdae ("serdev: Add serdev_device_write subroutine")
>> Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>> Signed-off-by: Rob Herring <robh@kernel.org>
>> ---
>>  drivers/tty/serdev/core.c | 5 +++--
>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
>> index 433de5ea9b02..ccfe56355c4f 100644
>> --- a/drivers/tty/serdev/core.c
>> +++ b/drivers/tty/serdev/core.c
>> @@ -127,7 +127,7 @@ int serdev_device_write(struct serdev_device *serdev,
>>                       unsigned long timeout)
>>  {
>>       struct serdev_controller *ctrl = serdev->ctrl;
>> -     int ret;
>> +     int ret, wr_cnt = 0;
>>
>>       if (!ctrl || !ctrl->ops->write_buf ||
>>           (timeout && !serdev->ops->write_wakeup))
>> @@ -143,12 +143,13 @@ int serdev_device_write(struct serdev_device *serdev,
>>
>>               buf += ret;
>>               count -= ret;
>> +             wr_cnt += ret;
>>
>>       } while (count &&
>>                (timeout = wait_for_completion_timeout(&serdev->write_comp,
>>                                                       timeout)));
>>
>>       mutex_unlock(&serdev->write_lock);
>> -     return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
>> +     return ret < 0 ? ret : (count ? -ETIMEDOUT : wr_cnt);
>
> That's some nasty use of the ternary operator. Ditching it completely
> would be more readable.
>
>         if (ret < 0)
>                 return ret;
>
>         if (count)
>                 return -ETIMEDOUT;
>
>         return wr_count;
>
> and here wr_count is the value of count passed to the function (and
> could just be stored on entry instead).

Okay.

I'll wait for Greg to apply Stefan's patch and respin on top of it.

Rob

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: serdev: fix serdev_device_write return value
  2017-05-02  9:25 ` Johan Hovold
  2017-05-02 12:30   ` Rob Herring
@ 2017-05-03 17:44   ` Andy Shevchenko
  2017-05-03 18:06     ` Greg Kroah-Hartman
  1 sibling, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2017-05-03 17:44 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Rob Herring, Greg Kroah-Hartman, linux-serial, linux-kernel,
	Andrey Smirnov

On Tue, May 2, 2017 at 12:25 PM, Johan Hovold <johan@kernel.org> wrote:
> On Mon, May 01, 2017 at 07:17:14PM -0500, Rob Herring wrote:

>> -     return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
>> +     return ret < 0 ? ret : (count ? -ETIMEDOUT : wr_cnt);
>
> That's some nasty use of the ternary operator. Ditching it completely
> would be more readable.
>
>         if (ret < 0)
>                 return ret;
>
>         if (count)
>                 return -ETIMEDOUT;
>
>         return wr_count;


While I agree on the first part, I would go still with one ternary at the end:

            return count ? -ETIMEDOUT : wr_count;

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tty: serdev: fix serdev_device_write return value
  2017-05-03 17:44   ` Andy Shevchenko
@ 2017-05-03 18:06     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2017-05-03 18:06 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Johan Hovold, Rob Herring, linux-serial, linux-kernel, Andrey Smirnov

On Wed, May 03, 2017 at 08:44:07PM +0300, Andy Shevchenko wrote:
> On Tue, May 2, 2017 at 12:25 PM, Johan Hovold <johan@kernel.org> wrote:
> > On Mon, May 01, 2017 at 07:17:14PM -0500, Rob Herring wrote:
> 
> >> -     return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
> >> +     return ret < 0 ? ret : (count ? -ETIMEDOUT : wr_cnt);
> >
> > That's some nasty use of the ternary operator. Ditching it completely
> > would be more readable.
> >
> >         if (ret < 0)
> >                 return ret;
> >
> >         if (count)
> >                 return -ETIMEDOUT;
> >
> >         return wr_count;
> 
> 
> While I agree on the first part, I would go still with one ternary at the end:
> 
>             return count ? -ETIMEDOUT : wr_count;

Ick, no, make it easy to read, we write code for developers first, the
compiler second.  Ditching it completly is a good idea.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-05-03 18:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-02  0:17 [PATCH] tty: serdev: fix serdev_device_write return value Rob Herring
2017-05-02  9:25 ` Johan Hovold
2017-05-02 12:30   ` Rob Herring
2017-05-03 17:44   ` Andy Shevchenko
2017-05-03 18:06     ` Greg Kroah-Hartman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.