Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1634052 > unrolled thread

[PATCH] tty: serdev: fix serdev_device_write return value

Started byRob Herring <robh@kernel.org>
First post2017-05-02 02:20 +0200
Last post2017-05-03 20:10 +0200
Articles 5 — 4 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] tty: serdev: fix serdev_device_write return value Rob Herring <robh@kernel.org> - 2017-05-02 02:20 +0200
    Re: [PATCH] tty: serdev: fix serdev_device_write return value Johan Hovold <johan@kernel.org> - 2017-05-02 11:30 +0200
      Re: [PATCH] tty: serdev: fix serdev_device_write return value Rob Herring <robh@kernel.org> - 2017-05-02 14:40 +0200
      Re: [PATCH] tty: serdev: fix serdev_device_write return value Andy Shevchenko <andy.shevchenko@gmail.com> - 2017-05-03 19:50 +0200
        Re: [PATCH] tty: serdev: fix serdev_device_write return value Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-03 20:10 +0200

#1634052 — [PATCH] tty: serdev: fix serdev_device_write return value

FromRob Herring <robh@kernel.org>
Date2017-05-02 02:20 +0200
Subject[PATCH] tty: serdev: fix serdev_device_write return value
Message-ID<tCtHP-7sj-1@gated-at.bofh.it>
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

[toc] | [next] | [standalone]


#1634286

FromJohan Hovold <johan@kernel.org>
Date2017-05-02 11:30 +0200
Message-ID<tCCi6-4Jn-25@gated-at.bofh.it>
In reply to#1634052
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

[toc] | [prev] | [next] | [standalone]


#1634381

FromRob Herring <robh@kernel.org>
Date2017-05-02 14:40 +0200
Message-ID<tCFfY-6Iv-19@gated-at.bofh.it>
In reply to#1634286
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

[toc] | [prev] | [next] | [standalone]


#1635179

FromAndy Shevchenko <andy.shevchenko@gmail.com>
Date2017-05-03 19:50 +0200
Message-ID<tD6zx-8vy-25@gated-at.bofh.it>
In reply to#1634286
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

[toc] | [prev] | [next] | [standalone]


#1635183

FromGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Date2017-05-03 20:10 +0200
Message-ID<tD6SR-tq-3@gated-at.bofh.it>
In reply to#1635179
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

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web