mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Khouloud Touil <ktouil@baylibre.com>,
	Linus Walleij <linusw@kernel.org>,
	Maxime Ripard <mripard@kernel.org>,
	Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 05/10] nvmem: core: Serialise writes when toggling the write-protect GPIO
Date: Thu,  3 Dec 2026 08:26:06 +0000	[thread overview]
Message-ID: <e5b42495cbacbac740476fa383a26f80eee7bac5.1796286324.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796286324.git.lee@kernel.org>

nvmem_access_chunk() deasserts nvmem->wp_gpio, calls ops->reg_write(),
and re-asserts nvmem->wp_gpio while callers hold only the shared SRCU
read lock.  Concurrent writers can therefore overlap, allowing one
writer to re-assert write protection while another writer is still
executing ops->reg_write().

Add a per-device wp_lock mutex to struct nvmem_device and hold it
across the WP GPIO deassert, ops->reg_write(), and WP GPIO re-assert
sequence whenever nvmem->wp_gpio is present.

Fixes: 2a127da461a9 ("nvmem: add support for the write-protect pin")
Closes: https://sashiko.dev/#/bug/linux-d7d845df-786a-40cf-b43c-f4ce2e295c82
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---

Cc: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Khouloud Touil <ktouil@baylibre.com>
Cc: Linus Walleij <linusw@kernel.org>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Srinivas Kandagatla <srini@kernel.org>

 drivers/nvmem/core.c      | 7 +++++++
 drivers/nvmem/internals.h | 2 ++
 2 files changed, 9 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index e7c687cb6ce6..2ba1ab037907 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -70,6 +70,11 @@ static int nvmem_access_chunk(struct nvmem_device *nvmem,
 	if (!ops->reg_write)
 		return -EOPNOTSUPP;
 
+	if (!nvmem->wp_gpio)
+		return ops->reg_write(nvmem->priv, offset, val, bytes);
+
+	guard(mutex)(&nvmem->wp_lock);
+
 	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
 	if (ret)
 		return ret;
@@ -574,6 +579,7 @@ static void nvmem_release(struct device *dev)
 	nvmem_device_remove_all_cells(nvmem);
 	ida_free(&nvmem_ida, nvmem->id);
 	of_node_put(nvmem->dev.of_node);
+	mutex_destroy(&nvmem->wp_lock);
 	cleanup_srcu_struct(&nvmem->srcu);
 	kfree(nvmem);
 }
@@ -983,6 +989,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		return ERR_PTR(rval);
 	}
 
+	mutex_init(&nvmem->wp_lock);
 	rcu_assign_pointer(nvmem->ops, ops);
 
 	device_initialize(&nvmem->dev);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5aefb..3d59ac799170 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -4,6 +4,7 @@
 #define _LINUX_NVMEM_INTERNALS_H
 
 #include <linux/device.h>
+#include <linux/mutex.h>
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 #include <linux/srcu.h>
@@ -34,6 +35,7 @@ struct nvmem_device {
 	const struct nvmem_keepout *keepout;
 	unsigned int		nkeepout;
 	struct gpio_desc	*wp_gpio;
+	struct mutex		wp_lock;	/* serialises WP GPIO writes */
 	struct nvmem_layout	*layout;
 	struct nvmem_operations __rcu *ops;
 	void *priv;
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03  8:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:26 [PATCH 00/10] nvmem: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-03  8:26 ` [PATCH 01/10] nvmem: meson-efuse: Reject negative or zero SMC size in meson_efuse_probe() Lee Jones
2026-12-03  8:26 ` [PATCH 02/10] nvmem: core: Prevent integer underflow in nvmem_cell_attr_read() Lee Jones
2026-12-03  8:26 ` [PATCH 03/10] nvmem: sprd: Avoid out-of-bounds read on partial writes in sprd_efuse_raw_prog() Lee Jones
2026-12-03  8:26 ` [PATCH 04/10] nvmem: core: Handle multi-byte bit offsets in nvmem_cell_prepare_write_buffer() Lee Jones
2026-12-03  8:26 ` Lee Jones [this message]
2026-12-03  8:26 ` [PATCH 06/10] nvmem: eeprom_93xx46: Reject unaligned writes on 16-bit devices Lee Jones
2026-12-03  8:26 ` [PATCH 07/10] nvmem: eeprom_93xx46: Hold device lock across erase/write enable sequences Lee Jones
2026-12-03  8:26 ` [PATCH 08/10] nvmem: eeprom_93xx46: Use DMA-safe command buffer for SPI transfers Lee Jones
2026-12-03  8:26 ` [PATCH 09/10] nvmem: meson-efuse: Detect short reads and writes from Secure Monitor Lee Jones
2026-12-03  8:26 ` [PATCH 10/10] nvmem: stm32: Avoid OP-TEE session leak on unaligned writes Lee Jones
2026-12-03 10:16   ` Patrick DELAUNAY

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=e5b42495cbacbac740476fa383a26f80eee7bac5.1796286324.git.lee@kernel.org \
    --to=lee@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=ktouil@baylibre.com \
    --cc=linusw@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mripard@kernel.org \
    --cc=srini@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®