public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [patch] mtd: nand: silence some shift wrap warnings
@ 2013-08-09  9:49 Dan Carpenter
  2013-08-13  6:23 ` Huang Shijie
  2013-08-16 14:55 ` Artem Bityutskiy
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Carpenter @ 2013-08-09  9:49 UTC (permalink / raw)
  To: David Woodhouse
  Cc: Artem Bityutskiy, kernel-janitors, Matthieu CASTET, Huang Shijie,
	linux-mtd, Brian Norris

There are static checkers which complain when we declare variables as
64 bit bitfields but only use the lower 32 bits because of shift
wrapping.  In this case "len" is declared as u64 as opposed to unsigned
long or something which might be 32 bits.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index 9a48758..3eddd04 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -108,13 +108,13 @@ static int check_offs_len(struct mtd_info *mtd,
 	int ret = 0;
 
 	/* Start address must align on block boundary */
-	if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
+	if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
 		pr_debug("%s: unaligned address\n", __func__);
 		ret = -EINVAL;
 	}
 
 	/* Length must align on block boundary */
-	if (len & ((1 << chip->phys_erase_shift) - 1)) {
+	if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
 		pr_debug("%s: length not block aligned\n", __func__);
 		ret = -EINVAL;
 	}
@@ -394,7 +394,7 @@ static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
 		memset(&einfo, 0, sizeof(einfo));
 		einfo.mtd = mtd;
 		einfo.addr = ofs;
-		einfo.len = 1 << chip->phys_erase_shift;
+		einfo.len = 1ULL << chip->phys_erase_shift;
 		nand_erase_nand(mtd, &einfo, 0);
 
 		/* Write bad block marker to OOB */
@@ -2630,7 +2630,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 		}
 
 		/* Increment page address and decrement length */
-		len -= (1 << chip->phys_erase_shift);
+		len -= (1ULL << chip->phys_erase_shift);
 		page += pages_per_block;
 
 		/* Check, if we cross a chip boundary */

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

* Re: [patch] mtd: nand: silence some shift wrap warnings
  2013-08-09  9:49 [patch] mtd: nand: silence some shift wrap warnings Dan Carpenter
@ 2013-08-13  6:23 ` Huang Shijie
  2013-08-13  6:35   ` Dan Carpenter
  2013-08-16 14:55 ` Artem Bityutskiy
  1 sibling, 1 reply; 5+ messages in thread
From: Huang Shijie @ 2013-08-13  6:23 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Artem Bityutskiy, kernel-janitors, Matthieu CASTET, linux-mtd,
	Brian Norris, David Woodhouse

于 2013年08月09日 17:49, Dan Carpenter 写道:
> There are static checkers which complain when we declare variables as

could you tell me which "checker" and how do you test it?

you'd better post the warning message here.

thanks
Huang Shijie
> 64 bit bitfields but only use the lower 32 bits because of shift
> wrapping.  In this case "len" is declared as u64 as opposed to unsigned
> long or something which might be 32 bits.
>
> Signed-off-by: Dan Carpenter<dan.carpenter@oracle.com>
>
> diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
> index 9a48758..3eddd04 100644
> --- a/drivers/mtd/nand/nand_base.c
> +++ b/drivers/mtd/nand/nand_base.c
> @@ -108,13 +108,13 @@ static int check_offs_len(struct mtd_info *mtd,
>   	int ret = 0;
>
>   	/* Start address must align on block boundary */
> -	if (ofs&  ((1<<  chip->phys_erase_shift) - 1)) {
> +	if (ofs&  ((1ULL<<  chip->phys_erase_shift) - 1)) {
>   		pr_debug("%s: unaligned address\n", __func__);
>   		ret = -EINVAL;
>   	}
>
>   	/* Length must align on block boundary */
> -	if (len&  ((1<<  chip->phys_erase_shift) - 1)) {
> +	if (len&  ((1ULL<<  chip->phys_erase_shift) - 1)) {
>   		pr_debug("%s: length not block aligned\n", __func__);
>   		ret = -EINVAL;
>   	}
> @@ -394,7 +394,7 @@ static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
>   		memset(&einfo, 0, sizeof(einfo));
>   		einfo.mtd = mtd;
>   		einfo.addr = ofs;
> -		einfo.len = 1<<  chip->phys_erase_shift;
> +		einfo.len = 1ULL<<  chip->phys_erase_shift;
>   		nand_erase_nand(mtd,&einfo, 0);
>
>   		/* Write bad block marker to OOB */
> @@ -2630,7 +2630,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
>   		}
>
>   		/* Increment page address and decrement length */
> -		len -= (1<<  chip->phys_erase_shift);
> +		len -= (1ULL<<  chip->phys_erase_shift);
>   		page += pages_per_block;
>
>   		/* Check, if we cross a chip boundary */
>

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

* Re: [patch] mtd: nand: silence some shift wrap warnings
  2013-08-13  6:23 ` Huang Shijie
