Re: [PATCH] linux/kernel.h: add container_from()

From: Allen
Date: Fri Aug 28 2020 - 03:07:26 EST


>
> I don't see that kind of redundancy being a _problem_, though. "So
> much redundancy" is just over-stating the issue completely.
>
> In fact, we often encourage people to split declaration from
> initialization exactly because it results in simpler expressions and
> more legible code, even if that name is now redundant. So it's a small
> extra typing of the type. Big deal.
>
> The above is also a common pattern that once you know how
> container_of() works, it's very legible.
>
> Sure, if you're new to the kernel, and haven't seen "container_of()"
> in other projects, it might initially be a bit of an odd pattern, but
> that's the advantage of having one single standardized model: it
> becomes a pattern, and you don't have to think about it.
>
> And particularly with that argument-type pattern, you really have to
> work at making over-long lines, since the indentation level will by
> definition be just one.
>
> Looking around, I do see a lot of people doing line-breaks, but that
> tends to be when they insist on putting the variable initialization in
> the declaration. And even then, it often seems pointless (eg
>
> struct idp_led *led = container_of(cdev,
> struct idp_led, cdev);
>
> was split for no good reason I can see, but it seems to be a pattern
> in that file).
>
> You really have to pick some pretty excessive type names (or variable
> names) to get close to 80 characters. Again, to pick an example:
>
> struct timer_group_priv *priv = container_of(handle,
> struct timer_group_priv, timer[handle->num]);
>
> ends up being long even if you were to split it, but that funky
> container_from() wouldn't have helped the real problem - the fact that
> the above is complex and nasty.
>

An example with a really long member name is

+struct nokia_modem_device *modem = from_tasklet(modem, t,
+ nokia_modem_rst_ind_tasklet);

With container_of() one can imagine how long it would end up. And
am sure we have many more examples in the kernel.

I agree, It would have been simpler to use container_of() as it's been
widely used, but as mentioned by Kees, for 1-to-many conversions
it does not work well.

I guess container_from() is a NAK, if you would want us to just use
container_of() to keep the code clean and simple instead of using wrappers,
or any other method, we are open to suggestions.

Thanks,
- Allen