All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-07 16:48 ` zengzhaoxiu at 163.com
  0 siblings, 0 replies; 16+ messages in thread
From: zengzhaoxiu @ 2016-04-07 16:48 UTC (permalink / raw)
  To: kgene, k.kozlowski, boris.brezillon, richard, dwmw2, computersforpeace
  Cc: linux-arm-kernel, linux-samsung-soc, linux-mtd, linux-kernel,
	Zeng Zhaoxiu

From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>

If there is only one bit difference in the ECC, the function should return 1.
The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
actually returns -1.

Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
whether the diff0 has only one 1-bit.
---
 drivers/mtd/nand/s3c2410.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index 9c9397b..c9698cf 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
 	diff0 |= (diff1 << 8);
 	diff0 |= (diff2 << 16);
 
-	if ((diff0 & ~(1<<fls(diff0))) == 0)
+	if ((diff0 & (diff0 - 1)) == 0)
 		return 1;
 
 	return -1;
-- 
2.5.5

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-07 16:48 ` zengzhaoxiu at 163.com
  0 siblings, 0 replies; 16+ messages in thread
From: zengzhaoxiu at 163.com @ 2016-04-07 16:48 UTC (permalink / raw)
  To: linux-arm-kernel

From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>

If there is only one bit difference in the ECC, the function should return 1.
The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
actually returns -1.

Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
whether the diff0 has only one 1-bit.
---
 drivers/mtd/nand/s3c2410.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index 9c9397b..c9698cf 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
 	diff0 |= (diff1 << 8);
 	diff0 |= (diff2 << 16);
 
-	if ((diff0 & ~(1<<fls(diff0))) == 0)
+	if ((diff0 & (diff0 - 1)) == 0)
 		return 1;
 
 	return -1;
-- 
2.5.5

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-07 16:48 ` zengzhaoxiu at 163.com
@ 2016-04-08  0:18   ` Boris Brezillon
  -1 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-08  0:18 UTC (permalink / raw)
  To: zengzhaoxiu
  Cc: kgene, k.kozlowski, richard, dwmw2, computersforpeace,
	linux-arm-kernel, linux-samsung-soc, linux-mtd, linux-kernel,
	Zeng Zhaoxiu

Hi Zeng,

On Fri,  8 Apr 2016 00:48:17 +0800
zengzhaoxiu@163.com wrote:

> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> 
> If there is only one bit difference in the ECC, the function should return 1.
> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> actually returns -1.
> 
> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> whether the diff0 has only one 1-bit.

Missing Signed-off-by here.

> ---
>  drivers/mtd/nand/s3c2410.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> index 9c9397b..c9698cf 100644
> --- a/drivers/mtd/nand/s3c2410.c
> +++ b/drivers/mtd/nand/s3c2410.c
> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>  	diff0 |= (diff1 << 8);
>  	diff0 |= (diff2 << 16);
>  
> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> +	if ((diff0 & (diff0 - 1)) == 0)

Or just

	if (hweight_long((unsigned long)diff0) == 1)

which is doing exactly what the comment says.

BTW, I don't understand why the current code is wrong? To me, it seems
it's correctly detecting the case where only a single bit is different.
What are you trying to fix exactly?

Best Regards,

