mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shuai Liu <847807867@qq.com>
To: linux-mtd@lists.infradead.org
Cc: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com,
	linux-kernel@vger.kernel.org, kevinshq@163.com
Subject: [PATCH] mtd: rawnand: samsung: fix pages_per_eraseblock calculation
Date: Fri, 23 Oct 2026 16:16:07 +0800	[thread overview]
Message-ID: <tencent_90082E9C18DB04917B9F74368BBAFC695306@qq.com> (raw)

In samsung_nand_decode_id(), the division by memorg->pagesize binds
tighter than the left shift due to C operator precedence, so the
expression is evaluated as:

    (128 * 1024) << (shift / pagesize)

The shift amount is at most 7 while pagesize is at least 2048, so the
integer division always yields 0 and pages_per_eraseblock ends up
being 131072 regardless of the actual chip geometry.

nanddev_init() later computes mtd->erasesize as
pagesize * pages_per_eraseblock, overwriting the correct value set in
this function with an astronomic value (e.g. 0x20000000, 512 MiB, for
a chip with 4096-byte pages).  MTD then rejects partitions not
starting on such an eraseblock boundary and forces them read-only,
breaking the rootfs on affected boards.

Add parentheses around the shift expression so that the eraseblock
size is computed before being divided by the page size.

Reported-by: kevinshq <kevinshq@163.com>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=222140
Fixes: 629a442cad5f ("mtd: rawnand: Fill memorg during detection")
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 drivers/mtd/nand/raw/nand_samsung.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand_samsung.c b/drivers/mtd/nand/raw/nand_samsung.c
index 0be6b7563..765c0a7d8 100644
--- a/drivers/mtd/nand/raw/nand_samsung.c
+++ b/drivers/mtd/nand/raw/nand_samsung.c
@@ -63,9 +63,9 @@ static void samsung_nand_decode_id(struct nand_chip *chip)
 
 		/* Get blocksize */
 		extid >>= 2;
-		memorg->pages_per_eraseblock = (128 * 1024) <<
-					       (((extid >> 1) & 0x04) |
-						(extid & 0x03)) /
+		memorg->pages_per_eraseblock = ((128 * 1024) <<
+						(((extid >> 1) & 0x04) |
+						 (extid & 0x03))) /
 					       memorg->pagesize;
 		mtd->erasesize = (128 * 1024) <<
 				 (((extid >> 1) & 0x04) | (extid & 0x03));
-- 
2.43.0


                 reply	other threads:[~2026-10-23  8:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=tencent_90082E9C18DB04917B9F74368BBAFC695306@qq.com \
    --to=847807867@qq.com \
    --cc=kevinshq@163.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®