* [PATCH] nvmem: keep write protection disabled while writers are active
@ 2026-11-17 22:09 James Hilliard
2026-11-19 13:25 ` Bartosz Golaszewski
2026-11-23 22:56 ` Srinivas Kandagatla
0 siblings, 2 replies; 4+ messages in thread
From: James Hilliard @ 2026-11-17 22:09 UTC (permalink / raw)
To: Srinivas Kandagatla, Khouloud Touil, Bartosz Golaszewski,
Linus Walleij
Cc: linux-kernel, stable, James Hilliard
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@vger.kernel.org
Link: https://lore.kernel.org/r/sashiko-outbox-219526@kernel.org/
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
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);
+}
+
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
@@ -84,13 +114,13 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
if (!ops->reg_write)
return -EOPNOTSUPP;
- ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
+ ret = nvmem_wp_write_begin(nvmem);
if (ret)
return ret;
wr_ok = ops->reg_write(nvmem->priv, offset, val, bytes);
- ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
+ ret = nvmem_wp_write_end(nvmem);
if (ret)
return ret;
@@ -930,6 +960,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.bus = &nvmem_bus_type;
nvmem->dev.parent = config->dev;
INIT_LIST_HEAD(&nvmem->cells);
+ mutex_init(&nvmem->wp_lock);
/*
* Must happen before we assign the release() callback in
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5aefb..aab2ee4f9329 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -34,6 +34,8 @@ struct nvmem_device {
const struct nvmem_keepout *keepout;
unsigned int nkeepout;
struct gpio_desc *wp_gpio;
+ struct mutex wp_lock;
+ unsigned int wp_writers;
struct nvmem_layout *layout;
struct nvmem_operations __rcu *ops;
void *priv;
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git for-fixes
change-id: 20261117-submit-nvmem-wp-serialization-v1-e44b68058814
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] nvmem: keep write protection disabled while writers are active
2026-11-17 22:09 [PATCH] nvmem: keep write protection disabled while writers are active James Hilliard
@ 2026-11-19 13:25 ` Bartosz Golaszewski
2026-11-19 17:13 ` James Hilliard
2026-11-23 22:56 ` Srinivas Kandagatla
1 sibling, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2026-11-19 13:25 UTC (permalink / raw)
To: James Hilliard
Cc: Srinivas Kandagatla, Khouloud Touil, Linus Walleij, linux-kernel,
stable
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@vger.kernel.org
> Link: https://lore.kernel.org/r/sashiko-outbox-219526@kernel.org/
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvmem: keep write protection disabled while writers are active
2026-11-19 13:25 ` Bartosz Golaszewski
@ 2026-11-19 17:13 ` James Hilliard
0 siblings, 0 replies; 4+ messages in thread
From: James Hilliard @ 2026-11-19 17:13 UTC (permalink / raw)
To: Bartosz Golaszewski
Cc: Srinivas Kandagatla, Khouloud Touil, Linus Walleij, linux-kernel,
stable
On Thu, Nov 19, 2026 at 6:25 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> 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@vger.kernel.org
> > Link: https://lore.kernel.org/r/sashiko-outbox-219526@kernel.org/
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
>
> 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:
There's a race on the first write as well. The first writer can
increment the count from 0 to 1 and be preempted before it deasserts
WP. A second writer then sees a nonzero count, skips the GPIO call
and enters the provider's write callback while WP is still asserted.
>
> 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.
The GPIO driver can serialize both calls and still execute them in
the wrong order. If the last writer drops the count to zero and is
preempted, a new writer can deassert WP and start writing. The old
writer can then resume and assert WP in the middle of that write.
The mutex keeps the count change and the GPIO transition together.
It's released before calling the provider, so it doesn't serialize
the writes themselves.
>
> Bart
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvmem: keep write protection disabled while writers are active
2026-11-17 22:09 [PATCH] nvmem: keep write protection disabled while writers are active James Hilliard
2026-11-19 13:25 ` Bartosz Golaszewski
@ 2026-11-23 22:56 ` Srinivas Kandagatla
1 sibling, 0 replies; 4+ messages in thread
From: Srinivas Kandagatla @ 2026-11-23 22:56 UTC (permalink / raw)
To: James Hilliard, Srinivas Kandagatla, Khouloud Touil,
Bartosz Golaszewski, Linus Walleij
Cc: linux-kernel, stable
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@vger.kernel.org
> Link: https://lore.kernel.org/r/sashiko-outbox-219526@kernel.org/
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
> 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?
--srini
> +
> static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
> void *val, size_t bytes)
> {
> @@ -84,13 +114,13 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
> if (!ops->reg_write)
> return -EOPNOTSUPP;
>
> - ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
> + ret = nvmem_wp_write_begin(nvmem);
> if (ret)
> return ret;
>
> wr_ok = ops->reg_write(nvmem->priv, offset, val, bytes);
>
> - ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
> + ret = nvmem_wp_write_end(nvmem);
> if (ret)
> return ret;
>
> @@ -930,6 +960,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> nvmem->dev.bus = &nvmem_bus_type;
> nvmem->dev.parent = config->dev;
> INIT_LIST_HEAD(&nvmem->cells);
> + mutex_init(&nvmem->wp_lock);
>
> /*
> * Must happen before we assign the release() callback in
> diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
> index bc7a99f5aefb..aab2ee4f9329 100644
> --- a/drivers/nvmem/internals.h
> +++ b/drivers/nvmem/internals.h
> @@ -34,6 +34,8 @@ struct nvmem_device {
> const struct nvmem_keepout *keepout;
> unsigned int nkeepout;
> struct gpio_desc *wp_gpio;
> + struct mutex wp_lock;
> + unsigned int wp_writers;
> struct nvmem_layout *layout;
> struct nvmem_operations __rcu *ops;
> void *priv;
>
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git for-fixes
> change-id: 20261117-submit-nvmem-wp-serialization-v1-e44b68058814
>
> Best regards,
> --
> James Hilliard <james.hilliard1@gmail.com>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 22:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 22:09 [PATCH] nvmem: keep write protection disabled while writers are active James Hilliard
2026-11-19 13:25 ` Bartosz Golaszewski
2026-11-19 17:13 ` James Hilliard
2026-11-23 22:56 ` Srinivas Kandagatla
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox