All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values
@ 2017-03-24 12:41 Nikolaus Schulz
  2017-03-25 18:03 ` Jonathan Cameron
  0 siblings, 1 reply; 5+ messages in thread
From: Nikolaus Schulz @ 2017-03-24 12:41 UTC (permalink / raw)
  To: Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, open list:IIO SUBSYSTEM AND DRIVERS,
	open list
  Cc: Nikolaus Schulz, stable

Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
switching from do_div(), which can't handle negative numbers, to
div_s64_rem().  Also use shift_right for shifting, which is safe with
negative values.

Signed-off-by: Nikolaus Schulz <nikolaus.schulz@avionic-design.de>
Cc: stable@vger.kernel.org
---
 drivers/iio/industrialio-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index d18ded4..3ff91e0 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -610,10 +610,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
 	case IIO_VAL_FRACTIONAL_LOG2:
-		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
-		tmp1 = do_div(tmp, 1000000000LL);
-		tmp0 = tmp;
-		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
+		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
+		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
+		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
 	case IIO_VAL_INT_MULTIPLE:
 	{
 		int i;
-- 
2.1.4

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

* Re: [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values
  2017-03-24 12:41 [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values Nikolaus Schulz
@ 2017-03-25 18:03 ` Jonathan Cameron
  2017-04-02  9:26   ` Jonathan Cameron
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2017-03-25 18:03 UTC (permalink / raw)
  To: Nikolaus Schulz, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, open list:IIO SUBSYSTEM AND DRIVERS,
	open list
  Cc: stable

On 24/03/17 12:41, Nikolaus Schulz wrote:
> Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
> switching from do_div(), which can't handle negative numbers, to
> div_s64_rem().  Also use shift_right for shifting, which is safe with
> negative values.
> 
> Signed-off-by: Nikolaus Schulz <nikolaus.schulz@avionic-design.de>
> Cc: stable@vger.kernel.org
Looks sane to me, but I'd like to give others time to comment on this
just in case there is some odd condition neither of us has thought of!

Give me a poke if we get nothing else for a few weeks.

Jonathan
> ---
>  drivers/iio/industrialio-core.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index d18ded4..3ff91e0 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -610,10 +610,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
>  		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>  	case IIO_VAL_FRACTIONAL_LOG2:
> -		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
> -		tmp1 = do_div(tmp, 1000000000LL);
> -		tmp0 = tmp;
> -		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
> +		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
> +		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
> +		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>  	case IIO_VAL_INT_MULTIPLE:
>  	{
>  		int i;
> 

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

* Re: [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values
  2017-03-25 18:03 ` Jonathan Cameron
@ 2017-04-02  9:26   ` Jonathan Cameron
  2017-04-02 10:09     ` Lars-Peter Clausen
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2017-04-02  9:26 UTC (permalink / raw)
  To: Nikolaus Schulz, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, open list:IIO SUBSYSTEM AND DRIVERS,
	open list
  Cc: stable

On 25/03/17 18:03, Jonathan Cameron wrote:
> On 24/03/17 12:41, Nikolaus Schulz wrote:
>> Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
>> switching from do_div(), which can't handle negative numbers, to
>> div_s64_rem().  Also use shift_right for shifting, which is safe with
>> negative values.
>>
>> Signed-off-by: Nikolaus Schulz <nikolaus.schulz@avionic-design.de>
>> Cc: stable@vger.kernel.org
> Looks sane to me, but I'd like to give others time to comment on this
> just in case there is some odd condition neither of us has thought of!
> 
> Give me a poke if we get nothing else for a few weeks.
Lars, I think this might have been your magic in the first place.

Could you sanity check this one please. It's in the category of very
risky of both Nikolaus and I have missed something!

Thanks,

