Skip to contentSkip to conversation

kernel.h: fix new warnings for container_of()

PATCH In review Arnd Bergmann 3 participants 5 messages Jun 20, 2017 – Jun 21, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel.h: fix new warnings for container_of()

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

This seems to happen for all structures that have a zero offset between the member and the container structure, i.e. idential pointers.

Reverting to an intermediate pointer avoids the warning, and using a void pointer instead of the target type should also avoid regressing on the previous patch again.

Fixes mmotm ("kernel.h: handle pointers to arrays better in container_of()")
---
 include/linux/kernel.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kernel.h +2 -1
index d043adadcf33..bd6d96cf80b1 100644
@@ -856,10 +856,11 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  *
  */
 #define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
 			 !__same_type(*(ptr), void),			\
 			 "pointer type mismatch in container_of()");	\
-	((type *)((char *)(ptr) - offsetof(type, member))); })
+	((type *)(__mptr - offsetof(type, member))); })
 
 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
Signature · 1 line
2.9.0
#1 lore raw
Quoted 4 lines · Arnd Bergmann

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

This is actually from the randstruct plugin, not native gcc. I'll go update the message reporting to include a "randstruct: " which should make this more obvious.

Quoted 10 lines

This seems to happen for all structures that have a zero offset between the member and the container structure, i.e. idential pointers.

Reverting to an intermediate pointer avoids the warning, and using a void pointer instead of the target type should also avoid regressing on the previous patch again.

Fixes mmotm ("kernel.h: handle pointers to arrays better in container_of()")

Thanks!

I'll take this into the kspp tree so all the fixes are in the same place.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#2 lore raw
Quoted 18 lines · Kees Cook
Quoted 4 lines · Arnd Bergmann

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

This is actually from the randstruct plugin, not native gcc. I'll go update the message reporting to include a "randstruct: " which should make this more obvious.

Quoted 8 lines

This seems to happen for all structures that have a zero offset between the member and the container structure, i.e. idential pointers.

Reverting to an intermediate pointer avoids the warning, and using a void pointer instead of the target type should also avoid regressing on the previous patch again.

Quoted 6 lines
Quoted 2 lines
Fixes mmotm ("kernel.h: handle pointers to arrays better in container_of()")

Thanks!

I'll take this into the kspp tree so all the fixes are in the same place.

Oh, nevermind, I see the Fixes is for what's already in -mm. :) And I see akpm picked this up already. Sorry for the noise!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
Acked-by on v1 1/1
#3 lore raw
In reply to Arnd Bergmann
Quoted 4 lines · Arnd Bergmann

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types: 'struct f2fs_inode_info' and 'struct inode'

Is that actually a warning, or just informational? In any case, it seems like a good idea to avoid it.

Quoted 23 lines

This seems to happen for all structures that have a zero offset between the member and the container structure, i.e. idential pointers.

Reverting to an intermediate pointer avoids the warning, and using a void pointer instead of the target type should also avoid regressing on the previous patch again.

Fixes mmotm ("kernel.h: handle pointers to arrays better in container_of()")
---
 include/linux/kernel.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/kernel.h +0 -0
index d043adadcf33..bd6d96cf80b1 100644
@@ -856,10 +856,11 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  *
  */

include/linux/kernel.h static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } · line 866

Quoted 10 lines
include/linux/kernel.h +2 -1
 #define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
 			 !__same_type(*(ptr), void),			\
 			 "pointer type mismatch in container_of()");	\
-	((type *)((char *)(ptr) - offsetof(type, member))); })
+	((type *)(__mptr - offsetof(type, member))); })
 
 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]> )=-
-=(                          Web: http://www.mev.co.uk/  )=-
#4 lore raw
Quoted 11 lines · Ian Abbott
Quoted 6 lines · Arnd Bergmann

I see new warnings with gcc-7.0.1 with the modified container_of():

fs/f2fs/dir.c: In function 'F2FS_I':
fs/f2fs/f2fs.h:1122:385: note: found mismatched ssa struct pointer types:
'struct f2fs_inode_info' and 'struct inode'

Is that actually a warning, or just informational? In any case, it seems like a good idea to avoid it.

It's informational, not a warning. I couldn't actually figure out what the message is trying to tell me here. Maybe the message could also be improved?

Arnd