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