Re: [PATCH v2] mtd: spinand: cache the last read page to avoid redundant SPI operations

From: Miquel Raynal

Date: Tue Sep 08 2026 - 06:32:47 EST


Hello Zongzhen,

On 08/09/2026 at 11:25:30 +08, fzz <1768315307@xxxxxx> wrote:

> From: Zongzhen Feng <1768315307@xxxxxx>
>
> When squashfs reads files through mtdblock, the mtdblock layer
> splits I/O into 512-byte sectors. For a 4K-page SPI NAND, this
> means reading the same page 8 times (4096 / 512), generating 7x
> redundant SPI read-from-cache operations. Each such operation
> involves a full SPI bus transaction, significantly slowing down
> boot time and file access.
>
> Cache the last successfully read page in spinand_device to avoid
> these redundant operations. When the same {target, eraseblock,
> page} is requested consecutively, the data is served directly
> from the bounce buffer (databuf) via memcpy, skipping the SPI
> transaction entirely.
>
> The cache is invalidated on write and erase operations. RAW reads,
> OOB reads, and continuous reads are excluded from caching to
> maintain correctness.
>
> Signed-off-by: Zongzhen Feng <1768315307@xxxxxx>

Sashiko reports interesting warnings, can you please address them?

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] The page caching mechanism uses the shared bounce buffer
`spinand->databuf` to store cached data, but fails to unconditionally
invalidate the cache when this buffer is overwritten by other
operations, leading to severe cross-page data corruption.
- [Critical] Pages with uncorrectable ECC errors are prematurely cached
as valid, causing subsequent reads to bypass ECC checks and silently
return corrupted data as successful.

Thanks,
Miquèl