[PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column

From: James Hilliard

Date: Sun Dec 06 2026 - 04:59:20 EST


When the spare capacity of a MEMWRITE data chunk exceeds U32_MAX, the
32-bit multiplication in adjust_oob_length() wraps before the result
is compared with the requested OOB length. This can shorten the OOB
write even when all the requested spare bytes fit alongside the data.

Count the pages touched by the chunk from its starting column on the
backing device, and multiply by the per-page OOB capacity in u64. Use
the offsets of parent partitions to find that column. SLC emulation
instead addresses logical pages from the start of its partition.

A later patch uses this calculation for MEMREAD on read-only partitions
that start partway through a backing page.

Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/mtdchar.c | 9 ++++-----
drivers/mtd/mtdcore.h | 9 +++++++++
2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index cbc7114686b9..d385dd67f98d 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -576,18 +576,17 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
struct mtd_oob_ops *ops)
{
- uint32_t start_page, end_page;
+ u64 pages;
u32 oob_per_page;

if (ops->len == 0 || ops->ooblen == 0)
return;

- start_page = mtd_div_by_ws(start, mtd);
- end_page = mtd_div_by_ws(start + ops->len - 1, mtd);
+ pages = mtd_div_by_ws(mtd_oob_page_offset(mtd, start) + (u64)ops->len - 1,
+ mtd) + 1;
oob_per_page = mtd_oobavail(mtd, ops);

- ops->ooblen = min_t(size_t, ops->ooblen,
- (end_page - start_page + 1) * oob_per_page);
+ ops->ooblen = min_t(u64, ops->ooblen, pages * oob_per_page);
}

static noinline_for_stack int
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index b014861a06a6..3e65f85ec39a 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -14,6 +14,15 @@ int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
int del_mtd_partitions(struct mtd_info *);
void release_mtd_partition(struct mtd_info *mtd);

+static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
+{
+ /* SLC emulation walks write units relative to the partition. */
+ if (!(mtd->flags & MTD_SLC_ON_MLC_EMULATION))
+ addr = mtd_get_master_ofs(mtd, addr);
+
+ return mtd_mod_by_ws(addr, mtd);
+}
+
struct mtd_partitions;

int parse_mtd_partitions(struct mtd_info *master, const char * const *types,

--
2.53.0