public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Kent Gibson <warthog618@gmail.com>, Alex Elder <elder@linaro.org>,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	Wolfram Sang <wsa@the-dreams.de>,
	linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH v3 00/24] gpio: rework locking and object life-time control
Date: Thu, 8 Feb 2024 19:43:08 +0200	[thread overview]
Message-ID: <ZcUSrCK_w06ZeV-W@smile.fi.intel.com> (raw)
In-Reply-To: <20240208095920.8035-1-brgl@bgdev.pl>

On Thu, Feb 08, 2024 at 10:58:56AM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> This is a big rework of locking in GPIOLIB. The current serialization is
> pretty much useless. There is one big spinlock (gpio_lock) that "protects"
> both the GPIO device list, GPIO descriptor access and who knows what else.
> 
> I'm putting "protects" in quotes as in several places the lock is
> taken, released whenever a sleeping function is called and re-taken
> without regards for the "protected" state that may have changed.
> 
> First a little background on what we're dealing with in GPIOLIB. We have
> consumer API functions that can be called from any context explicitly
> (get/set value, set direction) as well as many others which will get
> called in atomic context implicitly (e.g. set config called in certain
> situations from gpiod_direction_output()).
> 
> On the other side: we have GPIO provider drivers whose callbacks may or
> may not sleep depending on the underlying protocol.
> 
> This makes any attempts at serialization quite complex. We typically
> cannot use sleeping locks - we may be called from atomic - but we also
> often cannot use spinlocks - provider callbacks may sleep. Moreover: we
> have close ties with the interrupt and pinctrl subsystems, often either
> calling into them or getting called from them. They use their own locking
> schemes which are at odds with ours (pinctrl uses mutexes, the interrupt
> subsystem can call GPIO helpers with spinlock taken).
> 
> There is also another significant issue: the GPIO device object contains
> a pointer to gpio_chip which is the implementation of the GPIO provider.
> This object can be removed at any point - as GPIOLIB officially supports
> hotplugging with all the dynamic expanders that we provide drivers for -
> and leave the GPIO API callbacks with a suddenly NULL pointer. This is
> a problem that allowed user-space processes to easily crash the kernel
> until we patched it with a read-write semaphore in the user-space facing
> code (but the problem still exists for in-kernel users). This was
> recognized before as evidenced by the implementation of validate_desc()
> but without proper serialization, simple checking for a NULL pointer is
> pointless and we do need a generic solution for that issue as well.
> 
> If we want to get it right - the more lockless we go, the better. This is
> why SRCU seems to be the right candidate for the mechanism to use. In fact
> it's the only mechanism we can use our read-only critical sections to be
> called from atomic and protecc contexts as well as call driver callbacks
> that may sleep (for the latter case).
> 
> We're going to use it in three places: to protect the global list of GPIO
> devices, to ensure consistency when dereferencing the chip pointer in GPIO
> device struct and finally to ensure that users can access GPIO descriptors
> and always see a consistent state.
> 
> We do NOT serialize all API callbacks. This means that provider callbacks
> may be called simultaneously and GPIO drivers need to provide their own
> locking if needed. This is on purpose. First: we only support exclusive
> GPIO usage* so there's no risk of two drivers getting in each other's way
> over the same GPIO. Second: with this series, we ensure enough consistency
> to limit the chance of drivers or user-space users crashing the kernel.
> With additional improvements in handling the flags field in GPIO
> descriptors there's very little to gain, while bitbanging drivers may care
> about the increased performance of going lockless.
> 
> This series brings in one somewhat significant functional change for
> in-kernel users, namely: GPIO API calls, for which the underlying GPIO
> chip is gone, will no longer return 0 and emit a log message but instead
> will return -ENODEV.
> 
> I know this is a lot of code to go through but the more eyes we get on it
> the better.
> 
> Thanks,
> Bartosz
> 
> * - This is not technically true. We do provide the
> GPIOD_FLAGS_BIT_NONEXCLUSIVE flag. However this is just another piece of
> technical debt. This is a hack provided for a single use-case in the
> regulator framework which got out of control and is now used in many
> places that should have never touched it. It's utterly broken and doesn't
> even provide any contract as to what a "shared GPIO" is. I would argue
> that it's the next thing we should address by providing "reference counted
> GPIO enable", not just a flag allowing to request the same GPIO twice
> and then allow two drivers to fight over who toggles it as is the case
> now. For now, let's just treat users of GPIOD_FLAGS_BIT_NONEXCLUSIVE like
> they're consciously and deliberately choosing to risk undefined behavior.

