Re: [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses

From: Bartosz Golaszewski

Date: Thu Nov 19 2026 - 08:57:01 EST


On Tue, Nov 17, 2026 at 12:38:09PM -0700, James Hilliard wrote:
> The register callbacks are protected by SRCU, but keepout traversal and
> cell post-processing take place outside that protection. Unregister can
> finish draining a register read and return while its caller still uses
> a provider-owned keepout table or invokes a post-processing callback.
>
> Hold the SRCU read lock across the complete access, including keepout
> handling and post-processing. Reject accesses after the operations pointer
> has been cleared, including reads satisfied entirely by a keepout.
>
> Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
> Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
> ---
> drivers/nvmem/core.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 802637f375f8..73aa53145f0b 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -164,6 +164,10 @@ 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)
> {
> + guard(srcu)(&nvmem->srcu);
> + if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
> + return -ENODEV;
> +
> if (!nvmem->nkeepout)
> return __nvmem_reg_read(nvmem, offset, val, bytes);
>
> @@ -173,6 +177,10 @@ static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
> static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
> void *val, size_t bytes)
> {
> + guard(srcu)(&nvmem->srcu);
> + if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
> + return -ENODEV;
> +
> if (!nvmem->nkeepout)
> return __nvmem_reg_write(nvmem, offset, val, bytes);
>
> @@ -1651,6 +1659,10 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
> {
> int rc;
>
> + guard(srcu)(&nvmem->srcu);
> + if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
> + return -ENODEV;
> +
> rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
>
> if (rc)
>
> --
> 2.53.0
>

With that, you always double the SRCU critical section. I'd prefer to
see the existing SRCU guards in lower-level functions removed. If this
is not possible, then we may need to shuffle the code around and
introduce "unlocked" helpers that don't take the SRCU lock.

Bart