Skip to contentSkip to conversation

drivers/mtd/maps/physmap-bt1-rom.c:78:18: sparse: sparse: cast removes address space '__iomem' of expression

DISCUSSION kernel test robot 4 participants 12 messages Nov 2, 2020 – Nov 12, 2020 Memory technology devices (MTD)

lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head:   3cea11cd5e3b00d91caf0b4730194039b45c5891
commit: b3e79e7682e075326df8041b826b03453acacd0a mtd: physmap: Add Baikal-T1 physically mapped ROM support
date:   4 weeks ago
config: sparc64-randconfig-s032-20201031 (attached as .config)
compiler: sparc64-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-76-gf680124b-dirty
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b3e79e7682e075326df8041b826b03453acacd0a
git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
git fetch --no-tags linus master
git checkout b3e79e7682e075326df8041b826b03453acacd0a
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=sparc64

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"

>> drivers/mtd/maps/physmap-bt1-rom.c:78:18: sparse: sparse: cast removes address space '__iomem' of expression

vim +/__iomem +78 drivers/mtd/maps/physmap-bt1-rom.c

    57	
    58	static void __xipram bt1_rom_map_copy_from(struct map_info *map,
    59						   void *to, unsigned long from,
    60						   ssize_t len)
    61	{
    62		void __iomem *src = map->virt + from;
    63		ssize_t shift, chunk;
    64		u32 data;
    65	
    66		if (len <= 0 || from >= map->size)
    67			return;
    68	
    69		/* Make sure we don't go over the map limit. */
    70		len = min_t(ssize_t, map->size - from, len);
    71	
    72		/*
    73		 * Since requested data size can be pretty big we have to implement
    74		 * the copy procedure as optimal as possible. That's why it's split
    75		 * up into the next three stages: unaligned head, aligned body,
    76		 * unaligned tail.
    77		 */
  > 78		shift = (ssize_t)src & 0x3;
    79		if (shift) {
    80			chunk = min_t(ssize_t, 4 - shift, len);
    81			data = readl_relaxed(src - shift);
    82			memcpy(to, &data + shift, chunk);
    83			src += chunk;
    84			to += chunk;
    85			len -= chunk;
    86		}
    87	
    88		while (len >= 4) {
    89			data = readl_relaxed(src);
    90			memcpy(to, &data, 4);
    91			src += 4;
    92			to += 4;
    93			len -= 4;
    94		}
    95	
    96		if (len) {
    97			data = readl_relaxed(src);
    98			memcpy(to, &data, len);
    99		}

100 }
101

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#1 lore raw

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

-Sergey

Quoted 76 lines · kernel test robot
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head:   3cea11cd5e3b00d91caf0b4730194039b45c5891
commit: b3e79e7682e075326df8041b826b03453acacd0a mtd: physmap: Add Baikal-T1 physically mapped ROM support
date:   4 weeks ago
config: sparc64-randconfig-s032-20201031 (attached as .config)
compiler: sparc64-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.3-76-gf680124b-dirty
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b3e79e7682e075326df8041b826b03453acacd0a
git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
git fetch --no-tags linus master
git checkout b3e79e7682e075326df8041b826b03453acacd0a
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=sparc64

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"

Quoted 1 line
Quoted 1 line

drivers/mtd/maps/physmap-bt1-rom.c:78:18: sparse: sparse: cast removes address space '__iomem' of expression

vim +/__iomem +78 drivers/mtd/maps/physmap-bt1-rom.c

    57	
    58	static void __xipram bt1_rom_map_copy_from(struct map_info *map,
    59						   void *to, unsigned long from,
    60						   ssize_t len)
    61	{
    62		void __iomem *src = map->virt + from;
    63		ssize_t shift, chunk;
    64		u32 data;
    65	
    66		if (len <= 0 || from >= map->size)
    67			return;
    68	
    69		/* Make sure we don't go over the map limit. */
    70		len = min_t(ssize_t, map->size - from, len);
    71	
    72		/*
    73		 * Since requested data size can be pretty big we have to implement
    74		 * the copy procedure as optimal as possible. That's why it's split
    75		 * up into the next three stages: unaligned head, aligned body,
    76		 * unaligned tail.
    77		 */
Quoted 1 line

78 shift = (ssize_t)src & 0x3;

    79		if (shift) {
    80			chunk = min_t(ssize_t, 4 - shift, len);
    81			data = readl_relaxed(src - shift);
    82			memcpy(to, &data + shift, chunk);
    83			src += chunk;
    84			to += chunk;
    85			len -= chunk;
    86		}
    87	
    88		while (len >= 4) {
    89			data = readl_relaxed(src);
    90			memcpy(to, &data, 4);
    91			src += 4;
    92			to += 4;
    93			len -= 4;
    94		}
    95	
    96		if (len) {
    97			data = readl_relaxed(src);
    98			memcpy(to, &data, len);
    99		}

100 }
101

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#2 lore raw

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Perhaps if you find an elegant wait to avoid the warning it would be nice, otherwise I guess we'll let it aside as a false positive.

