Skip to contentSkip to conversation

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

PATCH Superseded Ian Abbott 2 participants 2 messages Oct 10, 2016 – Oct 17, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Abbott

author
#0 lore raw

PATCH 1/1 kernel.h: handle pointers to arrays better in container_of()

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.

Cc Andrew Morton <[email protected]>
Cc Michal Nazarewicz <[email protected]>
Cc Hidehiro Kawai <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Johannes Berg <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexander Potapenko <[email protected]>
---
 include/linux/kernel.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
include/linux/kernel.h +5 -6
index c64f998..5117b9f 100644
@@ -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 */
Signature · 1 line
2.9.3
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Ian Abbott

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.

Quoted 7 lines

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.

Cc Andrew Morton <[email protected]>
Cc Michal Nazarewicz <[email protected]>
Quoted 42 lines
Cc Hidehiro Kawai <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Rasmus Villemoes <[email protected]>
Cc Johannes Berg <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Alexander Potapenko <[email protected]>
---
 include/linux/kernel.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
include/linux/kernel.h +5 -6
index c64f998..5117b9f 100644
@@ -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 */
Signature · 1 line
2.9.3
Signature · 3 lines
Best regards
ミハウ “𝓶𝓲𝓷𝓪86” ナザレヴイツ
«If at first you don’t succeed, give up skydiving»