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

From: 1768315307

Date: Mon Sep 07 2026 - 23:41:49 EST


Hi Miquel,

Thanks for the review. Sent v2 with the fixes.

> why would you use mtdblock on top of a SPI NAND? It sounds like a very
> bad choice. Maybe mtdblock itself could be improved to avoid 512B
> accesses instead?

We use a read-only squashfs rootfs on mtdblock for SPI NAND. This is
a minimal configuration that avoids the complexity of UBI/UBIFS for
a read-only partition, and is commonly used in embedded devices like
routers and IoT systems.

The block layer's 512-byte sector size is not easily configurable per
device, and the driver already holds the full page in its bounce buffer
after each read. Adding a lightweight cache avoids the redundant SPI
reads without any additional hardware cost.

> Pseudonyms are not allowed in the Linux kernel.

Fixed in v2.

> Spurious change.

Fixed in v2.

Thanks,
Zongzhen Feng



原始邮件
发件人:Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
发件时间:2026年9月7日 20:33
收件人:fzz <1768315307@xxxxxx>
抄送:Vignesh Raghavendra <vigneshr@xxxxxx>, Richard Weinberger <richard@xxxxxx>, linux-mtd <linux-mtd@xxxxxxxxxxxxxxxxxxx>, linux-kernel <linux-kernel@xxxxxxxxxxxxxxx>
主题:Re: [PATCH] mtd: spinand: cache the last read page to avoid redundant SPI operations


Hello,

On 07/09/2026 at 19:10:40 +08, fzz <1768315307@xxxxxx> wrote:

> 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.

Why would you use mtdblock on top of a SPI NAND? It sounds like a very
bad choice.

Maybe mtdblock itself could be improved to avoid 512B accesses instead?

> Signed-off-by: fzz <1768315307@xxxxxx>

Unfortunately, for licensing and copyright reasons, pseudonyms are not
allowed in the Linux kernel.

> ---
>  drivers/mtd/nand/spi/core.c | 36 +++++++++++++++++++++++++++++++++++-
>  include/linux/mtd/spinand.h |  9 +++++++++
>  2 files changed, 44 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
> index 8bf9301f25e7..fc082127bda8 100644
> --- a/drivers/mtd/nand/spi/core.c
> +++ b/drivers/mtd/nand/spi/core.c
> @@ -557,7 +557,14 @@ static int spinand_read_from_cache_op(struct spinand_device *spinand,
>       req->ooblen);
>   else
>   memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
> -        req->ooblen);
> + req->ooblen);

Spurious change.

> + }
> +

Thanks,
Miquèl