* [PATCH] kernel.h: handle pointers to arrays better in container_of()
@ 2016-10-10 20:16 Ian Abbott
2016-10-17 15:18 ` Michal Nazarewicz
0 siblings, 1 reply; 2+ messages in thread
From: Ian Abbott @ 2016-10-10 20:16 UTC (permalink / raw)
To: linux-kernel
Cc: Ian Abbott, Andrew Morton, Michal Nazarewicz, Hidehiro Kawai,
Borislav Petkov, Rasmus Villemoes, Johannes Berg, Peter Zijlstra,
Alexander Potapenko
If the first parameter of container_of() is a pointer to a
non-const-qualified array type, the local variable __mptr will be
defined with a const-qualified array type. In ISO C, these types are
incompatible. They work as expected in GNU C, but some versions will
issue warnings. For example, GCC 4.9 produces the warning
"initialization from incompatible pointer type". Fix it by avoiding
defining the __mptr variable. This also avoids other GCC extensions.
The container_of_safe() macro has the same problem. Here, we cannot
avoid all GCC extensions without eliminating possible side-effects, but
we can avoid having pointers to differently qualified array types.
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Michal Nazarewicz <mina86@mina86.com>
Cc: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Johannes Berg <johannes.berg@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Alexander Potapenko <glider@google.com>
---
include/linux/kernel.h | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c64f998..5117b9f 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -832,9 +832,8 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
* @member: the name of the member within the struct.
*
*/
-#define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
+#define container_of(ptr, type, member) \
+ ((type *)((char *)(ptr) - offsetof(type, member)))
/**
* container_of_safe - safe version of container_of
@@ -846,9 +845,9 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
* @type, return 0.
*/
#define container_of_safe(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (size_t)__mptr >= offsetof(type,member) ? \
- (type *)( (char *)__mptr - offsetof(type,member) ) : (type *)0 ;})
+ char *__mptr = (char *)(ptr); \
+ (size_t)__mptr >= offsetof(type, member) ? \
+ (type *)(__mptr - offsetof(type, member)) : (type *)0; })
/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
--
2.9.3
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] kernel.h: handle pointers to arrays better in container_of()
2016-10-10 20:16 [PATCH] kernel.h: handle pointers to arrays better in container_of() Ian Abbott
@ 2016-10-17 15:18 ` Michal Nazarewicz
0 siblings, 0 replies; 2+ messages in thread
From: Michal Nazarewicz @ 2016-10-17 15:18 UTC (permalink / raw)
To: Ian Abbott, linux-kernel
Cc: Ian Abbott, Andrew Morton, Hidehiro Kawai, Borislav Petkov,
Rasmus Villemoes, Johannes Berg, Peter Zijlstra,
Alexander Potapenko
On Mon, Oct 10 2016, Ian Abbott wrote:
> If the first parameter of container_of() is a pointer to a
> non-const-qualified array type, the local variable __mptr will be
> defined with a const-qualified array type. In ISO C, these types are
> incompatible. They work as expected in GNU C, but some versions will
> issue warnings. For example, GCC 4.9 produces the warning
> "initialization from incompatible pointer type". Fix it by avoiding
> defining the __mptr variable. This also avoids other GCC extensions.
Alternatively, maybe even better, just drop ‘const’. Avoiding GCC
extensions buys us anything especially since typeof is implemented by
clang as well and likely many more compilers.
> The container_of_safe() macro has the same problem. Here, we cannot
> avoid all GCC extensions without eliminating possible side-effects, but
> we can avoid having pointers to differently qualified array types.
>
> Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Michal Nazarewicz <mina86@mina86.com>
Acked-by: Michal Nazarewicz <mina86@mina86.com>
> Cc: Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>
> Cc: Borislav Petkov <bp@suse.de>
> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Cc: Johannes Berg <johannes.berg@intel.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Alexander Potapenko <glider@google.com>
> ---
> include/linux/kernel.h | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index c64f998..5117b9f 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -832,9 +832,8 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
> * @member: the name of the member within the struct.
> *
> */
> -#define container_of(ptr, type, member) ({ \
> - const typeof( ((type *)0)->member ) *__mptr = (ptr); \
> - (type *)( (char *)__mptr - offsetof(type,member) );})
> +#define container_of(ptr, type, member) \
> + ((type *)((char *)(ptr) - offsetof(type, member)))
>
> /**
> * container_of_safe - safe version of container_of
> @@ -846,9 +845,9 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
> * @type, return 0.
> */
> #define container_of_safe(ptr, type, member) ({ \
> - const typeof( ((type *)0)->member ) *__mptr = (ptr); \
> - (size_t)__mptr >= offsetof(type,member) ? \
> - (type *)( (char *)__mptr - offsetof(type,member) ) : (type *)0 ;})
> + char *__mptr = (char *)(ptr); \
> + (size_t)__mptr >= offsetof(type, member) ? \
> + (type *)(__mptr - offsetof(type, member)) : (type *)0; })
>
>
> /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
> --
> 2.9.3
>
--
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-10-17 15:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-10 20:16 [PATCH] kernel.h: handle pointers to arrays better in container_of() Ian Abbott
2016-10-17 15:18 ` Michal Nazarewicz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®