Boris

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-08  0:18   ` Boris Brezillon
  0 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-08  0:18 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Zeng,

On Fri,  8 Apr 2016 00:48:17 +0800
zengzhaoxiu at 163.com wrote:

> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> 
> If there is only one bit difference in the ECC, the function should return 1.
> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> actually returns -1.
> 
> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> whether the diff0 has only one 1-bit.

Missing Signed-off-by here.

> ---
>  drivers/mtd/nand/s3c2410.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> index 9c9397b..c9698cf 100644
> --- a/drivers/mtd/nand/s3c2410.c
> +++ b/drivers/mtd/nand/s3c2410.c
> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>  	diff0 |= (diff1 << 8);
>  	diff0 |= (diff2 << 16);
>  
> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> +	if ((diff0 & (diff0 - 1)) == 0)

Or just

	if (hweight_long((unsigned long)diff0) == 1)

which is doing exactly what the comment says.

BTW, I don't understand why the current code is wrong? To me, it seems
it's correctly detecting the case where only a single bit is different.
What are you trying to fix exactly?

Best Regards,

Boris

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-08  0:18   ` Boris Brezillon
@ 2016-04-08  1:51     ` Zeng Zhaoxiu
  -1 siblings, 0 replies; 16+ messages in thread
From: Zeng Zhaoxiu @ 2016-04-08  1:51 UTC (permalink / raw)
  To: Boris Brezillon, zengzhaoxiu
  Cc: kgene, k.kozlowski, richard, dwmw2, computersforpeace,
	linux-arm-kernel, linux-samsung-soc, linux-mtd, linux-kernel



在 2016年04月08日 08:18, Boris Brezillon 写道:
> Hi Zeng,
>
> On Fri,  8 Apr 2016 00:48:17 +0800
> zengzhaoxiu@163.com wrote:
>
>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
>>
>> If there is only one bit difference in the ECC, the function should return 1.
>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
>> actually returns -1.
>>
>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
>> whether the diff0 has only one 1-bit.
> Missing Signed-off-by here.
>
>> ---
>>   drivers/mtd/nand/s3c2410.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
>> index 9c9397b..c9698cf 100644
>> --- a/drivers/mtd/nand/s3c2410.c
>> +++ b/drivers/mtd/nand/s3c2410.c
>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>>   	diff0 |= (diff1 << 8);
>>   	diff0 |= (diff2 << 16);
>>   
>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
>> +	if ((diff0 & (diff0 - 1)) == 0)
> Or just
>
> 	if (hweight_long((unsigned long)diff0) == 1)
>
> which is doing exactly what the comment says.
>
> BTW, I don't understand why the current code is wrong? To me, it seems
> it's correctly detecting the case where only a single bit is different.
> What are you trying to fix exactly?
>
> Best Regards,
>
> Boris
>

For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.

__fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-08  1:51     ` Zeng Zhaoxiu
  0 siblings, 0 replies; 16+ messages in thread
From: Zeng Zhaoxiu @ 2016-04-08  1:51 UTC (permalink / raw)
  To: linux-arm-kernel



? 2016?04?08? 08:18, Boris Brezillon ??:
> Hi Zeng,
>
> On Fri,  8 Apr 2016 00:48:17 +0800
> zengzhaoxiu at 163.com wrote:
>
>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
>>
>> If there is only one bit difference in the ECC, the function should return 1.
>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
>> actually returns -1.
>>
>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
>> whether the diff0 has only one 1-bit.
> Missing Signed-off-by here.
>
>> ---
>>   drivers/mtd/nand/s3c2410.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
>> index 9c9397b..c9698cf 100644
>> --- a/drivers/mtd/nand/s3c2410.c
>> +++ b/drivers/mtd/nand/s3c2410.c
>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>>   	diff0 |= (diff1 << 8);
>>   	diff0 |= (diff2 << 16);
>>   
>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
>> +	if ((diff0 & (diff0 - 1)) == 0)
> Or just
>
> 	if (hweight_long((unsigned long)diff0) == 1)
>
> which is doing exactly what the comment says.
>
> BTW, I don't understand why the current code is wrong? To me, it seems
> it's correctly detecting the case where only a single bit is different.
> What are you trying to fix exactly?
>
> Best Regards,
>
> Boris
>

For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.

__fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-08  1:51     ` Zeng Zhaoxiu
@ 2016-04-08  2:18       ` Boris Brezillon
  -1 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-08  2:18 UTC (permalink / raw)
  To: Zeng Zhaoxiu
  Cc: zengzhaoxiu, kgene, k.kozlowski, richard, dwmw2,
	computersforpeace, linux-arm-kernel, linux-samsung-soc,
	linux-mtd, linux-kernel

On Fri, 8 Apr 2016 09:51:04 +0800
Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:

