Re: [PATCH] nvmem: keep write protection disabled while writers are active
From: James Hilliard
Date: Tue Nov 24 2026 - 11:49:07 EST
On Tue, Nov 24, 2026 at 6:04 AM Bartosz Golaszewski <brgl@xxxxxxxxxx> wrote:
>
> 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.
Both NVMEM writes use the same WP descriptor. They look like one
consumer to gpio-shared-proxy, so it can't tell when the last write
through that descriptor has finished.
> 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.
The posted patch drops wp_lock before calling ops->reg_write(). The
mutex protects the writer count and the GPIO changes: the first writer
finishes deasserting WP before a second writer can enter, and only the
last writer reasserts it. The provider callbacks can still overlap.
One detail for a shared helper is how to handle a GPIO error on the
last user. regulator_ena_gpio_ctrl() keeps its enable count if disabling
the GPIO fails. In NVMEM the last provider write has already finished,
so we return the error if restoring WP fails, but there is no writer
left to count.
> 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?
If every writer starts with down_write(), a second writer still has to
wait while the first holds the downgraded read lock across its callback,
even though WP is already deasserted. Did you have a way for that
second writer to take the read side directly in mind?
>
> Bart