Re: [PATCH] nvmem: keep write protection disabled while writers are active

From: Bartosz Golaszewski

Date: Tue Nov 24 2026 - 08:04:44 EST


On Mon, Nov 23, 2026 at 10:56:54PM +0000, Srinivas Kandagatla wrote:
>
>
> On 11/17/26 10:09 PM, James Hilliard wrote:
> > The core deasserts the WP GPIO before calling the provider and asserts
> > it again on return. Two concurrent writes can both deassert WP before
> > the first one finishes. That writer then protects the device while the
> > other is still writing or waiting for the provider lock.
> >
> > Count active writers per device under a mutex. Only the first writer
> > deasserts WP and only the last one restores it, including on provider
> > errors. Keep the mutex outside the provider callback so providers that
> > forward writes to another NVMEM device can still nest.
> >
> > Fixes: 2a127da461a9 ("nvmem: add support for the write-protect pin")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Link: https://lore.kernel.org/r/sashiko-outbox-219526@xxxxxxxxxx/
> > Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
> > ---
> > drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++++++++--
> > drivers/nvmem/internals.h | 2 ++
> > 2 files changed, 35 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> > index 7d4cce8e2742..c0da902d909b 100644
> > --- a/drivers/nvmem/core.c
> > +++ b/drivers/nvmem/core.c
> > @@ -70,6 +70,36 @@ static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
> > return ops->reg_read(nvmem->priv, offset, val, bytes);
> > }
> >
> > +static int nvmem_wp_write_begin(struct nvmem_device *nvmem)
> > +{
> > + int ret;
> > +
> > + if (!nvmem->wp_gpio)
> > + return 0;
> > +
> > + guard(mutex)(&nvmem->wp_lock);
> > + if (!nvmem->wp_writers) {
> > + ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
> > + if (ret)
> > + return ret;
> > + }
> > + nvmem->wp_writers++;
> > +
> > + return 0;
> > +}
> > +
> > +static int nvmem_wp_write_end(struct nvmem_device *nvmem)
> > +{
> > + if (!nvmem->wp_gpio)
> > + return 0;
> > +
> > + guard(mutex)(&nvmem->wp_lock);
> > + if (--nvmem->wp_writers)
> > + return 0;
> > +
> > + return gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
> > +}
>
> This refcouning belong to more generic framwork, I see few more drivers
> doing pretty much the same.
> @Bart has this been discussed before?
>

Well, we have a use-counted gpio-shared-proxy, but this addresses real
hardware components sharing pins. This is a case of "logical sharing"
where you have a pin requested by the subsystem, which is then used by
multiple consumers. Normally, we do this at the subsystem level, like in
regulators - which BTW probably suffer from the same issue - but here
it's more visible and more likely to be hit.

We can either address it here or we can possibly introduce a use-count
helper for GPIO descriptors at the GPIOLIB level and re-use it in
regulator core. In that case, we'll still need serialization but I don't
like the idea of using a mutex and holding it over the entire operation.
How about a read-write semaphore, taking it for writing, checking if
we'll need to set value and keeping it for writing during
gpiod_set_value_cansleep() or - if not - calling downgrade_write() and
running the actual nvmem callback allowing for concurrent operations as
it is now?

Bart