> 
> 
> 在 2016年04月08日 08:18, Boris Brezillon 写道:
> > Hi Zeng,
> >
> > On Fri,  8 Apr 2016 00:48:17 +0800
> > zengzhaoxiu@163.com wrote:
> >
> >> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> >>
> >> If there is only one bit difference in the ECC, the function should return 1.
> >> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> >> actually returns -1.
> >>
> >> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> >> whether the diff0 has only one 1-bit.
> > Missing Signed-off-by here.
> >
> >> ---
> >>   drivers/mtd/nand/s3c2410.c | 2 +-
> >>   1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> >> index 9c9397b..c9698cf 100644
> >> --- a/drivers/mtd/nand/s3c2410.c
> >> +++ b/drivers/mtd/nand/s3c2410.c
> >> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
> >>   	diff0 |= (diff1 << 8);
> >>   	diff0 |= (diff2 << 16);
> >>   
> >> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> >> +	if ((diff0 & (diff0 - 1)) == 0)
> > Or just
> >
> > 	if (hweight_long((unsigned long)diff0) == 1)
> >
> > which is doing exactly what the comment says.
> >
> > BTW, I don't understand why the current code is wrong? To me, it seems
> > it's correctly detecting the case where only a single bit is different.
> > What are you trying to fix exactly?
> >
> > Best Regards,
> >
> > Boris
> >
> 
> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
> 
> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
> 

Indeed, I forgot that fls() was returning (position + 1). Anyway, I
still think using hweight clarifies what you really want to test.

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-08  2:18       ` Boris Brezillon
  0 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-08  2:18 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, 8 Apr 2016 09:51:04 +0800
Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:

> 
> 
> ? 2016?04?08? 08:18, Boris Brezillon ??:
> > Hi Zeng,
> >
> > On Fri,  8 Apr 2016 00:48:17 +0800
> > zengzhaoxiu at 163.com wrote:
> >
> >> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> >>
> >> If there is only one bit difference in the ECC, the function should return 1.
> >> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> >> actually returns -1.
> >>
> >> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> >> whether the diff0 has only one 1-bit.
> > Missing Signed-off-by here.
> >
> >> ---
> >>   drivers/mtd/nand/s3c2410.c | 2 +-
> >>   1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> >> index 9c9397b..c9698cf 100644
> >> --- a/drivers/mtd/nand/s3c2410.c
> >> +++ b/drivers/mtd/nand/s3c2410.c
> >> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
> >>   	diff0 |= (diff1 << 8);
> >>   	diff0 |= (diff2 << 16);
> >>   
> >> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> >> +	if ((diff0 & (diff0 - 1)) == 0)
> > Or just
> >
> > 	if (hweight_long((unsigned long)diff0) == 1)
> >
> > which is doing exactly what the comment says.
> >
> > BTW, I don't understand why the current code is wrong? To me, it seems
> > it's correctly detecting the case where only a single bit is different.
> > What are you trying to fix exactly?
> >
> > Best Regards,
> >
> > Boris
> >
> 
> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
> 
> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
> 

Indeed, I forgot that fls() was returning (position + 1). Anyway, I
still think using hweight clarifies what you really want to test.

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-08  2:18       ` Boris Brezillon
@ 2016-04-08  5:37         ` Zeng Zhaoxiu
  -1 siblings, 0 replies; 16+ messages in thread
From: Zeng Zhaoxiu @ 2016-04-08  5:37 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: zengzhaoxiu, kgene, k.kozlowski, richard, dwmw2,
	computersforpeace, linux-arm-kernel, linux-samsung-soc,
	linux-mtd, linux-kernel

在 2016年04月08日 10:18, Boris Brezillon 写道:
> On Fri, 8 Apr 2016 09:51:04 +0800
> Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:
>
>>
>> 在 2016年04月08日 08:18, Boris Brezillon 写道:
>>> Hi Zeng,
>>>
>>> On Fri,  8 Apr 2016 00:48:17 +0800
>>> zengzhaoxiu@163.com wrote:
>>>
>>>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
>>>>
>>>> If there is only one bit difference in the ECC, the function should return 1.
>>>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
>>>> actually returns -1.
>>>>
>>>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
>>>> whether the diff0 has only one 1-bit.
>>> Missing Signed-off-by here.
>>>
>>>> ---
>>>>    drivers/mtd/nand/s3c2410.c | 2 +-
>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
>>>> index 9c9397b..c9698cf 100644
>>>> --- a/drivers/mtd/nand/s3c2410.c
>>>> +++ b/drivers/mtd/nand/s3c2410.c
>>>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>>>>    	diff0 |= (diff1 << 8);
>>>>    	diff0 |= (diff2 << 16);
>>>>    
>>>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
>>>> +	if ((diff0 & (diff0 - 1)) == 0)
>>> Or just
>>>
>>> 	if (hweight_long((unsigned long)diff0) == 1)
>>>
>>> which is doing exactly what the comment says.
>>>
>>> BTW, I don't understand why the current code is wrong? To me, it seems
>>> it's correctly detecting the case where only a single bit is different.
>>> What are you trying to fix exactly?
>>>
>>> Best Regards,
>>>
>>> Boris
>>>
>> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
>> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
>>
>> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
>>
> Indeed, I forgot that fls() was returning (position + 1). Anyway, I
> still think using hweight clarifies what you really want to test.
>

"(n & (n - 1))" is used in is_power_of_2() in incluse/linux/log2.h,
it's result is equal to "n & ~(1 << __ffs(n))".

"(diff & (diff - 1))" is simple and fast, although here is not performance critical.
To improve readability of this code, we should add a new function and use it.

/*
  *  Determine whether some value has more than one 1-bits
  */

static inline __attribute__((const))
bool more_than_1_bit_set(unsigned long n)
{
     return (n & (n - 1)) != 0;
}

OTOH, I found many determinations like "hweightN(n) > 1" distributed in kernel,
these determinations are slower than "(n & (n - 1)) != 0" on most CPUs.
We can use this new function instead.

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-08  5:37         ` Zeng Zhaoxiu
  0 siblings, 0 replies; 16+ messages in thread
