linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Linus Walleij <linus.walleij@linaro.org>
Cc: Kent Gibson <warthog618@gmail.com>, Alex Elder <elder@linaro.org>,
	 Geert Uytterhoeven <geert+renesas@glider.be>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	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 10/22] gpio: reinforce desc->flags handling
Date: Thu, 1 Feb 2024 19:30:51 +0100	[thread overview]
Message-ID: <CAMRc=MeF0QLUEcTLsV4eWonYpok7FCG1oGXLRetTBoja88uPxg@mail.gmail.com> (raw)
In-Reply-To: <CACRpkdZ9M=SapefrMX24=H5xGG91FNMN5TS63n3GdpegS_JAZQ@mail.gmail.com>

On Wed, Jan 31, 2024 at 9:35 PM Linus Walleij <linus.walleij@linaro.org> wrote:
>
> On Wed, Jan 31, 2024 at 9:01 PM Linus Walleij <linus.walleij@linaro.org> wrote:
> > On Tue, Jan 30, 2024 at 1:48 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> >
> > > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > >
> > > We now removed the gpio_lock spinlock and modified the places
> > > previously protected by it to handle desc->flags access in a consistent
> > > way. Let's improve other places that were previously unprotected by
> > > reading the flags field of gpio_desc once and using the stored value for
> > > logic consistency. If we need to modify the field, let's also write it
> > > back once with a consistent value resulting from the function's logic.
> > >
> > > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > (...)
> >
> > I have a trouble with this one:
> >
> > gpiochip_find_base_unlocked()
> > > +       unsigned long flags;
> > (...)
> > > +       flags = READ_ONCE(desc->flags);
> > (...)
> > > +       if (test_bit(FLAG_OPEN_DRAIN, &flags) &&
> > > +           test_bit(FLAG_IS_OUT, &flags))
> > >                 return 0;
> > (...)
> > > +       assign_bit(FLAG_IS_OUT, &flags, !ret);
> > > +       WRITE_ONCE(desc->flags, flags);
> >
> > I unerstand the atomicity of each operation here, but ... if what you want
> > to protect is modifications from other CPUs, how do we know that another
> > CPU isn't coming in and reading and modifying and assigning
> > another flag inbetween these operations while the value is only
> > stored in the CPU-local flags variable?
> >
> > Same with gpiod_direction_output().
> >
> > To me it seems like maybe you need to actually protect the desc->flags
> > with the SRCU struct in these cases? (and not only use it for the
> > label protection then).
> >
> > An alternative is maybe to rewrite the code with test_and_set().
> >
> > But as you say it is currently unprotected, I just wonder if this really
> > adds any protection.
>
> After re-reading the cover letter I'm fine with this, but I still wonder
> if it buys us anything.
>

This was a tough one...

I don't really see any way around it. SRCU is for pointers but even
then - we wouldn't get with SRCU anything more than what we're getting
with atomic reads and writes. As neither sleeping nor atomic locks
will work in the case of the GPIO subsystem, I figured that we should
strive for the maximum of coherence we can achieve - and for that I
figured that we should read the flags once, do our thing and then
write back a consistent result. If someone else comes around at the
same time and writes something else - well, he better be an
*exclusive*  user of that GPIO and know what they're doing. :)

Anyway, I think this series is already a big step forward and should
at least protect us from crashing. We can continue the work on
achieving full state consistency later.

Bart