Cheers,
Miquèl

#3 lore raw
Quoted 7 lines · Miquel Raynal

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 2 lines

Hello Miquel,

Quoted 7 lines
Quoted 4 lines

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:
+ shift = (ssize_t __iomem)src & 0x3;
be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

-Sergey

Quoted 6 lines

Perhaps if you find an elegant wait to avoid the warning it would be nice, otherwise I guess we'll let it aside as a false positive.

Cheers,
Miquèl

maintainer
#4 lore raw

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 19 lines · Miquel Raynal
Quoted 7 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 2 lines

Hello Miquel,

Quoted 7 lines
Quoted 4 lines

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;
Quoted 2 lines

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

Thanks,
Miquèl

#5 lore raw
Quoted 22 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 17 lines · Miquel Raynal
Quoted 15 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 7 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

Hm, that's weird. sparse gets happy if a casting to an unsigned type is used here. That's why the similar statement defined in bt1_rom_map_read() doesn't cause the sparse warning, while the statement with ssize_t does.

Can people around explain whether that is just an internal sparse feature or there is a particular reason of having the unsigned types casting ignored by sparse in this case? I don't really understand why removing the __iomem attribute with casting to a signed type cause the warning, while casting to an unsigned type doesn't...

Anyway I'll send a patch with the fix of using the uintptr_t casting here. Thanks for suggesting the solution.

-Sergey

Quoted 3 lines

Thanks,
Miquèl

#6 lore raw
In reply to Miquel Raynal
Quoted 21 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 16 lines · Miquel Raynal
Quoted 14 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 8 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

       shift = (__force ssize_t)src & 0x3;
Quoted 7 lines

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#7 lore raw

Hello Vignesh

Quoted 30 lines · Vignesh Raghavendra
Quoted 27 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 21 lines · Miquel Raynal
Quoted 7 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 2 lines

Hello Miquel,

Quoted 7 lines
Quoted 4 lines

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

Quoted 8 lines
Quoted 2 lines

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

       shift = (__force ssize_t)src & 0x3;

Oh, great! That solution is actually much better than using some currently unexplained sparse peculiarity! I was thinking about applying some other attribute, but __force just didn't come to my mind. Thank you very much for the suggestion. I'll post the fix with the solution suggested by you.

-Sergey

Quoted 9 lines
Quoted 9 lines

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

maintainer
#8 lore raw

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 18:27:39 +0300:

Quoted 40 lines

Hello Vignesh

Quoted 37 lines · Vignesh Raghavendra
Quoted 21 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 16 lines · Miquel Raynal
Quoted 14 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 8 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

Quoted 7 lines
Quoted 1 line
       shift = (__force ssize_t)src & 0x3;  

Oh, great! That solution is actually much better than using some currently unexplained sparse peculiarity! I was thinking about applying some other attribute, but __force just didn't come to my mind. Thank you very much for the suggestion. I'll post the fix with the solution suggested by you.

Is the ssize_t cast the right one btw? I would definitely prefer an unsigned type here.

Thanks,
Miquèl

#9 lore raw
Quoted 54 lines · Miquel Raynal

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 18:27:39 +0300:

Quoted 40 lines

Hello Vignesh

Quoted 37 lines · Vignesh Raghavendra
Quoted 21 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 16 lines · Miquel Raynal
Quoted 14 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 8 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

Quoted 7 lines
Quoted 1 line
       shift = (__force ssize_t)src & 0x3;  

Oh, great! That solution is actually much better than using some currently unexplained sparse peculiarity! I was thinking about applying some other attribute, but __force just didn't come to my mind. Thank you very much for the suggestion. I'll post the fix with the solution suggested by you.

Quoted 2 lines

Is the ssize_t cast the right one btw? I would definitely prefer an unsigned type here.

The reason of me deciding to use the ssize_t type here was to prevent the types casting across the "shift", "chunk" and "len" variables within this method. It seemed a bit better than having a standard type like "unsigned int" here seeing the ssize_t type width won't exceed the long type size anyway. Moreover since the "len" variable has got the ssize_t type and I couldn't change it (the method is the map_info callback), I've decided to stick with what is available and defined "shift" and "chunk" as ssize_t-es. Another callback method bt1_rom_map_read() in his module has been designed in the same way.

Do you think it's better to change it in favor of using a different type like "unsigned int" here anyway? If so for unification I'd need to change bt1_rom_map_read() (though the "shift" variable has been defined as "unsigned long" there in the first place because the offs argument has got that type).

What to do with the __force attribute here? It does seem appropriate even if for some mystical reasons we haven't got the sparse warning for the unsigned types.

-Sergey

Quoted 3 lines

Thanks,
Miquèl