LGTM, but I haven't done thorough review, hence, FWIW,
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>


-- 
With Best Regards,
Andy Shevchenko



  parent reply	other threads:[~2024-02-08 17:43 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20240213120518eucas1p2d514aac6e6d1e29bbae05f32db6724db@eucas1p2.samsung.com>
2024-02-08  9:58 ` [PATCH v3 00/24] gpio: rework locking and object life-time control Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 01/24] gpio: protect the list of GPIO devices with SRCU Bartosz Golaszewski
2024-02-10 11:00     ` Hillf Danton
2024-02-10 11:07       ` Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 02/24] gpio: of: assign and read the hog pointer atomically Bartosz Golaszewski
2024-02-08  9:58   ` [PATCH v3 03/24] gpio: remove unused logging helpers Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 04/24] gpio: provide and use gpiod_get_label() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 05/24] gpio: don't set label from irq helpers Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 06/24] gpio: add SRCU infrastructure to struct gpio_desc Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 07/24] gpio: protect the descriptor label with SRCU Bartosz Golaszewski
2024-02-12 14:56     ` kernel test robot
2024-02-13 21:16     ` Mark Brown
2024-02-13 22:07       ` Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 08/24] gpio: sysfs: use gpio_device_find() to iterate over existing devices Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 09/24] gpio: remove gpio_lock Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 10/24] gpio: reinforce desc->flags handling Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 11/24] gpio: remove unneeded code from gpio_device_get_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 12/24] gpio: sysfs: extend the critical section for unregistering sysfs devices Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 13/24] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 14/24] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 15/24] gpio: cdev: don't access gdev->chip if it's not needed Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 16/24] gpio: sysfs: " Bartosz Golaszewski
2024-02-08 12:20     ` Linus Walleij
2024-02-08  9:59   ` [PATCH v3 17/24] gpio: don't dereference gdev->chip in gpiochip_setup_dev() Bartosz Golaszewski
2024-02-08 12:21     ` Linus Walleij
2024-02-08  9:59   ` [PATCH v3 18/24] gpio: reduce the functionality of validate_desc() Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 19/24] gpio: remove unnecessary checks from gpiod_to_chip() Bartosz Golaszewski
2024-02-08 17:39     ` Andy Shevchenko
2024-02-08 19:17       ` Bartosz Golaszewski
2024-02-08 19:24         ` Andy Shevchenko
2024-02-08 19:34           ` Bartosz Golaszewski
2024-02-09 13:59             ` Andy Shevchenko
2024-02-08  9:59   ` [PATCH v3 20/24] gpio: add the can_sleep flag to struct gpio_device Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 21/24] gpio: add SRCU infrastructure " Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 22/24] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Bartosz Golaszewski
2024-02-12 15:09     ` kernel test robot
2024-02-12 16:56       ` Bartosz Golaszewski
2024-02-12 21:20       ` Bartosz Golaszewski
2024-02-13  8:10         ` Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 23/24] gpio: remove the RW semaphore from the GPIO device Bartosz Golaszewski
2024-02-10  5:37     ` Kent Gibson
2024-02-12  9:53       ` Bartosz Golaszewski
2024-02-12  9:57         ` Kent Gibson
2024-02-12  9:59           ` Bartosz Golaszewski
2024-02-08  9:59   ` [PATCH v3 24/24] gpio: mark unsafe gpio_chip manipulators as deprecated Bartosz Golaszewski
2024-02-08 17:43   ` Andy Shevchenko [this message]
2024-02-12 10:07   ` [PATCH v3 00/24] gpio: rework locking and object life-time control Bartosz Golaszewski
2024-02-13 12:05   ` Marek Szyprowski
2024-02-13 12:08     ` Bartosz Golaszewski
2024-04-19  7:03 Jiawen Wu
2024-04-19 13:27 ` Andy Shevchenko
2024-04-19 21:29 ` Bartosz Golaszewski
2024-04-22  8:38   ` Jiawen Wu

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=ZcUSrCK_w06ZeV-W@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=brgl@bgdev.pl \
    --cc=elder@linaro.org \
    --cc=geert+renesas@glider.be \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    --cc=warthog618@gmail.com \
    --cc=wsa@the-dreams.de \
    /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