All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Santhosh Kumar K" <s-k6@ti.com>
Cc: "Takahiro Kuwano" <takahiro.kuwano@infineon.com>,
	conor+dt@kernel.org, robh@kernel.org,
	"Michael Walle" <mwalle@kernel.org>,
	"Richard Weinberger" <richard@nod.at>,
	"Pratyush Yadav" <pratyush@kernel.org>,
	"Vignesh Raghavendra" <vigneshr@ti.com>,
	devicetree@vger.kernel.org,
	"Miquel Raynal" <miquel.raynal@bootlin.com>
Subject: Re: [PATCH v8 10/18] spi: cadence-quadspi: refactor direct read path for PHY support
Date: Fri, 21 Aug 2026 13:15:48 +0000	[thread overview]
Message-ID: <20260821131548.E15041F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260821114822.4040338-11-s-k6@ti.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] `cqspi_memcpy_fromio()` incorrectly byte-swaps the memory-mapped byte stream on big-endian architectures.
- [High] `cqspi_memcpy_fromio()` triggers fatal alignment faults on strict-alignment architectures if the source address is odd.
- [High] `cqspi_memcpy_fromio()` is vulnerable to integer underflow and buffer overflow if called with a count of 1.
--

--- Patch [10]: [PATCH v8 10/18] spi: cadence-quadspi: refactor direct read path for PHY support ---
commit 590a74a26c162a104820e05cc0e00a87a2e899bf
Author: Santhosh Kumar K <s-k6@ti.com>

spi: cadence-quadspi: refactor direct read path for PHY support

This patch extracts the DMA transfer code from cqspi_direct_read_execute()
into a new cqspi_direct_read_dma() helper. It also adds cqspi_memcpy_fromio()
to handle non-DMA transfers, using 2-byte-aligned I/O reads for 8D-8D-8D mode
to satisfy the minimum 16-bit AHB transaction width required by the Cadence
OSPI controller.

> diff --git a/drivers/spi/spi-cadence-quadspi.c b/drivers/spi/spi-cadence-quadspi.c
> --- a/drivers/spi/spi-cadence-quadspi.c
> +++ b/drivers/spi/spi-cadence-quadspi.c

[ ... ]

> +static void cqspi_memcpy_fromio(const struct spi_mem_op *op, void *to,
> +				const void __iomem *from, size_t count)
> +{
> +	if (op->data.buswidth == 8 && op->data.dtr) {
> +		unsigned long from_addr = (unsigned long)from;
> +
> +		/* Handle unaligned start with 2-byte read */
> +		if (count && !IS_ALIGNED(from_addr, 4)) {
> +			put_unaligned_le16(__raw_readw(from), to);
> +			from += 2;
> +			to += 2;
> +			count -= 2;
> +		}

[Severity: High]
If an unprivileged user initiates a 1-byte SPI read at an unaligned address
(e.g., offset 2), this block in cqspi_memcpy_fromio() executes when count
is 1.

Since it writes 2 bytes into the 1-byte destination buffer, doesn't this
cause a buffer overflow?

Also, count -= 2 will underflow the unsigned size_t count to SIZE_MAX,
leading to a massive out-of-bounds read/write loop in the while statement
below.

Could we ensure count >= 2 before performing a 2-byte read?

[Severity: High]
If from_addr is an odd address (1-byte aligned), __raw_readw(from) in
cqspi_memcpy_fromio() attempts an unaligned 16-bit MMIO read.

On strict-alignment architectures like ARM, wouldn't an unaligned 16-bit
MMIO read trigger a Data Abort (Alignment Fault)?

Furthermore, adding 2 to an odd address results in another odd address,
meaning the subsequent __raw_readl() loop will also fault.

> +
> +		/*
> +		 * Use an explicit 32-bit read loop for the bulk; this
> +		 * guarantees the minimum 16-bit AHB transaction width required
> +		 * in 8D-8D-8D mode on all architectures.
> +		 */
> +		while (count >= 4) {
> +			put_unaligned_le32(__raw_readl(from), to);

[Severity: High]
Does this combination of __raw_readl() and put_unaligned_le32() in
cqspi_memcpy_fromio() corrupt data on big-endian architectures?

On a big-endian CPU, __raw_readl() performs a native 32-bit load. For
example, reading the byte stream [A, B, C, D] results in the value
0xAABBCCDD.

Passing this native 32-bit value to put_unaligned_le32() stores it in
little-endian format, writing out [D, C, B, A] and reversing the bytes in
every 4-byte chunk.

Should this use stream I/O functions like ioread32_rep(), or pair native
loads with native stores to preserve the original stream order regardless
of CPU endianness?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260821114822.4040338-1-s-k6@ti.com?part=10

  reply	other threads:[~2026-08-21 13:15 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-21 11:48 [PATCH v8 00/18] spi: cadence-quadspi: add PHY tuning support Santhosh Kumar K
2026-08-21 11:48 ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 01/18] spi: dt-bindings: add spi-max-post-config-frequency-hz property Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 02/18] spi: dt-bindings: add spi-phy-pattern-partition property Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 03/18] spi: parse spi-max-post-config-frequency-hz into post_config_max_speed_hz Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 12:12   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 04/18] spi: spi-mem: teach spi_mem_adjust_op_freq() about post-config ops Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 12:23   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 05/18] spi: spi-mem: add execute_tuning callback and spi_mem_execute_tuning() Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 06/18] spi: cadence-quadspi: move cqspi_readdata_capture earlier Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 07/18] spi: cadence-quadspi: add DQS support to read data capture Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 08/18] spi: cadence-quadspi: add PHY tuning support Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 12:54   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 09/18] spi: cadence-quadspi: skip DDR PHY tuning for 2-byte-address ops (i2383) Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 10/18] spi: cadence-quadspi: refactor direct read path for PHY support Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 13:15   ` sashiko-bot [this message]
2026-08-21 11:48 ` [PATCH v8 11/18] spi: cadence-quadspi: enable PHY for direct reads Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 13:31   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 12/18] spi: cadence-quadspi: enable PHY for indirect writes Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 13:43   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 13/18] spi: cadence-quadspi: reprogram CS timing on every chip-select switch Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 14/18] spi: cadence-quadspi: reprogram PHY DLL on runtime resume Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 13:58   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 15/18] mtd: spinand: extract variant ranking logic into spinand_op_find_best_variant() Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 16/18] mtd: spinand: negotiate optimal controller operating point before dirmap creation Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 14:13   ` sashiko-bot
2026-08-21 11:48 ` [PATCH v8 17/18] mtd: spi-nor: extract read op template construction into helper Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-21 11:48 ` [PATCH v8 18/18] mtd: spi-nor: run controller optimization before dirmap creation Santhosh Kumar K
2026-08-21 11:48   ` Santhosh Kumar K
2026-08-25  5:04 ` [PATCH v8 00/18] spi: cadence-quadspi: add PHY tuning support Santhosh Kumar K
2026-08-25  5:04   ` Santhosh Kumar K

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=20260821131548.E15041F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=pratyush@kernel.org \
    --cc=richard@nod.at \
    --cc=robh@kernel.org \
    --cc=s-k6@ti.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=takahiro.kuwano@infineon.com \
    --cc=vigneshr@ti.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.