From: Zeng Zhaoxiu @ 2016-04-08  5:37 UTC (permalink / raw)
  To: linux-arm-kernel

? 2016?04?08? 10:18, Boris Brezillon ??:
> On Fri, 8 Apr 2016 09:51:04 +0800
> Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:
>
>>
>> ? 2016?04?08? 08:18, Boris Brezillon ??:
>>> Hi Zeng,
>>>
>>> On Fri,  8 Apr 2016 00:48:17 +0800
>>> zengzhaoxiu at 163.com wrote:
>>>
>>>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
>>>>
>>>> If there is only one bit difference in the ECC, the function should return 1.
>>>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
>>>> actually returns -1.
>>>>
>>>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
>>>> whether the diff0 has only one 1-bit.
>>> Missing Signed-off-by here.
>>>
>>>> ---
>>>>    drivers/mtd/nand/s3c2410.c | 2 +-
>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
>>>> index 9c9397b..c9698cf 100644
>>>> --- a/drivers/mtd/nand/s3c2410.c
>>>> +++ b/drivers/mtd/nand/s3c2410.c
>>>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>>>>    	diff0 |= (diff1 << 8);
>>>>    	diff0 |= (diff2 << 16);
>>>>    
>>>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
>>>> +	if ((diff0 & (diff0 - 1)) == 0)
>>> Or just
>>>
>>> 	if (hweight_long((unsigned long)diff0) == 1)
>>>
>>> which is doing exactly what the comment says.
>>>
>>> BTW, I don't understand why the current code is wrong? To me, it seems
>>> it's correctly detecting the case where only a single bit is different.
>>> What are you trying to fix exactly?
>>>
>>> Best Regards,
>>>
>>> Boris
>>>
>> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
>> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
>>
>> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
>>
> Indeed, I forgot that fls() was returning (position + 1). Anyway, I
> still think using hweight clarifies what you really want to test.
>

"(n & (n - 1))" is used in is_power_of_2() in incluse/linux/log2.h,
it's result is equal to "n & ~(1 << __ffs(n))".

"(diff & (diff - 1))" is simple and fast, although here is not performance critical.
To improve readability of this code, we should add a new function and use it.

/*
  *  Determine whether some value has more than one 1-bits
  */

static inline __attribute__((const))
bool more_than_1_bit_set(unsigned long n)
{
     return (n & (n - 1)) != 0;
}

OTOH, I found many determinations like "hweightN(n) > 1" distributed in kernel,
these determinations are slower than "(n & (n - 1)) != 0" on most CPUs.
We can use this new function instead.

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-08  5:37         ` Zeng Zhaoxiu
@ 2016-04-11  7:49           ` Boris Brezillon
  -1 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-11  7:49 UTC (permalink / raw)
  To: Zeng Zhaoxiu
  Cc: zengzhaoxiu, kgene, k.kozlowski, richard, dwmw2,
	computersforpeace, linux-arm-kernel, linux-samsung-soc,
	linux-mtd, linux-kernel

