Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Varshini Rajendran" <varshini.rajendran@microchip.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v6 09/17] nvmem: microchip-otpc: nvmem: microchip-otpc: add tag-based packet lookup
Date: Thu, 06 Aug 2026 09:10:48 +0000	[thread overview]
Message-ID: <20260806091049.407441F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260806074024.531259-10-varshini.rajendran@microchip.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] TAG-based DT cells will be rejected by the nvmem core due to out-of-bounds offset values.

Pre-existing issues:
- [High] Concurrent accesses to nvmem_reg_read will corrupt OTP controller registers.
- [High] Out-of-bounds MMIO read during probe due to `size` vs `word_pos` miscalculation.
--

--- Patch [9]: [PATCH v6 09/17] nvmem: microchip-otpc: nvmem: microchip-otpc: add tag-based packet lookup ---
commit ffccf750925ce25af706c5e14106e91f61b012bc
Author: Varshini Rajendran <varshini.rajendran@microchip.com>

nvmem: microchip-otpc: nvmem: microchip-otpc: add tag-based packet lookup

Add support for accessing OTP packets by their tag which is a FourCC
while preserving backward compatibility with the existing ID-based lookup.
During probe, packet meta data including the tag is read and cached.

> diff --git a/drivers/nvmem/microchip-otpc.c b/drivers/nvmem/microchip-otpc.c
> index df979e8549fdb..a17f82afdc37d 100644
> --- a/drivers/nvmem/microchip-otpc.c
> +++ b/drivers/nvmem/microchip-otpc.c
> @@ -70,6 +74,52 @@ static struct mchp_otpc_packet *mchp_otpc_id_to_packet(struct mchp_otpc *otpc,

[ ... ]

> +static struct mchp_otpc_packet *mchp_otpc_resolve_packet(struct mchp_otpc *otpc, u32 off)
> +{
> +	/*
> +	 * Legacy id based packet access: offset = id * 4
> +	 * Inside the driver we use continuous unsigned integer numbers
> +	 * for packet id, thus divide off by 4 before passing it to
> +	 * mchp_otpc_id_to_packet().
> +	 */
> +	u32 remainder = off % 4;
> +	u32 id = off / 4;
> +
> +	if (!remainder && id < otpc->npackets)
> +		return mchp_otpc_id_to_packet(otpc, id);
> +
> +	/*
> +	 * TAG-based packet access: offset is a 4-byte ASCII tag (FourCC)
> +	 */
> +	return mchp_otpc_tag_to_packet(otpc, off);

[Severity: High]
Will TAG-based DT cells be rejected by the nvmem core due to out-of-bounds
offset values?

The nvmem core strictly validates DT cells in nvmem_add_cells_from_dt() using:
if (cell->offset >= nvmem->size || cell->bytes > nvmem->size - cell->offset)

Since the TAG value (e.g., 0x41435354) massively exceeds the physical size of
the OTP memory, will the core unconditionally reject the cell as out-of-bounds
and fail to register it?

> +}
> +

[ ... ]

> @@ -154,12 +225,11 @@ static int mchp_otpc_read(void *priv, unsigned int off, void *val,
>  	int ret, payload_size;
>  
>  	/*
> -	 * We reach this point with off being multiple of stride = 4 to
> -	 * be able to cross the subsystem. Inside the driver we use continuous
> -	 * unsigned integer numbers for packet id, thus divide off by 4
> -	 * before passing it to mchp_otpc_id_to_packet().
> +	 * From this point the offset has to be translated into the actual
> +	 * packet. For this we traverse the table of contents stored in a list
> +	 * "packet" based on the access type - packet id or tag.
>  	 */
> -	packet = mchp_otpc_id_to_packet(otpc, off / 4);
> +	packet = mchp_otpc_resolve_packet(otpc, off);
>  	if (!packet)
>  		return -EINVAL;
>  	offset = packet->offset;

[Severity: High]
This is a pre-existing issue, but does mchp_otpc_read() lack synchronization
when communicating with the hardware?

The nvmem core's sysfs binary file interface does not serialize reg_read
accesses across concurrent readers. When mchp_otpc_read() calls
mchp_otpc_prepare_read(), it performs a multi-register stateful sequence
(MCHP_OTPC_MR write, MCHP_OTPC_CR write, wait on MCHP_OTPC_SR) without holding
any lock.

Could concurrent reads interleave register writes and cause the hardware to
read from incorrect addresses or stall indefinitely?

[ ... ]

> @@ -215,6 +300,20 @@ static int mchp_otpc_init_packets_list(struct mchp_otpc *otpc, u32 *size)
>  
>  		packet->id = id++;
>  		packet->offset = word_pos;
> +		type = FIELD_GET(MCHP_OTPC_HR_PACKET, word);
> +
> +		if (type == MCHP_OTPC_HR_PACKET_REGULAR) {
> +			ret = mchp_otpc_read_packet_tag(otpc, packet->offset,
> +							&packet->tag);
> +			if (ret)
> +				return ret;
> +
> +			if (!mchp_otpc_is_valid_fourcc(packet->tag))
> +				packet->tag = 0;
> +		} else {
> +			packet->tag = 0;
> +		}
> +
>  		INIT_LIST_HEAD(&packet->list);
>  		list_add_tail(&packet->list, &otpc->packets);
>  

[Severity: High]
This isn't a bug introduced by this patch, but does the loop in
mchp_otpc_init_packets_list() perform an out-of-bounds MMIO read?

The loop condition is while (*size < MCHP_OTPC_SIZE). During each iteration,
*size advances by 4 * (payload_size + 1) bytes, but word_pos advances by
payload_size + 2 words (which is 4 * (payload_size + 2) bytes).

Because word_pos grows faster than *size, won't it exceed the physical
hardware bounds well before the loop terminates? If so, the subsequent call to
mchp_otpc_prepare_read(otpc, word_pos) could write this out-of-bounds offset
into the hardware address register.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260806074024.531259-1-varshini.rajendran@microchip.com?part=9

  reply	other threads:[~2026-08-06  9:10 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-06  7:40 [PATCH v6 00/17] Add thermal management support for sama7d65 Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 01/17] dt-bindings: iio: adc: at91-sama5d2: document sama7d65 Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 02/17] nvmem: add DEFINE_FREE for nvmem_cell_put cleanup Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 03/17] iio: adc: at91-sama5d2_adc: use cleanup.h for NVMEM buffer Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 04/17] iio: adc: at91-sama5d2_adc: rework temp calibration layout handling Varshini Rajendran