maintainer
#10 lore raw

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 19:10:43 +0300:

Quoted 68 lines · Miquel Raynal
Quoted 54 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 18:27:39 +0300:

Quoted 40 lines

Hello Vignesh

Quoted 37 lines · Vignesh Raghavendra
Quoted 21 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 16 lines · Miquel Raynal
Quoted 14 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 8 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

Quoted 7 lines
Quoted 1 line
       shift = (__force ssize_t)src & 0x3;    

Oh, great! That solution is actually much better than using some currently unexplained sparse peculiarity! I was thinking about applying some other attribute, but __force just didn't come to my mind. Thank you very much for the suggestion. I'll post the fix with the solution suggested by you.

Quoted 2 lines

Is the ssize_t cast the right one btw? I would definitely prefer an unsigned type here.

The reason of me deciding to use the ssize_t type here was to prevent the types casting across the "shift", "chunk" and "len" variables within this method. It seemed a bit better than having a standard type like "unsigned int" here seeing the ssize_t type width won't exceed the long type size anyway. Moreover since the "len" variable has got the ssize_t type and I couldn't change it (the method is the map_info callback), I've decided to stick with what is available and defined "shift" and "chunk" as ssize_t-es. Another callback method bt1_rom_map_read() in his module has been designed in the same way.

Quoted 2 lines

Do you think it's better to change it in favor of using a different type like "unsigned int" here anyway?

I would say yes.

Quoted 4 lines

If so for unification I'd need to
change bt1_rom_map_read() (though the "shift" variable has been defined as "unsigned long" there in the first place because the offs argument has got that type).

Fine.

Quoted 4 lines

What to do with the __force attribute here? It does seem appropriate even if for some mystical reasons we haven't got the sparse warning for the unsigned types.

Yeah this is strange. I would, however, suggest not to add this keyword if we don't need it.

Thanks,
Miquèl

#11 lore raw
Quoted 87 lines · Miquel Raynal

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 19:10:43 +0300:

Quoted 68 lines · Miquel Raynal
Quoted 54 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Thu, 12 Nov 2020 18:27:39 +0300:

Quoted 40 lines

Hello Vignesh

Quoted 37 lines · Vignesh Raghavendra
Quoted 21 lines · Miquel Raynal

Hi Sergey,

Serge Semin <[email protected]> wrote on Wed, 11 Nov 2020 22:22:59 +0300:

Quoted 16 lines · Miquel Raynal
Quoted 14 lines

Hi Serge,

Serge Semin <[email protected]> wrote on Tue, 10 Nov 2020 14:38:27 +0300:

Quoted 6 lines

Hello Miquel,

A situation noted by the warning below won't cause any problem because the casting is done to a non-dereferenced variable. It is utilized as a pointer bias later in that function. Shall we just ignore the warning or still fix it somehow?

Do you think the cast to a !__iomem value is mandatory here?

Quoted 8 lines
Quoted 5 lines

It's not mandatory to have the casting with no __iomem, but wouldn't doing like this:

(quoted diff) +1 -0
+ 	shift = (ssize_t __iomem)src & 0x3;

be looking weird? Really, is there a good way to somehow extract the first two bits of a __iomem pointer without getting the sparse warning?

I asked around me, what about trying uintptr_t?

One more way is to use __force to tell sparse that this casting is intentional:

Quoted 7 lines
Quoted 1 line
       shift = (__force ssize_t)src & 0x3;    

Oh, great! That solution is actually much better than using some currently unexplained sparse peculiarity! I was thinking about applying some other attribute, but __force just didn't come to my mind. Thank you very much for the suggestion. I'll post the fix with the solution suggested by you.

Quoted 2 lines

Is the ssize_t cast the right one btw? I would definitely prefer an unsigned type here.

The reason of me deciding to use the ssize_t type here was to prevent the types casting across the "shift", "chunk" and "len" variables within this method. It seemed a bit better than having a standard type like "unsigned int" here seeing the ssize_t type width won't exceed the long type size anyway. Moreover since the "len" variable has got the ssize_t type and I couldn't change it (the method is the map_info callback), I've decided to stick with what is available and defined "shift" and "chunk" as ssize_t-es. Another callback method bt1_rom_map_read() in his module has been designed in the same way.

Quoted 2 lines

Do you think it's better to change it in favor of using a different type like "unsigned int" here anyway?

I would say yes.

Quoted 4 lines

If so for unification I'd need to
change bt1_rom_map_read() (though the "shift" variable has been defined as "unsigned long" there in the first place because the offs argument has got that type).

Fine.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

What to do with the __force attribute here? It does seem appropriate even if for some mystical reasons we haven't got the sparse warning for the unsigned types.

Yeah this is strange. I would, however, suggest not to add this keyword if we don't need it.

Ok. "unsigned int" it is then.

-Sergey

Quoted 3 lines

Thanks,
Miquèl