* [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks
@ 2026-12-04 23:01 James Hilliard
2026-12-04 23:01 ` [PATCH v3 1/4] mtdchar: calculate OOB capacity from the starting column James Hilliard
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: James Hilliard @ 2026-12-04 23:01 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień
Cc: linux-mtd, linux-kernel, James Hilliard, stable
MEMREAD and MEMWRITE split requests into bounded data and OOB buffers.
Their chunk lengths and flash-address updates must keep both streams
associated with the same pages.
An intermediate read can consume OOB from pages beyond its data chunk.
OOB-only requests do not advance the flash address between chunks, while
a buffer ending within a page's OOB can leave the next operation starting
at the wrong page. When the OOB buffer is the limiting factor, the data
chunk must stop at the same page boundary.
The OOB-limited case was found while testing a proposed BootROM-format
MTD view with 1024 data bytes and 1152 OOB bytes per page. A 64-page
request has 65536 data bytes and 73728 OOB bytes, but the original
65536-byte buffer limit lets the first operation consume all the data
while transferring only part of the OOB.
First simplify the page-capacity calculation, then bound OOB in
intermediate reads to their data pages. Keep non-final OOB chunks
page-aligned and limit their accompanying data. Finish any remaining OOB
as OOB-only operations once the data is exhausted, including when the
last data fragment ends partway through a page. Advance by the pages
consumed by either stream and limit OOB-only writes to one page per call
for raw NAND.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v3:
- Rebase on mtd/next and preserve the per-chunk MEMREAD statistics
initialization in patch 4.
- Link to v2: https://patch.msgid.link/20261108-submit-mtdchar-page-chunking-v1-v2-0-838cf8934ba6@gmail.com
Changes in v2:
- Add a fourth patch to finish requests whose OOB extends past the data,
following Sashiko's review of patch 3.
- Switch to OOB-only operations once the data runs out, including when
the original non-NULL data buffer has zero remaining length.
- Advance beyond the final data page when OOB was consumed there, and
keep raw-NAND OOB-only writes to one page per backend operation.
- Link to v1: https://patch.msgid.link/20261106-submit-mtdchar-page-chunking-v1-v1-0-2973a03531bc@gmail.com
---
James Hilliard (4):
mtdchar: calculate OOB capacity from the starting column
mtdchar: bound OOB reads to the data in intermediate chunks
mtdchar: keep OOB chunks aligned with their data pages
mtdchar: finish OOB transfers after the data runs out
drivers/mtd/mtdchar.c | 82 +++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 67 insertions(+), 15 deletions(-)
---
base-commit: 4e5457fcbc14d21e7c004c3159e78fc63838029e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next
change-id: 20261106-submit-mtdchar-page-chunking-v1-668636b0becd
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/4] mtdchar: calculate OOB capacity from the starting column
2026-12-04 23:01 [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
@ 2026-12-04 23:01 ` James Hilliard
2026-12-04 23:01 ` [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks James Hilliard
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: James Hilliard @ 2026-12-04 23:01 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień
Cc: linux-mtd, linux-kernel, James Hilliard
adjust_oob_length() needs the number of pages covered by an operation,
not their absolute indices. Calculate that count from the initial page
column and the data length instead of subtracting absolute page numbers.
Use a u64 product for the resulting OOB capacity before limiting the
requested OOB length. This keeps the calculation independent of the
absolute flash offset and avoids narrowing the capacity before the
comparison.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index cbc7114686b9..87ff071afc97 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_mod_by_ws(start, mtd) + (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
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks
2026-12-04 23:01 [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-04 23:01 ` [PATCH v3 1/4] mtdchar: calculate OOB capacity from the starting column James Hilliard
@ 2026-12-04 23:01 ` James Hilliard
2026-12-04 23:23 ` sashiko-bot
2026-12-04 23:02 ` [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages James Hilliard
2026-12-04 23:02 ` [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out James Hilliard
3 siblings, 1 reply; 8+ messages in thread
From: James Hilliard @ 2026-12-04 23:01 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień
Cc: linux-mtd, linux-kernel, James Hilliard, stable
MEMREAD limits the data and OOB lengths independently for each call to
mtd_read_oob(). A backend such as SPI-NAND continues reading until both
streams are exhausted, so an intermediate call can read OOB from pages
beyond the end of its data chunk.
The next call advances by the data bytes transferred, not by the pages
visited for OOB. If OOB remains, it starts again at pages already read
and returns their OOB a second time.
Limit OOB in intermediate reads to the pages covered by their data,
as MEMWRITE already does. Leave the final call unchanged so SPI-NAND
can still return additional OOB pages when no data chunk follows.
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 87ff071afc97..25c59cc7f097 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -769,6 +769,9 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
if (ops.len == mtd->erasesize)
ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
+ if (ops.len < req.len)
+ adjust_oob_length(mtd, req.start, &ops);
+
ret = mtd_read_oob(mtd, (loff_t)req.start, &ops);
req.ecc_stats.uncorrectable_errors +=
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages
2026-12-04 23:01 [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-04 23:01 ` [PATCH v3 1/4] mtdchar: calculate OOB capacity from the starting column James Hilliard
2026-12-04 23:01 ` [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks James Hilliard
@ 2026-12-04 23:02 ` James Hilliard
2026-12-04 23:20 ` sashiko-bot
2026-12-04 23:02 ` [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out James Hilliard
3 siblings, 1 reply; 8+ messages in thread
From: James Hilliard @ 2026-12-04 23:02 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień
Cc: linux-mtd, linux-kernel, James Hilliard, stable
MEMREAD and MEMWRITE advance the flash address by the data bytes
transferred. OOB-only operations transfer no data, so another buffer-sized
chunk starts at the same address and repeats pages.
Advance OOB-only requests by the number of pages transferred. Round
non-final OOB chunks down to whole pages so the next call starts at the
next page, rather than repeating a partially consumed page. Allow the
buffer to hold at least one page of OOB and reject requests when the
selected mode has no OOB capacity.
When OOB is the limiting buffer, also cap the data chunk to those same
pages. Otherwise a combined request can exhaust its data before all OOB
has been transferred.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 53 +++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 47 insertions(+), 6 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 25c59cc7f097..454ec3ec0c98 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -589,6 +589,30 @@ static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
ops->ooblen = min_t(u64, ops->ooblen, pages * oob_per_page);
}
+static int mtdchar_adjust_io_lengths(struct mtd_info *mtd, uint64_t start,
+ struct mtd_oob_ops *ops, uint64_t remaining_oob)
+{
+ u32 oob_per_page = mtd_oobavail(mtd, ops);
+ u64 len;
+
+ if (!ops->ooblen)
+ return 0;
+ if (!oob_per_page)
+ return -EINVAL;
+
+ if (ops->ooblen < remaining_oob) {
+ /* Keep a page's OOB together when another chunk follows. */
+ ops->ooblen = rounddown(ops->ooblen, oob_per_page);
+ if (ops->len) {
+ len = (u64)(ops->ooblen / oob_per_page) * mtd->writesize;
+ len -= mtd_mod_by_ws(start, mtd);
+ ops->len = min_t(u64, ops->len, len);
+ }
+ }
+
+ return 0;
+}
+
static noinline_for_stack int
mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
{
@@ -628,7 +652,8 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
return -ENOMEM;
}
- oobbuf_len = min_t(size_t, req.ooblen, mtd->erasesize);
+ oobbuf_len = min_t(size_t, req.ooblen,
+ max(mtd->erasesize, mtd->oobsize));
if (oobbuf_len > 0) {
oobbuf = kvmalloc(oobbuf_len, GFP_KERNEL);
if (!oobbuf) {
@@ -648,13 +673,17 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
/*
* Shorten non-page-aligned, eraseblock-sized writes so that
- * the write ends on an eraseblock boundary. This is necessary
+ * the write ends on a page boundary. This is necessary
* for adjust_oob_length() to properly handle non-page-aligned
* writes.
*/
if (ops.len == mtd->erasesize)
ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
+ ret = mtdchar_adjust_io_lengths(mtd, req.start, &ops, req.ooblen);
+ if (ret)
+ break;
+
/*
* For writes which are not OOB-only, adjust the amount of OOB
* data written according to the number of data pages written.
@@ -674,7 +703,11 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
if (ret)
break;
- req.start += ops.retlen;
+ if (usr_data)
+ req.start += ops.retlen;
+ else
+ req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
+ mtd->writesize;
req.len -= ops.retlen;
usr_data += ops.retlen;
@@ -739,7 +772,8 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
}
}
- oobbuf_len = min_t(size_t, req.ooblen, mtd->erasesize);
+ oobbuf_len = min_t(size_t, req.ooblen,
+ max(mtd->erasesize, mtd->oobsize));
if (oobbuf_len > 0) {
oobbuf = kvmalloc(oobbuf_len, GFP_KERNEL);
if (!oobbuf) {
@@ -761,7 +795,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
/*
* Shorten non-page-aligned, eraseblock-sized reads so that the
- * read ends on an eraseblock boundary. This is necessary in
+ * read ends on a page boundary. This is necessary in
* order to prevent OOB data for some pages from being
* duplicated in the output of non-page-aligned reads requiring
* multiple mtd_read_oob() calls to be completed.
@@ -769,6 +803,9 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
if (ops.len == mtd->erasesize)
ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
+ ret = mtdchar_adjust_io_lengths(mtd, req.start, &ops, req.ooblen);
+ if (ret)
+ break;
if (ops.len < req.len)
adjust_oob_length(mtd, req.start, &ops);
@@ -789,7 +826,11 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
break;
}
- req.start += ops.retlen;
+ if (usr_data)
+ req.start += ops.retlen;
+ else
+ req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
+ mtd->writesize;
req.len -= ops.retlen;
usr_data += ops.retlen;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out
2026-12-04 23:01 [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (2 preceding siblings ...)
2026-12-04 23:02 ` [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages James Hilliard
@ 2026-12-04 23:02 ` James Hilliard
2026-12-04 23:23 ` sashiko-bot
3 siblings, 1 reply; 8+ messages in thread
From: James Hilliard @ 2026-12-04 23:02 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A combined request can consume all of its data while OOB still needs
another buffer-sized chunk. The ioctl loop then stops because the user
data pointer remains non-NULL. MEMREAD reports a short OOB count and
MEMWRITE can return success without programming the remaining OOB.
Continue until both streams are exhausted. Limit combined operations to
the pages covered by their data and pass a NULL data buffer for the
remaining OOB-only operations. Advance past the pages visited by either
stream, including a final partial data page. Limit OOB-only writes to one
page, as required by the raw NAND write interface.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 41 +++++++++++++++++++++++++----------------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 454ec3ec0c98..575fea4a6230 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -613,6 +613,20 @@ static int mtdchar_adjust_io_lengths(struct mtd_info *mtd, uint64_t start,
return 0;
}
+static u64 mtdchar_next_offset(struct mtd_info *mtd, u64 start,
+ struct mtd_oob_ops *ops)
+{
+ u64 next = start + ops->retlen;
+ u64 pages;
+
+ if (!ops->oobretlen)
+ return next;
+
+ pages = DIV_ROUND_UP_ULL(ops->oobretlen, mtd_oobavail(mtd, ops));
+ return max(next, start - mtd_mod_by_ws(start, mtd) +
+ pages * mtd->writesize);
+}
+
static noinline_for_stack int
mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
{
@@ -662,12 +676,12 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
}
}
- while (req.len > 0 || (!usr_data && req.ooblen > 0)) {
+ while (req.len > 0 || req.ooblen > 0) {
struct mtd_oob_ops ops = {
.mode = req.mode,
.len = min_t(size_t, req.len, datbuf_len),
.ooblen = min_t(size_t, req.ooblen, oobbuf_len),
- .datbuf = datbuf,
+ .datbuf = req.len ? datbuf : NULL,
.oobbuf = oobbuf,
};
@@ -684,6 +698,10 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
if (ret)
break;
+ /* Raw NAND accepts only one page per OOB-only write. */
+ if (!ops.len)
+ ops.ooblen = min_t(size_t, ops.ooblen, mtd_oobavail(mtd, &ops));
+
/*
* For writes which are not OOB-only, adjust the amount of OOB
* data written according to the number of data pages written.
@@ -703,11 +721,7 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
if (ret)
break;
- if (usr_data)
- req.start += ops.retlen;
- else
- req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
- mtd->writesize;
+ req.start = mtdchar_next_offset(mtd, req.start, &ops);
req.len -= ops.retlen;
usr_data += ops.retlen;
@@ -782,13 +796,13 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
}
}
- while (req.len > 0 || (!usr_data && req.ooblen > 0)) {
+ while (req.len > 0 || req.ooblen > 0) {
struct mtd_req_stats stats = {};
struct mtd_oob_ops ops = {
.mode = req.mode,
.len = min_t(size_t, req.len, datbuf_len),
.ooblen = min_t(size_t, req.ooblen, oobbuf_len),
- .datbuf = datbuf,
+ .datbuf = req.len ? datbuf : NULL,
.oobbuf = oobbuf,
.stats = &stats,
};
@@ -806,8 +820,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
ret = mtdchar_adjust_io_lengths(mtd, req.start, &ops, req.ooblen);
if (ret)
break;
- if (ops.len < req.len)
- adjust_oob_length(mtd, req.start, &ops);
+ adjust_oob_length(mtd, req.start, &ops);
ret = mtd_read_oob(mtd, (loff_t)req.start, &ops);
@@ -826,11 +839,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
break;
}
- if (usr_data)
- req.start += ops.retlen;
- else
- req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
- mtd->writesize;
+ req.start = mtdchar_next_offset(mtd, req.start, &ops);
req.len -= ops.retlen;
usr_data += ops.retlen;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages
2026-12-04 23:02 ` [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages James Hilliard
@ 2026-12-04 23:20 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-04 23:20 UTC (permalink / raw)
To: James Hilliard, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Michał Kępień
Cc: linux-mtd
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtdchar: mtdchar_read_ioctl() mutates req.start returned to
userspace
--
commit ce9de2caf1bea584d612c86184f12d6eedb89a17
Author: James Hilliard <james.hilliard1@gmail.com>
mtdchar: keep OOB chunks aligned with their data pages
This commit advances the flash offset in OOB-only MEMREAD and MEMWRITE
requests by the number of pages transferred, rounds intermediate OOB
chunks down to whole pages, and caps data chunks when OOB is the
limiting buffer.
> diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
> index 25c59cc7f0974..454ec3ec0c982 100644
> --- a/drivers/mtd/mtdchar.c
> +++ b/drivers/mtd/mtdchar.c
[ ... ]
> @@ -789,7 +826,11 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
> break;
> }
>
> - req.start += ops.retlen;
> + if (usr_data)
> + req.start += ops.retlen;
> + else
> + req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
> + mtd->writesize;
[Severity: Medium]
Does updating req.start directly here change the MEMREAD UAPI for
OOB-only calls?
Unlike mtdchar_write_ioctl(), mtdchar_read_ioctl() copies req back to
userspace before returning. Previously, OOB-only reads had
ops.retlen == 0 and left req.start unchanged; now any OOB-only read of
at least one page of OOB overwrites req.start in userspace.
Callers that reuse struct mtd_read_req across calls (such as reading OOB
and then data from the same offset) will see an altered offset, and
partial-page OOB reads advance req.start inconsistently. Tracking the
cursor in a local variable would keep req.start unchanged.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-mtdchar-page-chunking-v1-v3-0-2f36958a6e8d@gmail.com?part=3
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks
2026-12-04 23:01 ` [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks James Hilliard
@ 2026-12-04 23:23 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-04 23:23 UTC (permalink / raw)
To: James Hilliard, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Michał Kępień
Cc: linux-mtd
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtdchar: mtdchar_read_ioctl() clamps ops.ooblen before bounds
validation
--
commit a1e7568a6832dadbb9cdc28b11f43423f55e0bbb
Author: James Hilliard <james.hilliard1@gmail.com>
mtdchar: bound OOB reads to the data in intermediate chunks
This commit calls adjust_oob_length() on intermediate chunks in
mtdchar_read_ioctl() to limit ops.ooblen to the pages covered by each
chunk's data read, matching MEMWRITE and avoiding duplicate OOB reads.
> diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
> index 87ff071afc974..25c59cc7f0974 100644
> --- a/drivers/mtd/mtdchar.c
> +++ b/drivers/mtd/mtdchar.c
> @@ -769,6 +769,9 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
> if (ops.len == mtd->erasesize)
> ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
>
> + if (ops.len < req.len)
> + adjust_oob_length(mtd, req.start, &ops);
> +
> ret = mtd_read_oob(mtd, (loff_t)req.start, &ops);
>
> req.ecc_stats.uncorrectable_errors +=
[Severity: Medium]
Can clamping ops.ooblen here hide an out-of-bounds req.ooblen from
mtd_check_oob_ops() during intermediate iterations?
mtdchar_read_ioctl() only checks req.start + req.len against mtd->size
upfront and relies on mtd_check_oob_ops() to reject ops.ooblen > maxooblen.
For a multi-eraseblock read near the end of a partition where maxooblen is
below min(req.ooblen, mtd->erasesize), clamping ops.ooblen lets it pass.
Before this patch, the first mtd_read_oob() call returned -EINVAL
immediately with no side effects. With this change, the loop reads the
intermediate eraseblocks, copies partial data and OOB to userspace, and
updates req.start, req.len, req.ooblen, and req.ecc_stats before failing.
Validating req.ooblen against the remaining device OOB capacity before
entering the loop would preserve immediate -EINVAL rejection.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-mtdchar-page-chunking-v1-v3-0-2f36958a6e8d@gmail.com?part=2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out
2026-12-04 23:02 ` [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out James Hilliard
@ 2026-12-04 23:23 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-04 23:23 UTC (permalink / raw)
To: James Hilliard, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Michał Kępień
Cc: linux-mtd
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mtdchar: missing upfront OOB bounds check causes partial
flash I/O
- [Medium] mtdchar: per-page OOB write clamp silently drops OOB pages on
docg3
- [Low] mtdchar: mtdchar_read_ioctl() returns page-rounded req.start to
user
--
commit f16856695cba2d4a1b532796427c369789dbf8de
Author: James Hilliard <james.hilliard1@gmail.com>
mtdchar: finish OOB transfers after the data runs out
This commit keeps the MEMWRITE and MEMREAD ioctl loops running until
both data and OOB transfers complete, clamping OOB-only writes to one
page and advancing req.start past pages touched by either stream.
> diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
> index 454ec3ec0c982..575fea4a62300 100644
> --- a/drivers/mtd/mtdchar.c
> +++ b/drivers/mtd/mtdchar.c
[ ... ]
> @@ -684,6 +698,10 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
> if (ret)
> break;
>
> + /* Raw NAND accepts only one page per OOB-only write. */
> + if (!ops.len)
> + ops.ooblen = min_t(size_t, ops.ooblen, mtd_oobavail(mtd, &ops));
[Severity: Medium]
Can clamping ops.ooblen here cause out-of-bounds MEMWRITE requests to
partially program flash before failing with -EINVAL?
mtdchar_write_ioctl() only validates req.start + req.len against
mtd->size upfront. Previously, a single-chunk OOB-only write whose
req.ooblen exceeded the remaining device OOB capacity was rejected
immediately by mtd_check_oob_ops(). With the per-page clamp, each
iteration passes mtd_check_oob_ops() and irreversibly programs flash
pages up to mtd->size before finally returning -EINVAL.
The same regression occurs in mtdchar_read_ioctl() now that
adjust_oob_length() is called unconditionally: a single-chunk combined
read with an out-of-bounds req.ooblen now reads valid pages, overwrites
user buffers, and updates req.ecc_stats and returned lengths before
failing on the next iteration. Validating total req.ooblen against the
remaining OOB capacity upfront (matching mtd_check_oob_ops()) would
preserve atomic -EINVAL rejection for both ioctls.
[Severity: Medium]
Does splitting multi-page OOB-only writes into per-page calls silently
drop OOB pages on docg3?
In drivers/mtd/devices/docg3.c, doc_write_oob() delegates OOB-only
writes to doc_backup_oob(), which requires ooblen == DOC_LAYOUT_OOB_SIZE
and stages a single page into docg3->oob_write_buf for a later data
write instead of programming flash. Previously, a multi-page OOB-only
MEMWRITE failed with -EINVAL; now, in MTD_OPS_PLACE_OOB or MTD_OPS_RAW
mode, each iteration passes one page, overwriting docg3->oob_write_buf
and returning 0 while discarding all earlier OOB pages.
[ ... ]
> @@ -826,11 +839,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
> break;
> }
>
> - if (usr_data)
> - req.start += ops.retlen;
> - else
> - req.start += (u64)(ops.oobretlen / mtd_oobavail(mtd, &ops)) *
> - mtd->writesize;
> + req.start = mtdchar_next_offset(mtd, req.start, &ops);
[Severity: Low]
Should mtdchar_read_ioctl() avoid returning a page-rounded req.start to
userspace?
Because req is copied back to userspace at the end of
mtdchar_read_ioctl(), updating req.start in place via
mtdchar_next_offset() now returns the next page boundary on partial-page
combined reads instead of start + retlen. Tracking the loop offset in a
local variable would avoid leaking this internal page-rounding into the
returned structure.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-mtdchar-page-chunking-v1-v3-0-2f36958a6e8d@gmail.com?part=4
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-04 23:23 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 23:01 [PATCH v3 0/4] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-04 23:01 ` [PATCH v3 1/4] mtdchar: calculate OOB capacity from the starting column James Hilliard
2026-12-04 23:01 ` [PATCH v3 2/4] mtdchar: bound OOB reads to the data in intermediate chunks James Hilliard
2026-12-04 23:23 ` sashiko-bot
2026-12-04 23:02 ` [PATCH v3 3/4] mtdchar: keep OOB chunks aligned with their data pages James Hilliard
2026-12-04 23:20 ` sashiko-bot
2026-12-04 23:02 ` [PATCH v3 4/4] mtdchar: finish OOB transfers after the data runs out James Hilliard
2026-12-04 23:23 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox