[lkml]   [2017]   [May]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[RFC 1/1] linux/kernel.h: const-safe container_of() macro
Make container_of() macro const-safe by using _Generic selections.

It's easy to mistakenly pass a const ptr argument to container_of() and
use a non-const type for the container struct, which results in a
non-const reference to a struct that used to be const. This type of errors
are currently not flagged during compilation.

Another useful case for this is to provide a const-safe conversion between
different structs such as OF specific struct device_node and the more
generic struct fwnode_handle. const-ness of the argument is preserved in
the conversion and const warnings will be flagged by the compiler.

The "default" selection will match if the ptr argument is not const. The
"const typeof(*(ptr)) *" selection will match otherwise: constifying a
const type does not change it.

Signed-off-by: Sakari Ailus <>
Hi folks,

This patch currently triggers a large number of compiler warnings and not
necessarily all of the cases are at fault but nevertheless this suggests
that there could be issues.

I haven't checked most of the locations where the warnings are emitted and
rather wanted to ask about the approach.

Another option to support e.g. const-safe conversion could be to add a new
macro and leave the existing one as-is. This has the drawback of not
drawing attention to potential const-correctness issues.


include/linux/kernel.h | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 13bc08a..ddd4987 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -850,10 +850,20 @@ 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) ({ \
+#define ____container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

+#if GCC_VERSION >= 40900 /* GCC 4.9 added support for _Generic */
+#define container_of(ptr, type, member) \
+ _Generic((ptr), \
+ default: ____container_of(ptr, type, member), \
+ const typeof(*(ptr)) *: ____container_of(ptr, const type, \
+ member))
+#define container_of(ptr, type, member) ____container_of(ptr, type, member)
/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
 \ /
  Last update: 2017-05-19 14:08    [W:0.039 / U:1.448 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site