Re: [PATCH 3/3] gpio: use rcu_dereference_protected() to make lockdep happy
From: Bartosz Golaszewski
Date: Tue Feb 13 2024 - 07:07:14 EST
On Tue, Feb 13, 2024 at 1:03 PM Paul E. McKenney <paulmck@xxxxxxxxxx> wrote:
>
> On Tue, Feb 13, 2024 at 10:31:08AM +0100, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> >
> > Lockdep with CONFIG_PROVE_RCU enabled reports false positives about
> > suspicious rcu_dereference() usage. Let's silence it by using
> > rcu_dereference_protected().
> >
> > Fixes: d83cee3d2bb1 ("gpio: protect the pointer to gpio_chip in gpio_device with SRCU")
> > Reported-by: kernel test robot <oliver.sang@xxxxxxxxx>
> > Closes: https://lore.kernel.org/oe-lkp/202402122234.d85cca9b-lkp@xxxxxxxxx
> > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> > ---
> > drivers/gpio/gpiolib.c | 12 ++++++++----
> > drivers/gpio/gpiolib.h | 4 +++-
> > 2 files changed, 11 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> > index c1391a9a0af6..d7503376b918 100644
> > --- a/drivers/gpio/gpiolib.c
> > +++ b/drivers/gpio/gpiolib.c
> > @@ -109,7 +109,8 @@ const char *gpiod_get_label(struct gpio_desc *desc)
> > return "interrupt";
> >
> > return test_bit(FLAG_REQUESTED, &flags) ?
> > - rcu_dereference(desc->label) : NULL;
> > + rcu_dereference_protected(desc->label,
> > + lockdep_is_held(&desc->srcu)) : NULL;
>
> Why not this instead?
>
> > + srcu_dereference(desc->label, &desc->srcu) : NULL;
>
> And similarly for the rest of the changes.
>
Ah, I missed this one. Thanks, I'll use it.
For patch 1/3 in this series - should I stick with
rcu_access_pointer() or is it better to use srcu_dereference here as
well?
Bart
> Thanx, Paul
>
> > }
> >
> > static int desc_set_label(struct gpio_desc *desc, const char *label)
> > @@ -2978,7 +2979,8 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
> >
> > guard(srcu)(&gdev->srcu);
> >
> > - gc = rcu_dereference(gdev->chip);
> > + gc = rcu_dereference_protected(gdev->chip,
> > + lockdep_is_held(&gdev->srcu));
> > if (!gc)
> > return -ENODEV;
> >
> > @@ -3012,7 +3014,8 @@ static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc)
> > {
> > guard(srcu)(&gdev->srcu);
> >
> > - return gc == rcu_dereference(gdev->chip);
> > + return gc == rcu_dereference_protected(gdev->chip,
> > + lockdep_is_held(&gdev->srcu));
> > }
> >
> > int gpiod_get_array_value_complex(bool raw, bool can_sleep,
> > @@ -3593,7 +3596,8 @@ int gpiod_to_irq(const struct gpio_desc *desc)
> > gdev = desc->gdev;
> > /* FIXME Cannot use gpio_chip_guard due to const desc. */
> > guard(srcu)(&gdev->srcu);
> > - gc = rcu_dereference(gdev->chip);
> > + gc = rcu_dereference_protected(gdev->chip,
> > + lockdep_is_held(&gdev->srcu));
> > if (!gc)
> > return -ENODEV;
> >
> > diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
> > index 07443d26cbca..a857848b8955 100644
> > --- a/drivers/gpio/gpiolib.h
> > +++ b/drivers/gpio/gpiolib.h
> > @@ -14,6 +14,7 @@
> > #include <linux/err.h>
> > #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
> > #include <linux/gpio/driver.h>
> > +#include <linux/lockdep.h>
> > #include <linux/module.h>
> > #include <linux/notifier.h>
> > #include <linux/srcu.h>
> > @@ -202,7 +203,8 @@ DEFINE_CLASS(gpio_chip_guard,
> >
> > _guard.gdev = desc->gdev;
> > _guard.idx = srcu_read_lock(&_guard.gdev->srcu);
> > - _guard.gc = rcu_dereference(_guard.gdev->chip);
> > + _guard.gc = rcu_dereference_protected(_guard.gdev->chip,
> > + lockdep_is_held(&_guard.gdev->srcu));
> >
> > _guard;
> > }),
> > --
> > 2.40.1
> >