Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Hilliard" <james.hilliard1@gmail.com>
Cc: conor+dt@kernel.org, robh@kernel.org,
	linux-sunxi@lists.linux.dev, devicetree@vger.kernel.org
Subject: Re: [PATCH v3 11/15] mtd: rawnand: sunxi: select the packed H6/H616 OOB layout
Date: Wed, 09 Sep 2026 08:51:38 +0000	[thread overview]
Message-ID: <20260909085138.9A98A1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260909-submit-sunxi-nand-vendor-oob-layout-v1-v3-11-838cb0ba1547@gmail.com>

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

Pre-existing issues:
- [High] The free OOB region incorrectly overlaps with the ECC OOB region for ECC steps > 0, causing silent ECC data corruption.
- [High] A division by zero kernel panic occurs when maximizing ECC strength on a NAND flash with 512-byte pages.
- [High] Unbounded accumulation of user data lengths leads to out-of-bounds MMIO writes, clobbering adjacent controller configuration registers.
--

commit 576ae0cb150295224ce149147695feb0bb49c1cf
Author: James Hilliard <james.hilliard1@gmail.com>

mtd: rawnand: sunxi: select the packed H6/H616 OOB layout

This patch adds a randomized OOB layout for the H6/H616 controller,
configuring a separate protected user-data length for ECC step zero to match
the firmware format. It keeps the maximized user-data layout when the
property is absent.

> diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
> index 138a141edbe4d..bd239d150730b 100644
> --- a/drivers/mtd/nand/raw/sunxi_nand.c
> +++ b/drivers/mtd/nand/raw/sunxi_nand.c
[ ... ]
> @@ -2365,6 +2374,12 @@ static int sunxi_nfc_maximize_user_data(struct nand_chip *nand, uint32_t oobsize
>  	if (!sunxi_nand->user_data_bytes)
>  		return -ENOMEM;
>  
> +	if (sunxi_nand->randomized_oob) {
> +		sunxi_nand->user_data_bytes[0] =
> +			sunxi_nfc_h6_user_data_sz(nsectors);
> +		return 0;
> +	}
> +
>  	for (step = 0; (step < nsectors) && (remaining_bytes > 0); step++) {
>  		for (i = 0; i < c->nuser_data_tab; i++) {
>  			if (c->user_data_len_tab[i] > remaining_bytes)
>  				break;
>  			sunxi_nand->user_data_bytes[step] = c->user_data_len_tab[i];
>  		}

[Severity: High]
This is a pre-existing issue, but does this loop lack an aggregate bounds
check against the hardware MMIO limit?

It appears this greedily assigns up to 32 bytes of user data per ECC step.
For a large page NAND flash, such as an 8KB page with 8 steps, it could
allocate up to 256 bytes in total.

When this total is later written out in sunxi_nfc_hw_ecc_set_prot_oob_bytes()
via a writel loop:

sunxi_nand.c:sunxi_nfc_hw_ecc_set_prot_oob_bytes() {
    ...
    for (i = 0; i < user_data_sz / 4; i++, ptr += 4) {
        writel(sunxi_nfc_buf_to_user_data(ptr),
               nfc->regs + NFC_REG_USER_DATA(nfc, reg_index + i));
    }
    ...
}

Will this step outside the available USER_DATA register bank and clobber
adjacent controller configuration registers like SPARE_AREA or PAT_ID,
potentially causing controller malfunction or a kernel panic during probe?

[ ... ]
> @@ -2422,6 +2437,10 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
>  				bytes -= 2;
>  
>  			bytes -= total_user_data_sz;
> +		} else if (sunxi_nand->randomized_oob) {
> +			total_user_data_sz =
> +				sunxi_nfc_h6_user_data_sz(nsectors);
> +			bytes -= total_user_data_sz;
>  		} else {

[Severity: High]
This isn't a bug introduced by this patch, but earlier in this function
when the NAND_ECC_MAXIMIZE_STRENGTH flag is set, the code hardcodes
ecc->size to 1024.

If this is run on a NAND device with a 512-byte page size, will nsectors
evaluate to 0? 

If nsectors is 0, the later operation to calculate the remaining ECC bytes
performs a division by zero:

sunxi_nand.c:sunxi_nand_hw_ecc_ctrl_init() {
    ...
    /*
     * Once all user data has been subtracted, the rest can be used
     * for ECC bytes
     */
    bytes /= nsectors;
    ...
}

Will this crash the kernel on boot for 512-byte page devices?

[ ... ]

[Severity: High]
This is also a pre-existing issue, but does sunxi_nand_ooblayout_free()
incorrectly overlap the free OOB region with the ECC OOB region?

Looking at sunxi_nand_ooblayout_free() for sections > 0:

sunxi_nand.c:sunxi_nand_ooblayout_free() {
    ...
    oobregion->offset = sunxi_get_ecc_offset(sunxi_nand, ecc, section);
    oobregion->length = user_data_sz;

    return 0;
}

Should this be using sunxi_get_oob_offset() instead? By setting the free
region offset to the ECC region offset, it seems that if a filesystem utilizing
OOB writes to the free OOB area, it will write directly into the ECC parity
bytes, silently destroying the parity data and causing uncorrectable read
errors.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260909-submit-sunxi-nand-vendor-oob-layout-v1-v3-0-838cb0ba1547@gmail.com?part=11

  reply	other threads:[~2026-09-09  8:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09  8:30 [PATCH v3 00/15] mtd: rawnand: sunxi: support the Allwinner randomized OOB format James Hilliard
2026-09-09  8:30 ` [PATCH v3 01/15] mtd: rawnand: sunxi: use the logical step's OOB length in PIO James Hilliard
2026-09-09  8:30 ` [PATCH v3 02/15] mtd: rawnand: sunxi: propagate page-setup and erased-check errors James Hilliard
2026-09-09  8:30 ` [PATCH v3 03/15] mtd: rawnand: sunxi: stop failed program operations and disable ECC James Hilliard
2026-09-09  8:30 ` [PATCH v3 04/15] mtd: rawnand: sunxi: select the pattern ID for the current ECC step James Hilliard
2026-09-09  8:30 ` [PATCH v3 05/15] mtd: rawnand: sunxi: propagate buffer and column transfer errors James Hilliard
2026-09-09  8:30 ` [PATCH v3 06/15] mtd: rawnand: sunxi: avoid a second program confirm for OOB writes James Hilliard
2026-09-09  8:30 ` [PATCH v3 07/15] mtd: rawnand: sunxi: avoid redundant column changes for extra OOB James Hilliard
2026-09-09  8:30 ` [PATCH v3 08/15] mtd: rawnand: sunxi: clarify OOB register and step handling James Hilliard
2026-09-09  8:30 ` [PATCH v3 09/15] dt-bindings: mtd: sunxi: Add randomized OOB flag James Hilliard
2026-09-09  8:30 ` [PATCH v3 10/15] mtd: rawnand: sunxi: support randomized OOB formats James Hilliard
2026-09-09  8:44   ` sashiko-bot
2026-09-09  8:30 ` [PATCH v3 11/15] mtd: rawnand: sunxi: select the packed H6/H616 OOB layout James Hilliard
2026-09-09  8:51   ` sashiko-bot [this message]
2026-09-09  8:30 ` [PATCH v3 12/15] mtd: rawnand: sunxi: combine contiguous unprotected OOB reads James Hilliard
2026-09-09  8:30 ` [PATCH v3 13/15] mtd: rawnand: sunxi: avoid duplicate chip setup before page commands James Hilliard
2026-09-09  8:54   ` sashiko-bot
2026-09-09  8:30 ` [PATCH v3 14/15] mtd: rawnand: sunxi: reduce user-data length register accesses James Hilliard
2026-09-09  8:30 ` [PATCH v3 15/15] mtd: rawnand: sunxi: reuse ECC status within each DMA read James Hilliard

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=20260909085138.9A98A1F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=james.hilliard1@gmail.com \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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