All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH trivial] kernel.h: Make container_of_safe type safe
@ 2019-01-24 20:55 Jason Gunthorpe
  2019-01-28 13:42 ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Gunthorpe @ 2019-01-24 20:55 UTC (permalink / raw)
  To: linux-kernel, NeilBrown, Jiri Kosina

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.

Fixes: 05e6557b8ed8 ("staging: lustre: add container_of_safe()")
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
---
 include/linux/kernel.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Jiri, can you take this? Thanks

Jason

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 8f0e68e250a760..7b85aa607eb876 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -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 */
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH trivial] kernel.h: Make container_of_safe type safe
  2019-01-24 20:55 [PATCH trivial] kernel.h: Make container_of_safe type safe Jason Gunthorpe
@ 2019-01-28 13:42 ` Christoph Hellwig
  2019-01-28 18:44   ` Jason Gunthorpe
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2019-01-28 13:42 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: linux-kernel, NeilBrown, Jiri Kosina

On Thu, Jan 24, 2019 at 08:55:05PM +0000, Jason Gunthorpe wrote:
> 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.

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH trivial] kernel.h: Make container_of_safe type safe
  2019-01-28 13:42 ` Christoph Hellwig
@ 2019-01-28 18:44   ` Jason Gunthorpe
  2019-01-29  7:40     ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Gunthorpe @ 2019-01-28 18:44 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: linux-kernel, NeilBrown, Jiri Kosina

On Mon, Jan 28, 2019 at 05:42:42AM -0800, Christoph Hellwig wrote:
> On Thu, Jan 24, 2019 at 08:55:05PM +0000, Jason Gunthorpe wrote:
> > 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.
> 
> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH trivial] kernel.h: Make container_of_safe type safe
  2019-01-28 18:44   ` Jason Gunthorpe
@ 2019-01-29  7:40     ` Christoph Hellwig
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2019-01-29  7:40 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: Christoph Hellwig, linux-kernel, NeilBrown, Jiri Kosina

On Mon, Jan 28, 2019 at 06:44:24PM +0000, Jason Gunthorpe wrote:
> On Mon, Jan 28, 2019 at 05:42:42AM -0800, Christoph Hellwig wrote:
> > On Thu, Jan 24, 2019 at 08:55:05PM +0000, Jason Gunthorpe wrote:
> > > 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.
> > 
> > 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..

And the patch is a good example for an anti-pattern to avoid, I'll
chime in there..

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-01-29  7:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-24 20:55 [PATCH trivial] kernel.h: Make container_of_safe type safe Jason Gunthorpe
2019-01-28 13:42 ` Christoph Hellwig
2019-01-28 18:44   ` Jason Gunthorpe
2019-01-29  7:40     ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.