2026-08-06  9:07   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 05/17] iio: adc: at91-sama5d2_adc: add condition to validate calibration data Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 06/17] iio: adc: at91-sama5d2_adc: remove unnecessary casts in of_device_id Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 07/17] iio: adc: at91-sama5d2_adc: adapt the driver for sama7d65 Varshini Rajendran
2026-08-06  9:09   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 08/17] dt-bindings: nvmem: microchip,sama7g5-otpc: add sama7d65 and dt node example Varshini Rajendran
2026-08-06  9:07   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 09/17] nvmem: microchip-otpc: nvmem: microchip-otpc: add tag-based packet lookup Varshini Rajendran
2026-08-06  9:10   ` sashiko-bot [this message]
2026-08-06  7:40 ` [PATCH v6 10/17] nvmem: microchip-otpc: nvmem: add emulation mode and OTP access validation Varshini Rajendran
2026-08-06  9:08   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 11/17] ARM: dts: microchip: sama7d65: add cpu opps Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 12/17] ARM: dts: microchip: sama7d65: Add ADC node Varshini Rajendran
2026-08-06  9:12   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 13/17] ARM: dts: microchip: sama7d65_curiosity: Enable ADC, DVFS Varshini Rajendran
2026-08-06  7:40 ` [PATCH v6 14/17] ARM: dts: microchip: sama7d65: add otpc node Varshini Rajendran
2026-08-06  9:23   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 15/17] ARM: dts: microchip: sama7d65: add cells for temperature calibration Varshini Rajendran
2026-08-06  9:19   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 16/17] ARM: dts: microchip: sama7d65: add temperature sensor Varshini Rajendran
2026-08-06  9:18   ` sashiko-bot
2026-08-06  7:40 ` [PATCH v6 17/17] ARM: dts: microchip: sama7d65: add thermal zones node Varshini Rajendran

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=20260806091049.407441F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=varshini.rajendran@microchip.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