Hi Zeng,

On Fri, 8 Apr 2016 13:37:22 +0800
Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:

> 在 2016年04月08日 10:18, Boris Brezillon 写道:
> > On Fri, 8 Apr 2016 09:51:04 +0800
> > Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:
> >
> >>
> >> 在 2016年04月08日 08:18, Boris Brezillon 写道:
> >>> Hi Zeng,
> >>>
> >>> On Fri,  8 Apr 2016 00:48:17 +0800
> >>> zengzhaoxiu@163.com wrote:
> >>>
> >>>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> >>>>
> >>>> If there is only one bit difference in the ECC, the function should return 1.
> >>>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> >>>> actually returns -1.
> >>>>
> >>>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> >>>> whether the diff0 has only one 1-bit.
> >>> Missing Signed-off-by here.
> >>>
> >>>> ---
> >>>>    drivers/mtd/nand/s3c2410.c | 2 +-
> >>>>    1 file changed, 1 insertion(+), 1 deletion(-)
> >>>>
> >>>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> >>>> index 9c9397b..c9698cf 100644
> >>>> --- a/drivers/mtd/nand/s3c2410.c
> >>>> +++ b/drivers/mtd/nand/s3c2410.c
> >>>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
> >>>>    	diff0 |= (diff1 << 8);
> >>>>    	diff0 |= (diff2 << 16);
> >>>>    
> >>>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> >>>> +	if ((diff0 & (diff0 - 1)) == 0)
> >>> Or just
> >>>
> >>> 	if (hweight_long((unsigned long)diff0) == 1)
> >>>
> >>> which is doing exactly what the comment says.
> >>>
> >>> BTW, I don't understand why the current code is wrong? To me, it seems
> >>> it's correctly detecting the case where only a single bit is different.
> >>> What are you trying to fix exactly?
> >>>
> >>> Best Regards,
> >>>
> >>> Boris
> >>>
> >> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
> >> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
> >>
> >> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
> >>
> > Indeed, I forgot that fls() was returning (position + 1). Anyway, I
> > still think using hweight clarifies what you really want to test.
> >
> 
> "(n & (n - 1))" is used in is_power_of_2() in incluse/linux/log2.h,
> it's result is equal to "n & ~(1 << __ffs(n))".
> 
> "(diff & (diff - 1))" is simple and fast, although here is not performance critical.
> To improve readability of this code, we should add a new function and use it.
> 
> /*
>   *  Determine whether some value has more than one 1-bits
>   */
> 
> static inline __attribute__((const))
> bool more_than_1_bit_set(unsigned long n)
> {
>      return (n & (n - 1)) != 0;
> }
> 
> OTOH, I found many determinations like "hweightN(n) > 1" distributed in kernel,
> these determinations are slower than "(n & (n - 1)) != 0" on most CPUs.

Yes, probably, but it may be faster on a few CPUs :). Anyway, not sure
you should bother optimizing this now, especially since this test is in
the ECC correction path, and I doubt it makes any difference (detecting
and correcting errors is what takes most of the time here).

> We can use this new function instead.
> 

In the end, I don't care that much which solution you'll choose, since
it's driver specific code. Pick whatever implementation you prefer and
resend the patch with your SoB.

Thanks,

