kernel.h: Make container_of_safe type safe
PATCH 1/1 kernel.h: Make container_of_safe type safe
The final ternary expression is given void * and type * as arguments, so the resulting type of the macro ends up being void *.
container_of returns a type which maches the argument, this provides type safety and allows the expression to be immediately used as the target type.
Explicitly cast ERR_CAST() to get the correct type.
--- include/linux/kernel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Jiri, can you take this? Thanks
Jason
include/linux/kernel.h +1 -1
@@ -1013,7 +1013,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }BUILD_BUG_ON_MSG (!__same_type (*(ptr), ((type *)0 )->member) && \ !__same_type (*(ptr), void), \"pointer type mismatch in container_of()" ); \ -IS_ERR_OR_NULL (__mptr) ?ERR_CAST (__mptr) : \ +IS_ERR_OR_NULL (__mptr) ? ((type *)ERR_CAST (__mptr)) : \ ((type *)(__mptr - offsetof(type, member))); })/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
Signature · 1 line
2.20.1
Quoted 8 lines · Jason Gunthorpe
The final ternary expression is given void * and type * as arguments, so the resulting type of the macro ends up being void *.
container_ofreturns a type which maches the argument, this provides type safety and allows the expression to be immediately used as the target type.Explicitly cast
ERR_CAST()to get the correct type.
I think we should just kill off container_of_safe - it has no user in the tree, is grossly misnamed and not really all that useful to start with.
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Jason Gunthorpe
The final ternary expression is given void * and type * as arguments, so the resulting type of the macro ends up being void *.
container_ofreturns a type which maches the argument, this provides type safety and allows the expression to be immediately used as the target type.
Quoted 5 lines
Quoted 1 line
Explicitly cast
ERR_CAST()to get the correct type.I think we should just kill off
container_of_safe- it has no user in the tree, is grossly misnamed and not really all that useful to start with.
I see it open coded from time to time, here someone sent a patch open-coding it yesterday (with the same bug even):
https://patchwork.kernel.org/patch/10783535/
I noticed it didn't work right because I had a use for it..
Jason
Quoted 15 lines · Jason Gunthorpe
Quoted 8 lines · Christoph Hellwig
Quoted 7 lines · Jason Gunthorpe
The final ternary expression is given void * and type * as arguments, so the resulting type of the macro ends up being void *.
container_ofreturns a type which maches the argument, this provides type safety and allows the expression to be immediately used as the target type.Quoted 5 lines
Quoted 1 line
Explicitly cast
ERR_CAST()to get the correct type.I think we should just kill off
container_of_safe- it has no user in the tree, is grossly misnamed and not really all that useful to start with.
Quoted 6 lines
I see it open coded from time to time, here someone sent a patch open-coding it yesterday (with the same bug even):
https://patchwork.kernel.org/patch/10783535/
I noticed it didn't work right because I had a use for it..
And the patch is a good example for an anti-pattern to avoid, I'll chime in there..