nvmem: keep write protection disabled while writers are active
Subsystems (2)
PATCH 1/1 nvmem: keep write protection disabled while writers are active
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.
--- drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++++++++-- drivers/nvmem/internals.h | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +33 -2
@@ -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
drivers/nvmem/internals.h +2 -0
@@ -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,
Signature · 1 line
James Hilliard <[email protected]>
Quoted 10 lines · 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.
Quoted 5 lines
Fixes 2a127da461a9 ("nvmem: add support for the write-protect pin")Signed-off-by James Hilliard <[email protected]>---
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
Quoted 18 lines · Bartosz Golaszewski
Quoted 10 lines · 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.
Quoted 5 lines
Fixes 2a127da461a9 ("nvmem: add support for the write-protect pin")Signed-off-by James Hilliard <[email protected]>---
Quoted 5 lines
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.
Quoted 11 lines
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.
Quoted 2 lines
Bart
Srinivas Kandagatla
maintainerQuoted 46 lines · 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")Signed-off-by James Hilliard <[email protected]>--- drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++++++++-- drivers/nvmem/internals.h | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-)drivers/nvmem/core.c +19
-0@@ -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)
drivers/nvmem/core.c static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, · line 101
Quoted 10 lines
drivers/nvmem/core.c +10
-0+{ +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
Quoted 51 lines
drivers/nvmem/core.c +4
-2+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 drivers/nvmem/internals.h +2
-1@@ -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-e44b68058814Best regards,
Signature · 1 line
James Hilliard <[email protected]>
Quoted 55 lines · Srinivas Kandagatla
Quoted 52 lines · 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")Signed-off-by James Hilliard <[email protected]>--- drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++++++++-- drivers/nvmem/internals.h | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-)drivers/nvmem/core.c +25
-0@@ -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)
Quoted 9 lines
drivers/nvmem/core.c static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, · line 101
Quoted 4 lines
drivers/nvmem/core.c +4
-0+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
Quoted 67 lines · Bartosz Golaszewski
Quoted 55 lines · Srinivas Kandagatla
Quoted 52 lines · 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")Signed-off-by James Hilliard <[email protected]>--- drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++++++++-- drivers/nvmem/internals.h | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-)drivers/nvmem/core.c +25
-0@@ -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)Quoted 9 lines
drivers/nvmem/core.c static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset, · line 101
Quoted 4 lines
drivers/nvmem/core.c +4
-0+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?
Quoted 6 lines
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.
Quoted 4 lines
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.
Quoted 5 lines
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 - callingdowngrade_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?
Quoted 2 lines
Bart
Bartosz Golaszewski
Reviewed-byQuoted 2 lines · James Hilliard
Quoted 1 line · Bartosz Golaszewski
...
Quoted 8 lines
Quoted 7 lines
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.
Quoted 4 lines
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.
Quoted 10 lines
Quoted 4 lines
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_lockbefore callingops->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.
Quoted 6 lines
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.
Quoted 11 lines
Quoted 5 lines
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 - callingdowngrade_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:
Thanks,
Bartosz