Boris

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-11  7:49           ` Boris Brezillon
  0 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-11  7:49 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Zeng,

On Fri, 8 Apr 2016 13:37:22 +0800
Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:

> ? 2016?04?08? 10:18, Boris Brezillon ??:
> > On Fri, 8 Apr 2016 09:51:04 +0800
> > Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com> wrote:
> >
> >>
> >> ? 2016?04?08? 08:18, Boris Brezillon ??:
> >>> Hi Zeng,
> >>>
> >>> On Fri,  8 Apr 2016 00:48:17 +0800
> >>> zengzhaoxiu at 163.com wrote:
> >>>
> >>>> From: Zeng Zhaoxiu <zhaoxiu.zeng@gmail.com>
> >>>>
> >>>> If there is only one bit difference in the ECC, the function should return 1.
> >>>> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> >>>> actually returns -1.
> >>>>
> >>>> Here, we can use the simple expression "(diff0 & (diff0 - 1)) == 0" to determine
> >>>> whether the diff0 has only one 1-bit.
> >>> Missing Signed-off-by here.
> >>>
> >>>> ---
> >>>>    drivers/mtd/nand/s3c2410.c | 2 +-
> >>>>    1 file changed, 1 insertion(+), 1 deletion(-)
> >>>>
> >>>> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> >>>> index 9c9397b..c9698cf 100644
> >>>> --- a/drivers/mtd/nand/s3c2410.c
> >>>> +++ b/drivers/mtd/nand/s3c2410.c
> >>>> @@ -542,7 +542,7 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
> >>>>    	diff0 |= (diff1 << 8);
> >>>>    	diff0 |= (diff2 << 16);
> >>>>    
> >>>> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> >>>> +	if ((diff0 & (diff0 - 1)) == 0)
> >>> Or just
> >>>
> >>> 	if (hweight_long((unsigned long)diff0) == 1)
> >>>
> >>> which is doing exactly what the comment says.
> >>>
> >>> BTW, I don't understand why the current code is wrong? To me, it seems
> >>> it's correctly detecting the case where only a single bit is different.
> >>> What are you trying to fix exactly?
> >>>
> >>> Best Regards,
> >>>
> >>> Boris
> >>>
> >> For example, assuming diff0 is 1, then fls(diff0) is equal to 1, then "~(1 << fls(diff0))" is equal to 0xfffffffd,
> >> then the result of "(diff0 & ~(1 << fls(diff0)))" is 1 , not we expected 0.
> >>
> >> __fls(diff0) and "(fls(diff0) - 1)" are all right, but fls(diff0) is wrong.
> >>
> > Indeed, I forgot that fls() was returning (position + 1). Anyway, I
> > still think using hweight clarifies what you really want to test.
> >
> 
> "(n & (n - 1))" is used in is_power_of_2() in incluse/linux/log2.h,
> it's result is equal to "n & ~(1 << __ffs(n))".
> 
> "(diff & (diff - 1))" is simple and fast, although here is not performance critical.
> To improve readability of this code, we should add a new function and use it.
> 
> /*
>   *  Determine whether some value has more than one 1-bits
>   */
> 
> static inline __attribute__((const))
> bool more_than_1_bit_set(unsigned long n)
> {
>      return (n & (n - 1)) != 0;
> }
> 
> OTOH, I found many determinations like "hweightN(n) > 1" distributed in kernel,
> these determinations are slower than "(n & (n - 1)) != 0" on most CPUs.

Yes, probably, but it may be faster on a few CPUs :). Anyway, not sure
you should bother optimizing this now, especially since this test is in
the ECC correction path, and I doubt it makes any difference (detecting
and correcting errors is what takes most of the time here).

> We can use this new function instead.
> 

In the end, I don't care that much which solution you'll choose, since
it's driver specific code. Pick whatever implementation you prefer and
resend the patch with your SoB.

Thanks,

Boris

-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-11  7:49           ` Boris Brezillon
@ 2016-04-12  7:30             ` zengzhaoxiu at 163.com
  -1 siblings, 0 replies; 16+ messages in thread
From: zengzhaoxiu @ 2016-04-12  7:30 UTC (permalink / raw)
  To: boris.brezillon
  Cc: kgene, k.kozlowski, richard, dwmw2, computersforpeace,
	linux-arm-kernel, linux-samsung-soc, linux-mtd, linux-kernel,
	Zhaoxiu Zeng

From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

If there is only one bit difference in the ECC, the function should return 1.
The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
actually returns -1.

Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
---
 drivers/mtd/nand/s3c2410.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index 9c9397b..86ffb73 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -542,7 +542,8 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
 	diff0 |= (diff1 << 8);
 	diff0 |= (diff2 << 16);
 
-	if ((diff0 & ~(1<<fls(diff0))) == 0)
+	/* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
+	if ((diff0 & (diff0 - 1)) == 0)
 		return 1;
 
 	return -1;
-- 
2.5.0

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-12  7:30             ` zengzhaoxiu at 163.com
  0 siblings, 0 replies; 16+ messages in thread
