From: Linus Walleij <linus.walleij@linaro.org>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: "linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>,
Johan Hovold <johan@kernel.org>,
Alexandre Courbot <acourbot@nvidia.com>,
Arnd Bergmann <arnd@arndb.de>,
Michael Welling <mwelling@ieee.org>,
Markus Pargmann <mpa@pengutronix.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Mark Brown <broonie@kernel.org>,
Amit Kucheria <amit.kucheria@linaro.org>,
"arm@kernel.org" <arm@kernel.org>,
Haavard Skinnemoen <hskinnemoen@gmail.com>,
Sonic Zhang <sonic.zhang@analog.com>,
Greg Ungerer <gerg@uclinux.org>,
Ralf Baechle <ralf@linux-mips.org>,
Linux MIPS <linux-mips@linux-mips.org>,
Anatolij Gustschin <agust@denx.de>,
linux-wireless <linux-wireless@vger.kernel.org>,
Linux Input <linux-input@vger.kernel.org>,
Lee Jones <lee.jones@linaro.org>,
Russell King - ARM Linux <linux@arm.linux.org.uk>
Subject: Re: [PATCH 000/182] Rid struct gpio_chip from container_of() usage
Date: Mon, 14 Dec 2015 10:18:04 +0100 [thread overview]
Message-ID: <CACRpkdZDFPFOH=9FfYzLeKo5b-oXG4rPjoU4Rpq9Wv1-RvY6uQ@mail.gmail.com> (raw)
In-Reply-To: <20151209193034.GE27131@dtor-ws>
On Wed, Dec 9, 2015 at 8:30 PM, Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:
> On Wed, Dec 09, 2015 at 02:08:35PM +0100, Linus Walleij wrote:
>> This removes the use of container_of() constructions from *all*
>> GPIO drivers in the kernel. It is done by instead adding an
>> optional void *data pointer to the struct gpio_chip and an
>> accessor function, gpiochip_get_data() to get it from a driver.
>>
>> WHY?
>>
>> Because we want to have a proper userspace ABI for GPIO chips,
>> which involves using a character device that the user opens
>> and closes. While the character device is open, the underlying
>> kernel objects must not go away.
>>
>> Currently the GPIO drivers keep their state in the struct
>> gpio_chip, and that is often allocated by the drivers, very
>> often as a part of a containing per-instance state container
>> struct for the driver:
>>
>> struct foo_state {
>> struct gpio_chip chip; <- OMG my state is there
>> };
>>
>> Drivers cannot allocate and manage this state: if a user has the
>> character device open, the objects allocated must stay around
>> even if the driver goes away. Instead drivers need to pass a
>> descriptor to the GPIO core, and then the core should allocate
>> and manage the lifecycle of things related to the device, such
>> as the chardev itself or the struct device related to the GPIO
>> device.
>
> Yes, but it does not mean that the object that is being maintained by
> the subsystem and that us attached to character device needs to be
> gpio_chip itself. You can have something like
>
> struct gpio_chip_chardev {
> struct cdev chardev;
> struct gpio_chip *chip;
> bool dead;
> };
There needs to be a struct device too, amongst other things.
>
> struct gpio_chip {
> ...
> struct gpio_chip_chardev *chardev;
> ...
> };
>
> You alloctae the new structure when you register/export gpio chip in
> gpio subsystem core and leave all the individual drivers alone.
The current idea I have is something in the middle. Drivers have to
change a bit. The important part is that gpiolib handles allocation of
anything containing states. I'm thinking along the lines of Russell's
proposal to use netdev_alloc()'s design pattern.
The problem is that currently gpio_chip contains a lot of
stateful variables (like the dynamically allocated array of GPIO descriptors
etc) and those are used by the gpiolib core, so they have to be moved away
from gpio_chip.
So what happens if I don't change the design pattern:
int ret = gpiochip_add(&my_chip);
...
gpiochip_remove(&my_chip);
At this point we have to cross-reference the pointer to my chip to
find the chip to remove. This goes for anything that takes the struct
gpio_chip *
as parameter, like gpiochip_add_pin_range(), gpiochip_request_own_desc()
etc etc. So something inside gpiolib must take a gpio_chip * pointer and
turn that into the actual state container, e.g, a struct gpio_device.
Since struct gpio_chip needs to be static and stateless, it cannot contain
a pointer back to its struct gpio_device.
That means basically comparing pointers across a list of gpio_device's
to find it. And that's ... very kludgy. But if people think it's better to avoid
changing all drivers I will consider it.
I think it is better if the GPIO drivers get a handle on the
real gpio_device * to be used when calling these gpiochip_* related
functions and also in the callbacks, which is a bigger refactoring
indeed.
Part of this is trying to be elegant and mimic other subsystems and not
have GPIO be too hopelessly wayward about things.
If I compare to how struct input_dev is done, you appear to also use the
pattern Russell suggested with input_dev_allocate() akin to
netdev_alloc(), and the allocated struct holds all the vtable and states etc,
and I think it is a good pattern, and that GPIO should conform.
This current patch series however, just give us the equivalent of
input_get_drvdata()/input_set_drvdata() and that seems valuable on its
own, as it reduces code size and make things more readable.
Yours,
Linus Walleij
next prev parent reply other threads:[~2015-12-14 9:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-09 13:08 [PATCH 000/182] Rid struct gpio_chip from container_of() usage Linus Walleij
[not found] ` <1449666515-23343-1-git-send-email-linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-12-09 13:44 ` Russell King - ARM Linux
2015-12-09 14:46 ` Linus Walleij
2015-12-14 12:46 ` Johan Hovold
2015-12-18 14:42 ` Linus Walleij
2015-12-09 19:30 ` Dmitry Torokhov
2015-12-14 9:18 ` Linus Walleij [this message]
2015-12-15 7:25 ` Dmitry Torokhov
[not found] ` <CAKdAkRR6kEzEOtjxXqS4BPToEzKGBsLb=SzePFAiLSDhh+=qnw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-12-18 14:46 ` Linus Walleij
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CACRpkdZDFPFOH=9FfYzLeKo5b-oXG4rPjoU4Rpq9Wv1-RvY6uQ@mail.gmail.com' \
--to=linus.walleij@linaro.org \
--cc=acourbot@nvidia.com \
--cc=agust@denx.de \
--cc=amit.kucheria@linaro.org \
--cc=arm@kernel.org \
--cc=arnd@arndb.de \
--cc=broonie@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=gerg@uclinux.org \
--cc=gregkh@linuxfoundation.org \
--cc=hskinnemoen@gmail.com \
--cc=johan@kernel.org \
--cc=lee.jones@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-mips@linux-mips.org \
--cc=linux-wireless@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=mpa@pengutronix.de \
--cc=mwelling@ieee.org \
--cc=ralf@linux-mips.org \
--cc=sonic.zhang@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).