Re: [PATCH v3 2/5] nvmem: core: Let read post-processing return a shorter length
From: Srinivas Kandagatla
Date: Mon Nov 23 2026 - 16:56:37 EST
On 11/23/26 3:35 PM, Alexey Charkov wrote:
> A cell can hold data from which a provider derives a shorter value for
> some of its consumers, while others still need the raw cell content. A
> CPU ID which serves as the base for MAC addresses is one example: the
> MAC address consumer needs six bytes, whereas anyone after the ID itself
> needs all of it. The post-processing hook has no way to express that,
> as the reported length is always the full cell size, and consumers such
> as of_get_mac_address() reject anything but the exact length they
> expect.
This is the correct behaviour.
>
nvmem cell fundamentally has cells that are fixed size during creation.
> Let the hook return the shortened length of its output instead of 0,
> and zero the buffer beyond it, so that a caller ignoring the reported
this looks like workaround.
This behavior should be inside the provider not in the core.
--srini
> length sees no leftovers of the raw cell. Reject a length beyond the
> cell size as a bug in the hook. Every existing hook returns 0 on
> success, so their behaviour does not change. Clamp the sysfs cell
> attribute read to the reported length, as it can now be shorter than
> the attribute size.
>
> Signed-off-by: Alexey Charkov <alchark@xxxxxxxxxxx>
> ---
> drivers/nvmem/core.c | 17 ++++++++++++++---
> include/linux/nvmem-provider.h | 8 +++++++-
> 2 files changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index d976c0d6603b..dde77f78e109 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -384,7 +384,8 @@ static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
> goto destroy_cell;
> }
>
> - read_len = min_t(unsigned int, cell_sz - pos, count);
> + /* Post-processing may have left less data than the attribute size */
> + read_len = pos < cell_sz ? min_t(size_t, cell_sz - pos, count) : 0;
> memcpy(buf, content + pos, read_len);
> kfree(content);
>
> @@ -1647,6 +1648,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
> struct nvmem_cell_entry *cell,
> void *buf, size_t *len, const char *id, int index)
> {
> + size_t out_len = cell->bytes;
> int rc;
>
> rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
> @@ -1661,12 +1663,21 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
> if (cell->read_post_process) {
> rc = cell->read_post_process(cell->priv, id, index,
> cell->offset, buf, cell->raw_len);
> - if (rc)
> + if (rc < 0)
> return rc;
> +
> + /* The post-processor may have shrunk the data: drop the rest */
> + if (rc > 0) {
> + if (WARN_ON_ONCE(rc > cell->bytes))
> + return -EINVAL;
> +
> + out_len = rc;
> + memset(buf + out_len, 0, cell->bytes - out_len);
> + }
> }
>
> if (len)
> - *len = cell->bytes;
> + *len = out_len;
>
> return 0;
> }
> diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
> index 6063fe5b7784..a95529aa3e80 100644
> --- a/include/linux/nvmem-provider.h
> +++ b/include/linux/nvmem-provider.h
> @@ -20,7 +20,13 @@ typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
> void *val, size_t bytes);
> typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
> void *val, size_t bytes);
> -/* used for vendor specific post processing of cell data */
> +/*
> + * Used for vendor specific post processing of cell data. Returns 0 if the
> + * data keeps the cell size (the cell's bytes, not the raw length passed in),
> + * the length of the processed data if it ends up shorter (1 up to the cell
> + * size), or a negative error code. The core zeroes the buffer beyond a
> + * shortened length. An empty result cannot be reported.
> + */
> typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
> unsigned int offset, void *buf,
> size_t bytes);
>