All of lore.kernel.org
 help / color / mirror / Atom feed
* [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; 7+ 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] 7+ 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; 7+ 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] 7+ 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; 7+ 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] 7+ 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
  2026-11-24 13:04   ` Bartosz Golaszewski
  1 sibling, 1 reply; 7+ 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] 7+ messages in thread

* Re: [PATCH] nvmem: keep write protection disabled while writers are active
  2026-11-23 22:56 ` Srinivas Kandagatla
@ 2026-11-24 13:04   ` Bartosz Golaszewski
  2026-11-24 16:48     ` James Hilliard
  0 siblings, 1 reply; 7+ messages in thread
From: Bartosz Golaszewski @ 2026-11-24 13:04 UTC (permalink / raw)
  To: Srinivas Kandagatla
  Cc: James Hilliard, Khouloud Touil, Linus Walleij, linux-kernel,
	stable

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@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?
> 

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvmem: keep write protection disabled while writers are active
  2026-11-24 13:04   ` Bartosz Golaszewski
@ 2026-11-24 16:48     ` James Hilliard
  2026-11-25 16:01       ` Bartosz Golaszewski
  0 siblings, 1 reply; 7+ messages in thread
From: James Hilliard @ 2026-11-24 16:48 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Srinivas Kandagatla, Khouloud Touil, Linus Walleij, linux-kernel,
	stable

On Tue, Nov 24, 2026 at 6:04 AM Bartosz Golaszewski <brgl@kernel.org> 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@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?
> >
>
> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvmem: keep write protection disabled while writers are active
  2026-11-24 16:48     ` James Hilliard
@ 2026-11-25 16:01       ` Bartosz Golaszewski
  0 siblings, 0 replies; 7+ messages in thread
From: Bartosz Golaszewski @ 2026-11-25 16:01 UTC (permalink / raw)
  To: James Hilliard
  Cc: Srinivas Kandagatla, Khouloud Touil, Linus Walleij, linux-kernel,
	stable

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@kernel.org> 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@oss.qualcomm.com>

Thanks,
Bartosz

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-25 16:01 UTC | newest]

Thread overview: 7+ 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
2026-11-24 13:04   ` Bartosz Golaszewski
2026-11-24 16:48     ` James Hilliard
2026-11-25 16:01       ` Bartosz Golaszewski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.