@ 2013-08-13  6:35   ` Dan Carpenter
  2013-08-13  7:31     ` Brian Norris
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2013-08-13  6:35 UTC (permalink / raw)
  To: Huang Shijie
  Cc: Artem Bityutskiy, kernel-janitors, Matthieu CASTET, linux-mtd,
	Brian Norris, David Woodhouse

On Tue, Aug 13, 2013 at 02:23:02PM +0800, Huang Shijie wrote:
> 于 2013年08月09日 17:49, Dan Carpenter 写道:
> >There are static checkers which complain when we declare variables as
> 
> could you tell me which "checker" and how do you test it?
> 
> you'd better post the warning message here.
> 

It's some stuff I'm working on but haven't released yet.  But "len"
and "ofs" are declared as u64 and loff_t.  If they were declared as
unsigned long then maybe the current code would be fine because the
code would be correct-ish on 32 bit arches.

So this is definitely the right thing to do.

regards,
dan carpenter

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

* Re: [patch] mtd: nand: silence some shift wrap warnings
  2013-08-13  6:35   ` Dan Carpenter
@ 2013-08-13  7:31     ` Brian Norris
  0 siblings, 0 replies; 5+ messages in thread
From: Brian Norris @ 2013-08-13  7:31 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Artem Bityutskiy, kernel-janitors, Matthieu CASTET, Huang Shijie,
	linux-mtd, David Woodhouse

On 08/12/2013 11:35 PM, Dan Carpenter wrote:
> On Tue, Aug 13, 2013 at 02:23:02PM +0800, Huang Shijie wrote:
>> 于 2013年08月09日 17:49, Dan Carpenter 写道:
>>> There are static checkers which complain when we declare variables as
>>
>> could you tell me which "checker" and how do you test it?
>>
>> you'd better post the warning message here.
>>
>
> It's some stuff I'm working on but haven't released yet.  But "len"

Which static checker isn't all that important, so no worries. The patch 
can stand on its own merits.

> and "ofs" are declared as u64 and loff_t.  If they were declared as
> unsigned long then maybe the current code would be fine because the
> code would be correct-ish on 32 bit arches.

IIUC, it's still correct, since we surely will never have an eraseblock 
size near 4GB (i.e., if the shift is ever larger than 31, we have a bug 
somewhere else).

> So this is definitely the right thing to do.

As far as types are concerned, yes, this is still the right thing to do.

Brian

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

* Re: [patch] mtd: nand: silence some shift wrap warnings
  2013-08-09  9:49 [patch] mtd: nand: silence some shift wrap warnings Dan Carpenter
  2013-08-13  6:23 ` Huang Shijie
@ 2013-08-16 14:55 ` Artem Bityutskiy
  1 sibling, 0 replies; 5+ messages in thread
From: Artem Bityutskiy @ 2013-08-16 14:55 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: kernel-janitors, Matthieu CASTET, Huang Shijie, linux-mtd,
	Brian Norris, David Woodhouse

On Fri, 2013-08-09 at 12:49 +0300, Dan Carpenter wrote:
> There are static checkers which complain when we declare variables as
> 64 bit bitfields but only use the lower 32 bits because of shift
> wrapping.  In this case "len" is declared as u64 as opposed to unsigned
> long or something which might be 32 bits.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Pushed to l2-mtd.git, thanks!

-- 
Best Regards,
Artem Bityutskiy

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

end of thread, other threads:[~2013-08-16 14:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-09  9:49 [patch] mtd: nand: silence some shift wrap warnings Dan Carpenter
2013-08-13  6:23 ` Huang Shijie
2013-08-13  6:35   ` Dan Carpenter
2013-08-13  7:31     ` Brian Norris
2013-08-16 14:55 ` Artem Bityutskiy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox