From: Srinivas Kandagatla <srini@kernel.org>
To: "Alexey Charkov" <alchark@flipper.net>,
"Srinivas Kandagatla" <srini@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Heiko Stuebner" <heiko@sntech.de>,
"Rafał Miłecki" <rafal@milecki.pl>,
"Michael Walle" <michael@walle.cc>,
"Miquel Raynal" <miquel.raynal@bootlin.com>,
"Finley Xiao" <finley.xiao@rock-chips.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: Ryan Leung <untilscour@protonmail.com>,
Ricardo Pardini <ricardo@pardini.net>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-rockchip@lists.infradead.org,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v3 2/5] nvmem: core: Let read post-processing return a shorter length
Date: Mon, 23 Nov 2026 21:56:29 +0000 [thread overview]
Message-ID: <0391e09d-3d44-4f91-8cad-dbfe8d4741d5@kernel.org> (raw)
In-Reply-To: <20261123-rk3576-otp-cpuid-mac-v3-2-40c756dcf36f@flipper.net>
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@flipper.net>
> ---
> 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);
>
next prev parent reply other threads:[~2026-11-23 21:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 15:35 [PATCH v3 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
2026-11-23 15:35 ` [PATCH v3 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip " Alexey Charkov
2026-11-23 15:35 ` [PATCH v3 2/5] nvmem: core: Let read post-processing return a shorter length Alexey Charkov
2026-11-23 21:56 ` Srinivas Kandagatla [this message]
2026-11-23 15:35 ` [PATCH v3 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID Alexey Charkov
2026-11-23 15:35 ` [PATCH v3 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout Alexey Charkov
2026-11-23 15:35 ` [PATCH v3 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576 Alexey Charkov
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=0391e09d-3d44-4f91-8cad-dbfe8d4741d5@kernel.org \
--to=srini@kernel.org \
--cc=alchark@flipper.net \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=finley.xiao@rock-chips.com \
--cc=gregkh@linuxfoundation.org \
--cc=heiko@sntech.de \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=michael@walle.cc \
--cc=miquel.raynal@bootlin.com \
--cc=rafal@milecki.pl \
--cc=ricardo@pardini.net \
--cc=robh@kernel.org \
--cc=untilscour@protonmail.com \
/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