Re: [PATCH] nvmem: keep write protection disabled while writers are active
From: Bartosz Golaszewski
Date: Wed Nov 25 2026 - 11:01:49 EST
On Tue, Nov 24, 2026 at 09:48:31AM -0700, James Hilliard wrote:
> On Tue, Nov 24, 2026 at 6:04 AM Bartosz Golaszewski <brgl@xxxxxxxxxx> wrote:
> >
...
> >
> > 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.
>
Sorry if I didn't make myself clear, I didn't mean we should use
gpio-shared-proxy here - I meant, the "logical sharing" doesn't mtch the goal
of gpio-shared-proxy.
> > 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.
>
Thanks for clarifying, I got confused by the usage of a non-scoped guard()
but it's in a dedicated function.
> 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.
>
Right. Maybe it is better to fix it locally for now.
> > 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?
>
No, you're right, we need to wait until setting the value is done.
Nevermind that comment.
With the above clarifications:
Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
Thanks,
Bartosz