From: zengzhaoxiu at 163.com @ 2016-04-12  7:30 UTC (permalink / raw)
  To: linux-arm-kernel

From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

If there is only one bit difference in the ECC, the function should return 1.
The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
actually returns -1.

Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
---
 drivers/mtd/nand/s3c2410.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index 9c9397b..86ffb73 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -542,7 +542,8 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
 	diff0 |= (diff1 << 8);
 	diff0 |= (diff2 << 16);
 
-	if ((diff0 & ~(1<<fls(diff0))) == 0)
+	/* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
+	if ((diff0 & (diff0 - 1)) == 0)
 		return 1;
 
 	return -1;
-- 
2.5.0

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

* Re: [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
  2016-04-12  7:30             ` zengzhaoxiu at 163.com
@ 2016-04-12 11:34               ` Boris Brezillon
  -1 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-12 11:34 UTC (permalink / raw)
  To: zengzhaoxiu
  Cc: kgene, k.kozlowski, richard, dwmw2, computersforpeace,
	linux-arm-kernel, linux-samsung-soc, linux-mtd, linux-kernel,
	Zhaoxiu Zeng

On Tue, 12 Apr 2016 15:30:35 +0800
zengzhaoxiu@163.com wrote:

> From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
> 
> If there is only one bit difference in the ECC, the function should return 1.
> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> actually returns -1.
> 
> Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

Applied.

Thanks,

Boris

> ---
>  drivers/mtd/nand/s3c2410.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> index 9c9397b..86ffb73 100644
> --- a/drivers/mtd/nand/s3c2410.c
> +++ b/drivers/mtd/nand/s3c2410.c
> @@ -542,7 +542,8 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>  	diff0 |= (diff1 << 8);
>  	diff0 |= (diff2 << 16);
>  
> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> +	/* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
> +	if ((diff0 & (diff0 - 1)) == 0)
>  		return 1;
>  
>  	return -1;



-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

* [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data()
@ 2016-04-12 11:34               ` Boris Brezillon
  0 siblings, 0 replies; 16+ messages in thread
From: Boris Brezillon @ 2016-04-12 11:34 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, 12 Apr 2016 15:30:35 +0800
zengzhaoxiu at 163.com wrote:

> From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
> 
> If there is only one bit difference in the ECC, the function should return 1.
> The result of "diff0 & ~(1<<fls(diff0))" is equal to diff0, so the function
> actually returns -1.
> 
> Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>

Applied.

Thanks,

Boris

> ---
>  drivers/mtd/nand/s3c2410.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
> index 9c9397b..86ffb73 100644
> --- a/drivers/mtd/nand/s3c2410.c
> +++ b/drivers/mtd/nand/s3c2410.c
> @@ -542,7 +542,8 @@ static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
>  	diff0 |= (diff1 << 8);
>  	diff0 |= (diff2 << 16);
>  
> -	if ((diff0 & ~(1<<fls(diff0))) == 0)
> +	/* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
> +	if ((diff0 & (diff0 - 1)) == 0)
>  		return 1;
>  
>  	return -1;



-- 
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

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

end of thread, other threads:[~2016-04-12 11:34 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-07 16:48 [PATCH] mtd: nand: s3c2410: fix bug in s3c2410_nand_correct_data() zengzhaoxiu
2016-04-07 16:48 ` zengzhaoxiu at 163.com
2016-04-08  0:18 ` Boris Brezillon
2016-04-08  0:18   ` Boris Brezillon
2016-04-08  1:51   ` Zeng Zhaoxiu
2016-04-08  1:51     ` Zeng Zhaoxiu
2016-04-08  2:18     ` Boris Brezillon
2016-04-08  2:18       ` Boris Brezillon
2016-04-08  5:37       ` Zeng Zhaoxiu
2016-04-08  5:37         ` Zeng Zhaoxiu
2016-04-11  7:49         ` Boris Brezillon
2016-04-11  7:49           ` Boris Brezillon
2016-04-12  7:30           ` zengzhaoxiu
2016-04-12  7:30             ` zengzhaoxiu at 163.com
2016-04-12 11:34             ` Boris Brezillon
2016-04-12 11:34               ` Boris Brezillon

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.