From: Srinivas Kandagatla <srini@kernel.org>
To: James Hilliard <james.hilliard1@gmail.com>,
Srinivas Kandagatla <srini@kernel.org>,
Khouloud Touil <ktouil@baylibre.com>,
Bartosz Golaszewski <brgl@kernel.org>,
Linus Walleij <linusw@kernel.org>
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] nvmem: keep write protection disabled while writers are active
Date: Mon, 23 Nov 2026 22:56:54 +0000 [thread overview]
Message-ID: <8c0a616b-e7a5-4043-bc86-3bee5d0c9e48@kernel.org> (raw)
In-Reply-To: <20261117-submit-nvmem-wp-serialization-v1-v1-1-539d121f8f5e@gmail.com>
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>
>
next prev parent reply other threads:[~2026-11-23 22:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
2026-11-24 13:04 ` Bartosz Golaszewski
2026-11-24 16:48 ` James Hilliard
2026-11-25 16:01 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8c0a616b-e7a5-4043-bc86-3bee5d0c9e48@kernel.org \
--to=srini@kernel.org \
--cc=brgl@kernel.org \
--cc=james.hilliard1@gmail.com \
--cc=ktouil@baylibre.com \
--cc=linusw@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.