Jonathan
> 
> Jonathan
>> ---
>>  drivers/iio/industrialio-core.c | 7 +++----
>>  1 file changed, 3 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
>> index d18ded4..3ff91e0 100644
>> --- a/drivers/iio/industrialio-core.c
>> +++ b/drivers/iio/industrialio-core.c
>> @@ -610,10 +610,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
>>  		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>  	case IIO_VAL_FRACTIONAL_LOG2:
>> -		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>> -		tmp1 = do_div(tmp, 1000000000LL);
>> -		tmp0 = tmp;
>> -		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
>> +		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
>> +		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
>> +		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>  	case IIO_VAL_INT_MULTIPLE:
>>  	{
>>  		int i;
>>
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

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

* Re: [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values
  2017-04-02  9:26   ` Jonathan Cameron
@ 2017-04-02 10:09     ` Lars-Peter Clausen
  2017-04-02 10:15       ` Jonathan Cameron
  0 siblings, 1 reply; 5+ messages in thread
From: Lars-Peter Clausen @ 2017-04-02 10:09 UTC (permalink / raw)
  To: Jonathan Cameron, Nikolaus Schulz, Hartmut Knaack,
	Peter Meerwald-Stadler, open list:IIO SUBSYSTEM AND DRIVERS,
	open list
  Cc: stable

On 04/02/2017 11:26 AM, Jonathan Cameron wrote:
> On 25/03/17 18:03, Jonathan Cameron wrote:
>> On 24/03/17 12:41, Nikolaus Schulz wrote:
>>> Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
>>> switching from do_div(), which can't handle negative numbers, to
>>> div_s64_rem().  Also use shift_right for shifting, which is safe with
>>> negative values.
>>>
>>> Signed-off-by: Nikolaus Schulz <nikolaus.schulz@avionic-design.de>
>>> Cc: stable@vger.kernel.org
>> Looks sane to me, but I'd like to give others time to comment on this
>> just in case there is some odd condition neither of us has thought of!
>>
>> Give me a poke if we get nothing else for a few weeks.
> Lars, I think this might have been your magic in the first place.
> 
> Could you sanity check this one please. It's in the category of very
> risky of both Nikolaus and I have missed something!

It's the same as this:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/iio/industrialio-core.c?id=171c0091837c81ed5c949fec6966bb5afff2d1cf

Should be OK.

Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>

> 
> Thanks,
> 
> Jonathan
>>
>> Jonathan
>>> ---
>>>  drivers/iio/industrialio-core.c | 7 +++----
>>>  1 file changed, 3 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
>>> index d18ded4..3ff91e0 100644
>>> --- a/drivers/iio/industrialio-core.c
>>> +++ b/drivers/iio/industrialio-core.c
>>> @@ -610,10 +610,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>>>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
>>>  		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>>  	case IIO_VAL_FRACTIONAL_LOG2:
>>> -		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>>> -		tmp1 = do_div(tmp, 1000000000LL);
>>> -		tmp0 = tmp;
>>> -		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
>>> +		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
>>> +		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
>>> +		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>>  	case IIO_VAL_INT_MULTIPLE:
>>>  	{
>>>  		int i;
>>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

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

* Re: [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values
  2017-04-02 10:09     ` Lars-Peter Clausen
@ 2017-04-02 10:15       ` Jonathan Cameron
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2017-04-02 10:15 UTC (permalink / raw)
  To: Lars-Peter Clausen, Nikolaus Schulz, Hartmut Knaack,
	Peter Meerwald-Stadler, open list:IIO SUBSYSTEM AND DRIVERS,
	open list
  Cc: stable

On 02/04/17 11:09, Lars-Peter Clausen wrote:
> On 04/02/2017 11:26 AM, Jonathan Cameron wrote:
>> On 25/03/17 18:03, Jonathan Cameron wrote:
>>> On 24/03/17 12:41, Nikolaus Schulz wrote:
>>>> Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
>>>> switching from do_div(), which can't handle negative numbers, to
>>>> div_s64_rem().  Also use shift_right for shifting, which is safe with
>>>> negative values.
>>>>
>>>> Signed-off-by: Nikolaus Schulz <nikolaus.schulz@avionic-design.de>
>>>> Cc: stable@vger.kernel.org
>>> Looks sane to me, but I'd like to give others time to comment on this
>>> just in case there is some odd condition neither of us has thought of!
>>>
>>> Give me a poke if we get nothing else for a few weeks.
>> Lars, I think this might have been your magic in the first place.
>>
>> Could you sanity check this one please. It's in the category of very
>> risky of both Nikolaus and I have missed something!
> 
> It's the same as this:
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/iio/industrialio-core.c?id=171c0091837c81ed5c949fec6966bb5afff2d1cf
> 
> Should be OK.
> 
> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
Good point.  I'd not thought to check and had forgotten that one.

Applied to the fixes-togreg branch.  Thanks,

Jonathan
> 
>>
>> Thanks,
>>
>> Jonathan
>>>
>>> Jonathan
>>>> ---
>>>>  drivers/iio/industrialio-core.c | 7 +++----
>>>>  1 file changed, 3 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
>>>> index d18ded4..3ff91e0 100644
>>>> --- a/drivers/iio/industrialio-core.c
>>>> +++ b/drivers/iio/industrialio-core.c
>>>> @@ -610,10 +610,9 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>>>>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
>>>>  		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>>>  	case IIO_VAL_FRACTIONAL_LOG2:
>>>> -		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
>>>> -		tmp1 = do_div(tmp, 1000000000LL);
>>>> -		tmp0 = tmp;
>>>> -		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
>>>> +		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
>>>> +		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
>>>> +		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>>>>  	case IIO_VAL_INT_MULTIPLE:
>>>>  	{
>>>>  		int i;
>>>>
>>>
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-iio" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
> 

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

end of thread, other threads:[~2017-04-02 10:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-24 12:41 [PATCH] iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values Nikolaus Schulz
2017-03-25 18:03 ` Jonathan Cameron
2017-04-02  9:26   ` Jonathan Cameron
2017-04-02 10:09     ` Lars-Peter Clausen
2017-04-02 10:15       ` Jonathan Cameron

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.