> Maybe some words looped back from the
> commit message that we are not really protecting the callbacks
> because access is [predominantly] exclusive?
>
> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
>
> Yours,
> Linus Walleij

  reply	other threads:[~2024-02-01 18:31 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-30 12:48 [PATCH 00/22] gpio: rework locking and object life-time control Bartosz Golaszewski
2024-01-30 12:48 ` [PATCH 01/22] gpio: protect the list of GPIO devices with SRCU Bartosz Golaszewski
2024-01-31  9:34   ` kernel test robot
2024-01-31 12:45     ` Bartosz Golaszewski
2024-01-31 15:01   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 02/22] gpio: of: assign and read the hog pointer atomically Bartosz Golaszewski
2024-01-31 17:38   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 03/22] gpio: remove unused logging helpers Bartosz Golaszewski
2024-01-31 17:39   ` Linus Walleij
2024-01-31 18:08     ` Bartosz Golaszewski
2024-01-31 20:33       ` Linus Walleij
2024-01-30 12:48 ` [PATCH 04/22] gpio: provide and use gpiod_get_label() Bartosz Golaszewski
2024-01-31 19:36   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 05/22] gpio: don't set label from irq helpers Bartosz Golaszewski
2024-01-31 19:38   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 06/22] gpio: add SRCU infrastructure to struct gpio_desc Bartosz Golaszewski
2024-01-31 19:35   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 07/22] gpio: protect the descriptor label with SRCU Bartosz Golaszewski
2024-01-31 19:41   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 08/22] gpio: sysfs: use gpio_device_find() to iterate over existing devices Bartosz Golaszewski
2024-01-31 19:42   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 09/22] gpio: remove gpio_lock Bartosz Golaszewski
2024-01-31 19:51   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 10/22] gpio: reinforce desc->flags handling Bartosz Golaszewski
2024-01-31 20:01   ` Linus Walleij
2024-01-31 20:35     ` Linus Walleij
2024-02-01 18:30       ` Bartosz Golaszewski [this message]
2024-01-30 12:48 ` [PATCH 11/22] gpio: remove unneeded code from gpio_device_get_desc() Bartosz Golaszewski
2024-01-31 20:02   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 12/22] gpio: sysfs: extend the critical section for unregistering sysfs devices Bartosz Golaszewski
2024-01-31 20:06   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 13/22] gpio: sysfs: pass the GPIO device - not chip - to sysfs callbacks Bartosz Golaszewski
2024-01-31 20:09   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 14/22] gpio: cdev: replace gpiochip_get_desc() with gpio_device_get_desc() Bartosz Golaszewski
2024-01-31 20:10   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 15/22] gpio: cdev: don't access gdev->chip if it's not needed Bartosz Golaszewski
2024-01-31 20:11   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 16/22] gpio: reduce the functionality of validate_desc() Bartosz Golaszewski
2024-01-31 20:16   ` Linus Walleij
2024-01-31 20:19     ` Linus Walleij
2024-01-30 12:48 ` [PATCH 17/22] gpio: remove unnecessary checks from gpiod_to_chip() Bartosz Golaszewski
2024-01-30 12:48 ` [PATCH 18/22] gpio: add the can_sleep flag to struct gpio_device Bartosz Golaszewski
2024-01-31 20:17   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 19/22] gpio: add SRCU infrastructure " Bartosz Golaszewski
2024-01-30 12:48 ` [PATCH 20/22] gpio: protect the pointer to gpio_chip in gpio_device with SRCU Bartosz Golaszewski
2024-01-31  0:41   ` kernel test robot
2024-01-31  8:15     ` Bartosz Golaszewski
2024-01-31  2:20   ` kernel test robot
2024-01-31  9:02     ` Bartosz Golaszewski
2024-01-31  9:24       ` Paul E. McKenney
2024-01-31  9:28         ` Bartosz Golaszewski
2024-01-31  9:41           ` Bartosz Golaszewski
2024-01-31  9:42           ` Paul E. McKenney
2024-01-31 10:17             ` brgl
2024-01-31 11:00               ` Paul E. McKenney
2024-01-31 12:23                 ` Bartosz Golaszewski
2024-01-31 20:23   ` Linus Walleij
2024-02-01  5:03   ` Dan Carpenter
2024-02-01  7:57     ` Bartosz Golaszewski
2024-01-30 12:48 ` [PATCH 21/22] gpio: remove the RW semaphore from the GPIO device Bartosz Golaszewski
2024-01-31 20:24   ` Linus Walleij
2024-01-30 12:48 ` [PATCH 22/22] gpio: mark unsafe gpio_chip manipulators as deprecated Bartosz Golaszewski
2024-01-31 20:29   ` Linus Walleij
2024-02-01  9:14     ` Bartosz Golaszewski
2024-01-31 20:32 ` [PATCH 00/22] gpio: rework locking and object life-time control Linus Walleij
2024-02-01  8:43   ` Bartosz Golaszewski

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='CAMRc=MeF0QLUEcTLsV4eWonYpok7FCG1oGXLRetTBoja88uPxg@mail.gmail.com' \
    --to=brgl@bgdev.pl \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --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;
as well as URLs for NNTP newsgroup(s).