From: Ian Abbott <abbotti@mev.co.uk>
To: linux-kernel@vger.kernel.org
Cc: Ian Abbott <abbotti@mev.co.uk>,
Andrew Morton <akpm@linux-foundation.org>,
Michal Nazarewicz <mina86@mina86.com>,
Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>,
Borislav Petkov <bp@suse.de>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Johannes Berg <johannes.berg@intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Alexander Potapenko <glider@google.com>
Subject: [PATCH] kernel.h: handle pointers to arrays better in container_of()
Date: Mon, 10 Oct 2016 21:16:02 +0100 [thread overview]
Message-ID: <20161010201602.19412-1-abbotti@mev.co.uk> (raw)
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
next reply other threads:[~2016-10-10 20:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-10 20:16 Ian Abbott [this message]
2016-10-17 15:18 ` Michal Nazarewicz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161010201602.19412-1-abbotti@mev.co.uk \
--to=abbotti@mev.co.uk \
--cc=akpm@linux-foundation.org \
--cc=bp@suse.de \
--cc=glider@google.com \
--cc=hidehiro.kawai.ez@hitachi.com \
--cc=johannes.berg@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mina86@mina86.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®