From: srini@kernel.org
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
James Hilliard <james.hilliard1@gmail.com>,
stable@vger.kernel.org, Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 04/10] nvmem: drain keepout and cell post-processing accesses
Date: Tue, 1 Dec 2026 23:26:51 +0000 [thread overview]
Message-ID: <20261201232657.537736-5-srini@kernel.org> (raw)
In-Reply-To: <20261201232657.537736-1-srini@kernel.org>
From: James Hilliard <james.hilliard1@gmail.com>
A provider can unregister while a read is still walking its keepout table
or running a cell's post-processing callback. Waiting for the register
callback alone does not keep that provider state alive.
Take the SRCU read lock once for each register or cell access. Use the
same operations pointer for every chunk, including the reads needed for
a partial-byte write. Unregister then waits for the entire operation;
new accesses fail even if they would only touch a keepout.
Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
---
drivers/nvmem/core.c | 110 ++++++++++++++++++++++---------------------
1 file changed, 56 insertions(+), 54 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index be7c046b703d..81d787f7050a 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -54,32 +54,18 @@ static LIST_HEAD(nvmem_lookup_list);
static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
-static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
- void *val, size_t bytes)
+static int nvmem_access_chunk(struct nvmem_device *nvmem,
+ const struct nvmem_operations *ops,
+ unsigned int offset, void *val, size_t bytes, bool write)
{
- struct nvmem_operations *ops;
-
- guard(srcu)(&nvmem->srcu);
- ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
- if (!ops)
- return -ENODEV;
-
- if (!ops->reg_read)
- return -EOPNOTSUPP;
-
- return ops->reg_read(nvmem->priv, offset, val, bytes);
-}
-
-static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
- void *val, size_t bytes)
-{
- struct nvmem_operations *ops;
int ret, wr_ok;
- guard(srcu)(&nvmem->srcu);
- ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
- if (!ops)
- return -ENODEV;
+ if (!write) {
+ if (!ops->reg_read)
+ return -EOPNOTSUPP;
+
+ return ops->reg_read(nvmem->priv, offset, val, bytes);
+ }
if (!ops->reg_write)
return -EOPNOTSUPP;
@@ -97,17 +83,20 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
return wr_ok;
}
-static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
- unsigned int offset, void *val,
- size_t bytes, int write)
+static int nvmem_access_locked(struct nvmem_device *nvmem,
+ const struct nvmem_operations *ops,
+ unsigned int offset, void *val, size_t bytes, bool write)
{
-
unsigned int end = offset + bytes;
unsigned int kend, ksize;
const struct nvmem_keepout *keepout = nvmem->keepout;
- const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
+ const struct nvmem_keepout *keepoutend;
int rc;
+ if (!nvmem->nkeepout)
+ return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write);
+
+ keepoutend = keepout + nvmem->nkeepout;
/*
* Skip all keepouts before the range being accessed.
* Keepouts are sorted.
@@ -120,11 +109,7 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
if (offset < keepout->start) {
kend = min(end, keepout->start);
ksize = kend - offset;
- if (write)
- rc = __nvmem_reg_write(nvmem, offset, val, ksize);
- else
- rc = __nvmem_reg_read(nvmem, offset, val, ksize);
-
+ rc = nvmem_access_chunk(nvmem, ops, offset, val, ksize, write);
if (rc)
return rc;
@@ -150,13 +135,8 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
* If we ran out of keepouts but there's still stuff to do, send it
* down directly
*/
- if (offset < end) {
- ksize = end - offset;
- if (write)
- return __nvmem_reg_write(nvmem, offset, val, ksize);
- else
- return __nvmem_reg_read(nvmem, offset, val, ksize);
- }
+ if (offset < end)
+ return nvmem_access_chunk(nvmem, ops, offset, val, end - offset, write);
return 0;
}
@@ -164,19 +144,27 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- if (!nvmem->nkeepout)
- return __nvmem_reg_read(nvmem, offset, val, bytes);
+ const struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
- return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
+ return nvmem_access_locked(nvmem, ops, offset, val, bytes, false);
}
static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- if (!nvmem->nkeepout)
- return __nvmem_reg_write(nvmem, offset, val, bytes);
+ const struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
- return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
+ return nvmem_access_locked(nvmem, ops, offset, val, bytes, true);
}
#ifdef CONFIG_NVMEM_SYSFS
@@ -1650,10 +1638,15 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
struct nvmem_cell_entry *cell,
void *buf, size_t *len, const char *id, int index)
{
+ const struct nvmem_operations *ops;
int rc;
- rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->raw_len, false);
if (rc)
return rc;
@@ -1709,6 +1702,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
EXPORT_SYMBOL_GPL(nvmem_cell_read);
static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
+ const struct nvmem_operations *ops,
u8 *_buf, int len)
{
struct nvmem_device *nvmem = cell->nvmem;
@@ -1728,7 +1722,7 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
*b <<= bit_offset;
/* setup the first byte with lsb bits from nvmem */
- rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false);
if (rc)
goto err;
*b++ |= GENMASK(bit_offset - 1, 0) & v;
@@ -1747,8 +1741,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
/* if it's not end on byte boundary */
if ((nbits + bit_offset) % BITS_PER_BYTE) {
/* setup the last byte with msb bits from nvmem */
- rc = nvmem_reg_read(nvmem,
- cell->offset + cell->bytes - 1, &v, 1);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset + cell->bytes - 1,
+ &v, 1, false);
if (rc)
goto err;
*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
@@ -1764,10 +1758,18 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
{
struct nvmem_device *nvmem = cell->nvmem;
+ const struct nvmem_operations *ops;
int rc;
- if (!nvmem || nvmem->read_only ||
- (cell->bit_offset == 0 && len != cell->bytes))
+ if (!nvmem)
+ return -EINVAL;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
+
+ if (nvmem->read_only || (cell->bit_offset == 0 && len != cell->bytes))
return -EINVAL;
/*
@@ -1781,12 +1783,12 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
if (cell->bit_offset || cell->nbits) {
if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
return -EINVAL;
- buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
+ buf = nvmem_cell_prepare_write_buffer(cell, ops, buf, len);
if (IS_ERR(buf))
return PTR_ERR(buf);
}
- rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->bytes, true);
/* free the tmp buffer */
if (cell->bit_offset || cell->nbits)
--
2.53.0
next prev parent reply other threads:[~2026-12-01 23:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 23:26 [PATCH 00/10] nvmem: fixes for v7.4 srini
2026-12-01 23:26 ` [PATCH 01/10] nvmem: layouts: onie-tlv: check both field header and value bounds srini
2026-12-01 23:26 ` [PATCH 02/10] nvmem: core: validate DT cell bits before allocating the name srini
2026-12-01 23:26 ` [PATCH 03/10] nvmem: retain the provider node until device release srini
2026-12-01 23:26 ` srini [this message]
2026-12-01 23:26 ` [PATCH 05/10] nvmem: exclude disconnected providers from callback admission srini
2026-12-01 23:26 ` [PATCH 06/10] nvmem: release write protection when the provider disconnects srini
2026-12-01 23:26 ` [PATCH 07/10] nvmem: airoha-smc-efuses: Fix the SMC function ID for eFuse read srini
2026-12-01 23:26 ` [PATCH 08/10] nvmem: imx-ocotp-scu: assign nvmem_cell_info::raw_len srini
2026-12-01 23:26 ` [PATCH 09/10] nvmem: core: reject reads and writes beyond the device size srini
2026-12-01 23:26 ` [PATCH 10/10] nvmem: sprd: fix out-of-bounds read in sprd_efuse_read() srini
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=20261201232657.537736-5-srini@kernel.org \
--to=srini@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=james.hilliard1@gmail.com \
--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.