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

From: Bartosz Golaszewski

Date: Thu Nov 19 2026 - 08:26:32 EST


On Tue, Nov 17, 2026 at 03:09:08PM -0700, 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>
> ---

This adds a mutex where there was previously none in nvmem core.
Synchronization was left to the underlying providers.

Can we not use atomic ops and avoid the mutex? I'm talking about
something like:

if (atomic_fetch_inc(nvmem->wp_cnt) == 0)
gpiod_set_value_cansleep(nvmem->wp_gpio, 0);

if (atomic_dec_return(nvmem->wp_cnt) == 0)
gpiod_set_value_cansleep(nvmem->wp_gpio, 1);

The only problem here is when we put the last reference, start setting
the value to 1 to enable WP, while another writer bumps it back up and
we drive WP pin back low. But then we expect the GPIO chip driver to
handle synchronization in this case.

Bart