[PATCH v2] mtd: mtdpart: use parent_size consistently in the bounds check

From: Fang Xieyan

Date: Fri Dec 04 2026 - 10:45:15 EST


allocate_partition() validates a partition against parent->size, but
the surrounding checks and the truncation itself work on parent_size
since commit 9e3307a16953: with SLC emulation, add_mtd_device()
shrinks mtd->size while part.size keeps the original value. This
leftover condition mixes the two size domains, can fire for a
sub-partition that fits, and its u64 addition can overflow.

Compare the size against parent_size - child->part.offset instead,
which the preceding offset check guarantees not to underflow. The
practical impact is limited; this is mainly a small fix to keep the
boundary handling consistent.

Link: https://lore.kernel.org/linux-mtd/20261203080547.15619-1-fangxy@xxxxxxxxxxxx/
Fixes: 9e3307a16953 ("mtd: Add support for emulated SLC mode on MLC NANDs")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
---

Base: v7.4-rc5 (fe6ff8d5d675); the condition is unchanged since
9e3307a16953.

Changes since v1:
- Reworked the commit message following Miquel's review: the primary
point is the mixed size domains left by 9e3307a16953, the overflow
is a secondary observation, impact wording downgraded.
- Fixes tag now points at 9e3307a16953 instead of the initial import.

Boundary analysis: the preceding check guarantees
child->part.offset < parent_size, so parent_size - child->part.offset
cannot underflow.

Testing: built and booted v7.4-rc5 with and without the patch on a
debug-only harness (fake MLC master, 256 KiB eraseblock, two pairing
groups); a sub-partition of an slc-mode parent partition keeps its
requested size with the fix in place and is resized by the truncation
path without it.

drivers/mtd/mtdpart.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index ddded0dbe77e..1b396f3bd928 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -143,7 +143,7 @@ static struct mtd_info *allocate_partition(struct mtd_info *parent,
part->name);
goto out_register;
}
- if (child->part.offset + child->part.size > parent->size) {
+ if (child->part.size > parent_size - child->part.offset) {
child->part.size = parent_size - child->part.offset;
printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
part->name, parent->name, child->part.size);
--
2.50.1