From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796286387; b=xoPRQ8SpbBmejlIHodCruSfl4pnaSAd624cGLA6UQ17XkDplIgLjIk7EPbsEk6dVFXzX nGi0BYvaJ9PnIHnuH1PYmuSQmxQtlyQ4X8UJ5DlLsEpbipH16zJZm1bPPdWB/OE+LFRoP TMvAGjBJQ9Xfl6H9yP7r3wE8CKMEx/Jcn3eYfogBXCmhiw/NFHGiYJ4JVilrgAnTVYmCY rnhN8LzmljU7aBD7/n1Lbu1soZiFrggWrMa1a46foRmITImbMAIZpEqodnrfTY5TqZ8pQ I7Kvb4UDQTXGnIWPVhMVG6+M1y0o5iRq2Kms9NOUKAa6r+yFBKDuENFVqBbtewid//g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796286387; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=Uynm+798LFpp2CrX4SMpj8cudLKGZwF1UcT5ZKRCr68=; b=WExiRAV6uBnKv1KArBktbHaadUARDg99O18p0PBHxoc/cEWsK8U8n6oSz/ePaTzdKUO5 wcpAU0TgkY9sjZtdtQUpPhaYT8KeO4lZ115RPmM5meYdM7na2jmUOtWhrsiLTkDWyTY/0 r9Br6ydJ9RzbFAKvN+prO6KpZ2AhXGA5ui++f0cNQ48Zy3DGw0zb8yYYVuon0KVf70107 9U8GaJRvWL3WV2KRkL2HoTlVeLqefkiTTcNyDqPfZrDor15sVlDd+NKaPBr5NvK4+xdht 0fHtRqU3rW/CoC7q4J/eFOA685WQVZAYvWWFX4ALjBnt4Bomsz/9nkAo1fLnlLN2bKA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cy3pIo+8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B668C606A6B5; Thu, 3 Dec 2026 08:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cy3pIo+8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25CF51F00893; Thu, 3 Dec 2026 08:26:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796286387; bh=Uynm+798LFpp2CrX4SMpj8cudLKGZwF1UcT5ZKRCr68=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Cy3pIo+8TjVHwfzeL8CdEoAaZzXwUT+C7kfuv/hw6amHYCfl0JXj8xqitZ6PtcdE3 RLWPbunDM4szhfRWx3Abdj69fjwrrvDS1JUutdFgKEEuJRBX1YElVKEMWAYyLH1c6G mZQO1RRmnuPfelFQn9hxNFXVfqnp8SEhgcODWfVyxBeBRsDBkoKOSEWDy9V4cr8I0u /Ag5FAN+x+QmTEQc5qwneAXvXvREdkjRWi+QgSnTKM4Q2y4HtwHiPzajXjY65WL5x6 0McVBx+br3fooYIg4h5k7LLHpue67xtL+TghLLwwsK45nKtJ7AY4QpnGvkf8DtFSXz LfkUpl+bkOadw== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org, Bartosz Golaszewski , Greg Kroah-Hartman , Khouloud Touil , Linus Walleij , Maxime Ripard , Srinivas Kandagatla Subject: [PATCH 05/10] nvmem: core: Serialise writes when toggling the write-protect GPIO Date: Thu, 3 Dec 2026 08:26:06 +0000 Message-ID: X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Cc: Bartosz Golaszewski Cc: Greg Kroah-Hartman Cc: Khouloud Touil Cc: Linus Walleij Cc: Maxime Ripard Cc: Srinivas Kandagatla 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 +#include #include #include #include @@ -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