Re: [PATCH 3/5] driver core: make struct device_type.uevent() take a const *
From: Barnabás Pőcze
Date: Wed Nov 23 2022 - 18:24:49 EST
Hi
2022. november 23., szerda 20:06 keltezéssel, Greg Kroah-Hartman írta:
> On Wed, Nov 23, 2022 at 02:25:59PM -0400, Jason Gunthorpe wrote:
>
> > On Wed, Nov 23, 2022 at 07:10:49PM +0100, Greg Kroah-Hartman wrote:
> >
> > > On Wed, Nov 23, 2022 at 05:49:36PM +0000, Matthew Wilcox wrote:
> > >
> > > > On Wed, Nov 23, 2022 at 01:29:56PM -0400, Jason Gunthorpe wrote:
> > > >
> > > > > #define generic_container_of(in_type, in, out_type, out_member) \
> > > > > _Generic(in, \
> > > > > const in_type *: ((const out_type *)container_of(in, out_type, out_member)), \
> > > > > in_type *: ((out_type *)container_of(in, out_type, out_member)) \
> > > > > )
> > > >
> > > > There's a neat trick I found in seqlock.h:
> > > >
> > > > #define generic_container_of(in_t, in, out_t, m) \
> > > > _Generic(*(in), \
> > > > const in_t: ((const out_t *)container_of(in, out_t, m)), \
> > > > in_t: ((out_t *)container_of(in, out_type, m)) \
> > > > )
> > > >
> > > > and now it fits in 80 columns ;-)
> > >
> > > Nice trick! Dropping the inline functions is a bit different, let me
> > > see if that still gives a sane error if we pass an incorrect type or
> > > mess with the const * the wrong way. I'll run some tests tomorrow
> > > afternoon...
> >
> > The errors in some cases are very verbose, but it is somewhat
> > understandable - the worst is when _Generic fails to match anything,
> > but also at least clang partially expanded container_of and it throws
> > other assertions too.
> >
> > I also wonder if this could just be rolled into the normal
> > container_of.
>
>
> I think we might be able to now, my previous attempts with inline
> functions prevented that. I'll beat on that tomorrow...
>
> > in_type would have to be derived like:
> >
> > in_type = typeof((out_type *)NULL)->out_member)
> >
> > But I don't know if you can use typeof in a generic type matching expression..
>
>
> Maybe that is what threw me before, I can't remember. I do know we
> tried a number of different attempts, can't recall the failed ones...
>
I am sorry I haven't followed the previous discussion,
but has something like the following been considered?
#define container_of2(ptr, type, member) \
_Generic((ptr), \
typeof_member(const type, member) *: container_of((ptr), const type, member), \
const void *: container_of((ptr), const type, member), \
default: container_of((ptr), type, member) \
)
This is not perfect by any means, but I believe it is a reasonable
improvement. It only really works reliably if the member type and
the specified `type` in the arguments are unqualified,
but I believe that should cover the majority of cases.
I have tried to build an x86-64 defconfig with this and there are
numerous const violations, but I haven't seen any false positives.
> [...]
Regards,
Barnabás Pőcze