* [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks
@ 2026-12-06 9:58 James Hilliard
2026-12-06 9:58 ` [PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column James Hilliard
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
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.
Reject requests that extend beyond the device before either stream is
transferred. Bound each intermediate chunk to the same data and OOB
pages, and finish any OOB left after the data runs out. MEMREAD still
advances the address returned to userspace only by the main data read.
On raw NAND and OneNAND, check data writes against the subpage size
before programming, and split OOB-only writes at page boundaries.
Check writability and known NAND write-mode restrictions on every
member before programming the first one. Use the backing page column
even after a combined write has transferred its last spare byte.
DiskOnChip G3 can stage one complete spare area for a later data write.
Use the driver's page rules to reject an invalid tail before programming
the first page. On concatenated devices, finish each member's OOB even
when its last main-data byte has already been read.
This series applies after the DiskOnChip G3 v2 [1] and OneNAND v1 [2]
OOB fixes. It uses the DiskOnChip write validator and relies on the
OneNAND fixes for reads with a partial spare area or a nonzero OOB
column.
[1] https://lore.kernel.org/r/20261206-submit-docg3-oob-fixes-v1-v2-0-3b0c523d750c@gmail.com/
[2] https://lore.kernel.org/r/20261204-submit-onenand-oob-fixes-v1-v1-0-6c68634d239b@gmail.com/
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v4:
- Check the whole data and OOB range, including each concat member,
before splitting a request. Use the backing page column for unaligned
partitions.
- Use the DiskOnChip G3 write validator before programming any prefix;
keep the native DiskOnChip and OneNAND OOB fixes in separate series.
- Keep the OOB buffer on later data chunks of a combined MEMWRITE so
DiskOnChip G3 cannot use a spare area staged by an earlier request.
- Check each member's writability, NAND subpage alignment and known raw
write restrictions on OneNAND, raw NAND and SPI-NAND. Limit OOB-only
writes to one page on devices that require it.
- Use each backing member's geometry even on data-only continuations.
- Keep MEMREAD's returned start tied to main data. After a short read,
resume the remaining stream at its own offset. If both streams remain,
require a common restart address.
- Return a concat member's short write to the caller before advancing.
Stop a mixed write if a retry would program the same page again.
- Preserve ECC errors across clean MEMREAD chunks.
- Link to v3: https://patch.msgid.link/20261204-submit-mtdchar-page-chunking-v1-v3-0-2f36958a6e8d@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 (9):
mtdchar: calculate OOB capacity from the backing page column
mtdchar: validate the full OOB range before splitting requests
mtd: concat: keep both streams of an OOB read within each member
mtdchar: check each member before splitting MEMWRITE
mtdchar: keep MEMREAD data and OOB on their backing pages
mtdchar: keep MEMWRITE data and OOB on their backing pages
mtd: nand: check MEMWRITE alignment and raw mode before programming
mtd: concat: return a short OOB write before advancing to another member
mtdchar: retain ECC errors across MEMREAD chunks
drivers/mtd/devices/docg3.c | 11 ++
drivers/mtd/mtdchar.c | 216 ++++++++++++++++++++++++--------
drivers/mtd/mtdconcat.c | 119 ++++++++++++------
drivers/mtd/mtdcore.c | 60 ++++++---
drivers/mtd/mtdcore.h | 68 +++++++++-
drivers/mtd/nand/onenand/onenand_base.c | 25 ++--
drivers/mtd/nand/raw/nand_base.c | 17 +++
drivers/mtd/nand/spi/core.c | 19 ++-
include/linux/mtd/mtd.h | 19 +++
9 files changed, 424 insertions(+), 130 deletions(-)
---
base-commit: 4e5457fcbc14d21e7c004c3159e78fc63838029e
base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next
change-id: 20261106-submit-mtdchar-page-chunking-v1-668636b0becd
prerequisite-change-id: 20261204-submit-docg3-oob-fixes-v1-3ac1e192f872:v2
prerequisite-patch-id: f9a0098b4b9ed5ed775a47942b6ee3bd047b4365
prerequisite-patch-id: c57eb30e2678a75aa7dd5683b557a4e22b94c1f5
prerequisite-patch-id: 07fe496f9c338749547a17449ae49d9bb842596d
prerequisite-patch-id: a355a2c8b80a3e5274337309b0053319ba31c927
prerequisite-patch-id: 1c04bb6a01bf3995d7da59b8c8117549d1adebb4
prerequisite-change-id: 20261204-submit-onenand-oob-fixes-v1-10db347cf4bc:v1
prerequisite-patch-id: 5e8c344b71554f2d37c3e3c199a2b9d6f6208118
prerequisite-patch-id: c16f2d9da60d0ea1b17fb4c0147fd35b0635bcc8
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 2/9] mtdchar: validate the full OOB range before splitting requests James Hilliard
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 2/9] mtdchar: validate the full OOB range before splitting requests
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-06 9:58 ` [PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 3/9] mtd: concat: keep both streams of an OOB read within each member James Hilliard
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
When MEMWRITE splits an OOB request past the end of the device, an early
chunk can program flash before a later chunk fails with -EINVAL. MEMREAD
can likewise copy part of the request to userspace. Checking each chunk
does not validate the original request.
Check the full data and OOB ranges before allocating buffers or starting
I/O. Share the mode-specific OOB range check with the MTD core. Count a
backing page's spare bytes only if the span reaches the end of that page,
even when the span starts partway through it. Compare page counts instead
of storing the device's total OOB capacity in a size_t.
For a concatenated MTD, use each member's OOB capacity in the requested
mode. Members with the same physical spare size can have different amounts
of free space in AUTO_OOB mode.
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 | 15 ++++++++++----
drivers/mtd/mtdconcat.c | 17 ++++++++++++++++
drivers/mtd/mtdcore.c | 37 ++++++++++++++++++++---------------
drivers/mtd/mtdcore.h | 52 +++++++++++++++++++++++++++++++++++++++++++++----
include/linux/mtd/mtd.h | 2 ++
5 files changed, 99 insertions(+), 24 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index d385dd67f98d..a702a3a977ab 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -573,6 +573,15 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
}
}
+static bool mtdchar_req_valid(struct mtd_info *mtd, u64 start, u64 len,
+ size_t ooblen, unsigned int mode)
+{
+ struct mtd_oob_ops ops = { .mode = mode, .ooblen = ooblen };
+
+ return start <= mtd->size && len <= mtd->size - start &&
+ mtd_oob_range_valid(mtd, start, &ops);
+}
+
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
struct mtd_oob_ops *ops)
{
@@ -598,7 +607,6 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
uint8_t *datbuf = NULL, *oobbuf = NULL;
size_t datbuf_len, oobbuf_len;
int ret = 0;
- u64 end;
if (copy_from_user(&req, argp, sizeof(req)))
return -EFAULT;
@@ -618,7 +626,7 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (check_add_overflow(req.start, req.len, &end) || end > mtd->size)
+ if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode))
return -EINVAL;
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
@@ -698,7 +706,6 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
size_t datbuf_len, oobbuf_len;
size_t orig_len, orig_ooblen;
int ret = 0;
- u64 end;
if (copy_from_user(&req, argp, sizeof(req)))
return -EFAULT;
@@ -725,7 +732,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (check_add_overflow(req.start, req.len, &end) || end > mtd->size) {
+ if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode)) {
ret = -EINVAL;
goto out;
}
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 354cc71cd722..51690d5a30ff 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -244,6 +244,22 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
return err;
}
+static struct mtd_info *concat_oob_device(struct mtd_info *mtd, u64 *start)
+{
+ struct mtd_concat *concat = CONCAT(mtd);
+ int i;
+
+ for (i = 0; i < concat->num_subdev; i++) {
+ struct mtd_info *subdev = concat->subdev[i];
+
+ if (*start < subdev->size)
+ return subdev;
+ *start -= subdev->size;
+ }
+
+ return NULL;
+}
+
static int
concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
{
@@ -667,6 +683,7 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
concat->mtd.oobavail = subdev[0]->oobavail;
subdev_master = mtd_get_master(subdev[0]);
+ concat->mtd.master.oob_device = concat_oob_device;
if (subdev_master->_writev)
concat->mtd._writev = concat_writev;
if (subdev_master->_read_oob)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 0d041892e173..af509dbf1f6c 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1650,6 +1650,25 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
}
EXPORT_SYMBOL_GPL(mtd_panic_write);
+bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops)
+{
+ struct mtd_oob_ops left = *ops;
+
+ while (left.ooblen) {
+ struct mtd_oob_span span = mtd_oob_span(mtd, start);
+ size_t n = mtd_oob_span_len(&span, &left);
+
+ if (n == left.ooblen)
+ break;
+ if (!n || mtd_oob_page_offset(span.mtd, span.start + span.len))
+ return false;
+ start += span.len;
+ left.ooblen -= n;
+ left.ooboffs = 0;
+ }
+ return true;
+}
+
static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
struct mtd_oob_ops *ops)
{
@@ -1667,22 +1686,8 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
if (offs < 0 || offs + ops->len > mtd->size)
return -EINVAL;
- if (ops->ooblen) {
- size_t maxooblen;
-
- if (mtd_div_by_ws(offs, mtd) >=
- mtd_div_by_ws(mtd->size, mtd))
- return -EINVAL;
-
- if (ops->ooboffs >= mtd_oobavail(mtd, ops))
- return -EINVAL;
-
- maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
- mtd_div_by_ws(offs, mtd)) *
- mtd_oobavail(mtd, ops)) - ops->ooboffs;
- if (ops->ooblen > maxooblen)
- return -EINVAL;
- }
+ if (!mtd_oob_range_valid(mtd, offs, ops))
+ return -EINVAL;
return 0;
}
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index 3e65f85ec39a..c25b2e25cfdf 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -1,8 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * These are exported solely for the purpose of mtd_blkdevs.c and mtdchar.c.
- * You should not use them for _anything_ else.
- */
+/* Internal interfaces shared by the MTD core, its frontends and concat. */
extern struct mutex mtd_table_mutex;
extern struct backing_dev_info *mtd_bdi;
@@ -13,6 +10,7 @@ int del_mtd_device(struct mtd_info *mtd);
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);
+bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops);
static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
{
@@ -23,6 +21,52 @@ static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
return mtd_mod_by_ws(addr, mtd);
}
+/* A contiguous part of a device with one backing OOB geometry and policy. */
+struct mtd_oob_span {
+ struct mtd_info *mtd;
+ u64 start;
+ u64 len;
+};
+
+static inline struct mtd_oob_span mtd_oob_span(struct mtd_info *mtd, u64 start)
+{
+ struct mtd_info *master = mtd_get_master(mtd);
+ u64 len = start < mtd->size ? mtd->size - start : 0;
+
+ while (len && master->master.oob_device) {
+ start = mtd_get_master_ofs(mtd, start);
+ mtd = master->master.oob_device(master, &start);
+ if (!mtd)
+ return (struct mtd_oob_span){};
+ len = min_t(u64, len, mtd->size - start);
+ master = mtd_get_master(mtd);
+ }
+
+ return (struct mtd_oob_span){ .mtd = mtd, .start = start, .len = len };
+}
+
+/* Number of requested spare bytes addressable before the end of the span. */
+static inline size_t mtd_oob_span_len(const struct mtd_oob_span *span,
+ struct mtd_oob_ops *ops)
+{
+ u32 avail, first, first_data;
+ u64 pages;
+
+ if (!span->len || !ops->ooblen)
+ return 0;
+ avail = mtd_oobavail(span->mtd, ops);
+ first_data = span->mtd->writesize - mtd_oob_page_offset(span->mtd, span->start);
+ if (ops->ooboffs >= avail || span->len < first_data)
+ return 0;
+
+ first = avail - ops->ooboffs;
+ pages = div_u64(span->len - first_data, span->mtd->writesize);
+ if (ops->ooblen <= first || div_u64(ops->ooblen - first - 1, avail) < pages)
+ return ops->ooblen;
+
+ return first + pages * avail;
+}
+
struct mtd_partitions;
int parse_mtd_partitions(struct mtd_info *master, const char * const *types,
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 8d10d9d2e830..6dda43f1b56a 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,7 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @oob_device: find the member of a composite MTD and translate its data offset
*
* This struct is embedded in mtd_info and contains master-specific
* properties/fields. The master is the root MTD device from the MTD partition
@@ -233,6 +234,7 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ struct mtd_info *(*oob_device)(struct mtd_info *mtd, u64 *start);
};
struct mtd_info {
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 3/9] mtd: concat: keep both streams of an OOB read within each member
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-06 9:58 ` [PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column James Hilliard
2026-12-06 9:58 ` [PATCH v4 2/9] mtdchar: validate the full OOB range before splitting requests James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 4/9] mtdchar: check each member before splitting MEMWRITE James Hilliard
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A read spanning two members can fail on the first device because it
receives OOB bytes meant for the second. Finishing the main data can
also end the read early, even when spare bytes remain in the same
member: raw NAND and OneNAND stop a combined read on its last data
page.
Limit combined reads to the pages covered by their data and read any
remaining spare areas from the following pages. Only the first page
uses the caller's OOB offset. Preserve the counts on a short member
read instead of skipping to the next member, and accumulate each
member's ECC statistics before the next call resets them.
Fixes: 8593fbc68b0d ("[MTD] Rework the out of band handling completely")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdconcat.c | 110 +++++++++++++++++++++++++++---------------------
1 file changed, 63 insertions(+), 47 deletions(-)
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 51690d5a30ff..4dec8cc067cc 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -20,6 +20,8 @@
#include <asm/div64.h>
+#include "mtdcore.h"
+
/*
* how to calculate the size required for the above structure,
* including the pointer array subdev points to:
@@ -260,61 +262,75 @@ static struct mtd_info *concat_oob_device(struct mtd_info *mtd, u64 *start)
return NULL;
}
-static int
-concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
+static int concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
{
- struct mtd_concat *concat = CONCAT(mtd);
- struct mtd_oob_ops devops = *ops;
- int i, err, ret = 0;
+ struct mtd_oob_ops part = *ops;
+ int ret = 0;
ops->retlen = ops->oobretlen = 0;
-
- for (i = 0; i < concat->num_subdev; i++) {
- struct mtd_info *subdev = concat->subdev[i];
-
- if (from >= subdev->size) {
- from -= subdev->size;
- continue;
- }
-
- /* partial read ? */
- if (from + devops.len > subdev->size)
- devops.len = subdev->size - from;
-
- err = mtd_read_oob(subdev, from, &devops);
- ops->retlen += devops.retlen;
- ops->oobretlen += devops.oobretlen;
-
- /* Save information about bitflips! */
- if (unlikely(err)) {
- if (mtd_is_eccerr(err)) {
- mtd->ecc_stats.failed++;
- ret = err;
- } else if (mtd_is_bitflip(err)) {
- mtd->ecc_stats.corrected++;
- /* Do not overwrite -EBADMSG !! */
- if (!ret)
- ret = err;
- } else
- return err;
+ while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
+ struct mtd_req_stats stats = {};
+ struct mtd_oob_span span;
+ struct mtd_info *subdev;
+ u64 pos = from;
+ int err;
+
+ subdev = concat_oob_device(mtd, &pos);
+ if (!subdev)
+ return -EINVAL;
+ span = mtd_oob_span(subdev, pos);
+ if (!span.len)
+ return -EINVAL;
+ part.ooblen = ops->ooblen - ops->oobretlen;
+ part.len = ops->len - ops->retlen;
+ if (part.ooblen && part.ooboffs) {
+ /* Only the first page starts partway into its spare area. */
+ u32 col = mtd_oob_page_offset(span.mtd, span.start);
+
+ span.len = min_t(u64, span.len, span.mtd->writesize - col);
+ } else if (part.ooblen && part.len && part.len < span.len) {
+ /* A data-driven reader stops at the last data page. */
+ u32 col = mtd_oob_page_offset(span.mtd, span.start + part.len);
+
+ span.len = min_t(u64, span.len, (u64)part.len +
+ (col ? span.mtd->writesize - col : 0));
}
-
- if (devops.datbuf) {
- devops.len = ops->len - ops->retlen;
- if (!devops.len)
- return ret;
- devops.datbuf += devops.retlen;
+ part.len = min_t(u64, part.len, span.len);
+ part.ooblen = mtd_oob_span_len(&span, &part);
+ if (!part.len && !part.ooblen)
+ return -EINVAL;
+ part.datbuf = part.len ? ops->datbuf + ops->retlen : NULL;
+ part.oobbuf = part.ooblen ? ops->oobbuf + ops->oobretlen : NULL;
+ part.stats = ops->stats ? &stats : NULL;
+
+ err = mtd_read_oob(subdev, pos, &part);
+ ops->retlen += part.retlen;
+ ops->oobretlen += part.oobretlen;
+ if (ops->stats) {
+ ops->stats->uncorrectable_errors += stats.uncorrectable_errors;
+ ops->stats->corrected_bitflips += stats.corrected_bitflips;
+ ops->stats->max_bitflips = max(ops->stats->max_bitflips,
+ stats.max_bitflips);
}
- if (devops.oobbuf) {
- devops.ooblen = ops->ooblen - ops->oobretlen;
- if (!devops.ooblen)
- return ret;
- devops.oobbuf += ops->oobretlen;
+ if (mtd_is_eccerr(err)) {
+ mtd->ecc_stats.failed++;
+ ret = err;
+ } else if (mtd_is_bitflip(err)) {
+ mtd->ecc_stats.corrected++;
+ if (!ret)
+ ret = err;
+ } else if (err) {
+ return err;
}
- from = 0;
+ /* A short member read did not reach the following member. */
+ if (part.retlen != part.len || part.oobretlen != part.ooblen)
+ break;
+ from += span.len;
+ part.ooboffs = 0;
}
- return -EINVAL;
+
+ return ret;
}
static int
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 4/9] mtdchar: check each member before splitting MEMWRITE
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (2 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 3/9] mtd: concat: keep both streams of an OOB read within each member James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 5/9] mtdchar: keep MEMREAD data and OOB on their backing pages James Hilliard
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A MEMWRITE can program a writable member before reaching a read-only
one and returning -EROFS. On DiskOnChip G3, a partial last spare area
can likewise fail after an earlier page has been programmed. An OOB-only
call on that device stages spare bytes for a separate data write; it
cannot complete the OOB tail of a combined request.
Walk the entire request before the first write. Use each member's
writability and let the backing driver check its part of the request
without performing I/O. Reuse the DiskOnChip G3 write check and allow
staging only for a standalone OOB-only request on that device.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 11 ++++++++++
drivers/mtd/mtdchar.c | 22 +++++++++++---------
drivers/mtd/mtdcore.c | 49 +++++++++++++++++++++++++++++++--------------
drivers/mtd/mtdcore.h | 11 ++++++++--
include/linux/mtd/mtd.h | 5 +++++
5 files changed, 71 insertions(+), 27 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 4b2d635616ec..6bef2f7bbcea 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1352,6 +1352,16 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE;
}
+static int doc_check_oob_write(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob)
+{
+ /* Staging spare bytes is only valid as a separate one-page request. */
+ if ((!ops->len && !standalone_oob) || !doc_oob_write_valid(mtd, to, ops))
+ return -EINVAL;
+
+ return 0;
+}
+
/**
* doc_backup_oob - Backup OOB into docg3 structure
* @mtd: flash device
@@ -1775,6 +1785,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
mtd->_erase = doc_erase;
mtd->_read_oob = doc_read_oob;
mtd->_write_oob = doc_write_oob;
+ mtd->master.check_oob_write = doc_check_oob_write;
mtd->_block_isbad = doc_block_isbad;
mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
mtd->oobavail = 8;
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index a702a3a977ab..2ea54fa33cd2 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -573,13 +573,15 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
}
}
-static bool mtdchar_req_valid(struct mtd_info *mtd, u64 start, u64 len,
- size_t ooblen, unsigned int mode)
+static int mtdchar_check_req(struct mtd_info *mtd, u64 start, u64 len,
+ size_t ooblen, unsigned int mode, bool write)
{
- struct mtd_oob_ops ops = { .mode = mode, .ooblen = ooblen };
+ struct mtd_oob_ops ops = { .mode = mode, .len = len, .ooblen = ooblen };
- return start <= mtd->size && len <= mtd->size - start &&
- mtd_oob_range_valid(mtd, start, &ops);
+ if (start > mtd->size || len > mtd->size - start)
+ return -EINVAL;
+
+ return mtd_check_oob_range(mtd, start, &ops, write);
}
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
@@ -626,8 +628,9 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode))
- return -EINVAL;
+ ret = mtdchar_check_req(mtd, req.start, req.len, req.ooblen, req.mode, true);
+ if (ret)
+ return ret;
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
if (datbuf_len > 0) {
@@ -732,10 +735,9 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode)) {
- ret = -EINVAL;
+ ret = mtdchar_check_req(mtd, req.start, req.len, req.ooblen, req.mode, false);
+ if (ret)
goto out;
- }
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
if (datbuf_len > 0) {
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index af509dbf1f6c..a61f84ed19fd 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1650,23 +1650,45 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
}
EXPORT_SYMBOL_GPL(mtd_panic_write);
-bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops)
+int mtd_check_oob_range(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops,
+ bool check_write)
{
- struct mtd_oob_ops left = *ops;
+ struct mtd_oob_ops part = { .mode = ops->mode, .ooboffs = ops->ooboffs };
+ size_t data = check_write ? ops->len : 0, oob = ops->ooblen;
- while (left.ooblen) {
+ while (data || oob) {
struct mtd_oob_span span = mtd_oob_span(mtd, start);
- size_t n = mtd_oob_span_len(&span, &left);
- if (n == left.ooblen)
- break;
- if (!n || mtd_oob_page_offset(span.mtd, span.start + span.len))
- return false;
+ if (!span.len)
+ return -EINVAL;
+ if (check_write && !span.writable)
+ return -EROFS;
+ part.len = min_t(u64, data, span.len);
+ part.ooblen = oob;
+ part.ooblen = mtd_oob_span_len(&span, &part);
+ if (part.ooblen < oob &&
+ (!part.ooblen || mtd_oob_page_offset(span.mtd, span.start + span.len)))
+ return -EINVAL;
+ if (check_write) {
+ struct mtd_info *master = mtd_get_master(span.mtd);
+ bool standalone = !ops->len && part.ooblen == ops->ooblen;
+
+ if (master->master.check_oob_write) {
+ u64 to = mtd_get_master_ofs(span.mtd, span.start);
+ int ret;
+
+ ret = master->master.check_oob_write(master, to, &part,
+ standalone);
+ if (ret)
+ return ret;
+ }
+ }
+ data -= part.len;
+ oob -= part.ooblen;
start += span.len;
- left.ooblen -= n;
- left.ooboffs = 0;
+ part.ooboffs = 0;
}
- return true;
+ return 0;
}
static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
@@ -1686,10 +1708,7 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
if (offs < 0 || offs + ops->len > mtd->size)
return -EINVAL;
- if (!mtd_oob_range_valid(mtd, offs, ops))
- return -EINVAL;
-
- return 0;
+ return mtd_check_oob_range(mtd, offs, ops, false);
}
static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index c25b2e25cfdf..5d3600a8abac 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -10,7 +10,8 @@ int del_mtd_device(struct mtd_info *mtd);
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);
-bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops);
+int mtd_check_oob_range(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops,
+ bool check_write);
static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
{
@@ -26,23 +27,29 @@ struct mtd_oob_span {
struct mtd_info *mtd;
u64 start;
u64 len;
+ bool writable;
};
static inline struct mtd_oob_span mtd_oob_span(struct mtd_info *mtd, u64 start)
{
struct mtd_info *master = mtd_get_master(mtd);
u64 len = start < mtd->size ? mtd->size - start : 0;
+ u32 flags = mtd->flags;
while (len && master->master.oob_device) {
start = mtd_get_master_ofs(mtd, start);
mtd = master->master.oob_device(master, &start);
if (!mtd)
return (struct mtd_oob_span){};
+ flags &= mtd->flags;
len = min_t(u64, len, mtd->size - start);
master = mtd_get_master(mtd);
}
- return (struct mtd_oob_span){ .mtd = mtd, .start = start, .len = len };
+ return (struct mtd_oob_span){
+ .mtd = mtd, .start = start, .len = len,
+ .writable = flags & MTD_WRITEABLE,
+ };
}
/* Number of requested spare bytes addressable before the end of the span. */
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 6dda43f1b56a..2992bbe59b89 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,9 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @check_oob_write: check a write's mode and geometry without performing I/O;
+ * return 0 or a negative errno. standalone_oob means the entire request
+ * is OOB-only on this device.
* @oob_device: find the member of a composite MTD and translate its data offset
*
* This struct is embedded in mtd_info and contains master-specific
@@ -234,6 +237,8 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ int (*check_oob_write)(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob);
struct mtd_info *(*oob_device)(struct mtd_info *mtd, u64 *start);
};
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 5/9] mtdchar: keep MEMREAD data and OOB on their backing pages
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (3 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 4/9] mtdchar: check each member before splitting MEMWRITE James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 6/9] mtdchar: keep MEMWRITE " James Hilliard
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A MEMREAD can return the same spare bytes twice if a chunk reads past
its last data page or if an OOB-only chunk leaves the cursor unchanged.
Conversely, readers that stop at the last data page can omit the
remaining spare areas.
Plan each chunk using the current backing device's page column and OOB
width, including when a concat member differs from the first. Stop
intermediate chunks at a whole spare area and continue until both
streams finish. Track the internal flash cursor separately from the
start returned to userspace, which advances by main-data bytes only.
After a short read, advance each userspace buffer by the byte count the
device returned for that stream, but only after both copies succeed.
Resume a single remaining stream at its own flash offset. If both streams
remain, require a common restart address. Preserve -EBADMSG if the short
read cannot resume.
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 125 +++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 109 insertions(+), 16 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 2ea54fa33cd2..fe875d2e5f7d 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -584,6 +584,12 @@ static int mtdchar_check_req(struct mtd_info *mtd, u64 start, u64 len,
return mtd_check_oob_range(mtd, start, &ops, write);
}
+static u64 mtdchar_data_pages(struct mtd_info *mtd, u64 start, size_t len)
+{
+ return len ? 1ULL + mtd_div_by_ws(mtd_oob_page_offset(mtd, start) +
+ (u64)len - 1, mtd) : 0;
+}
+
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
struct mtd_oob_ops *ops)
{
@@ -593,13 +599,85 @@ static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
if (ops->len == 0 || ops->ooblen == 0)
return;
- pages = mtd_div_by_ws(mtd_oob_page_offset(mtd, start) + (u64)ops->len - 1,
- mtd) + 1;
+ pages = mtdchar_data_pages(mtd, start, ops->len);
oob_per_page = mtd_oobavail(mtd, ops);
ops->ooblen = min_t(u64, ops->ooblen, pages * oob_per_page);
}
+static void 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 == remaining_oob)
+ return;
+
+ /* 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_oob_page_offset(mtd, start);
+ ops->len = min_t(u64, ops->len, len);
+ }
+}
+
+static bool mtdchar_next_offset(const struct mtd_oob_span *span, u64 *start,
+ struct mtd_oob_ops *ops, u64 data_left, u64 oob_left)
+{
+ struct mtd_info *mtd = span->mtd;
+ u32 avail, column;
+ u64 step = ops->retlen;
+
+ /* A read with only one stream left can revisit the other stream's page. */
+ if (!oob_left) {
+ *start += step;
+ return true;
+ }
+ if (!data_left)
+ step = 0;
+ column = mtd_oob_page_offset(mtd, span->start);
+ avail = mtd_oobavail(mtd, ops);
+ if (ops->oobretlen) {
+ u64 pages = DIV_ROUND_UP_ULL(ops->oobretlen, avail);
+
+ step = max(step, pages * mtd->writesize - column);
+ }
+ if (ops->retlen != ops->len || ops->oobretlen != ops->ooblen) {
+ /* Both remaining streams must resume at this one address. */
+ if (data_left && step != ops->retlen)
+ return false;
+ if (oob_left &&
+ (ops->oobretlen % avail ||
+ div_u64(step + column, mtd->writesize) != ops->oobretlen / avail))
+ return false;
+ }
+ *start += step;
+ return true;
+}
+
+static struct mtd_oob_span mtdchar_prepare_chunk(struct mtd_info *mtd, u64 start,
+ struct mtd_oob_ops *ops,
+ u64 oob_left)
+{
+ struct mtd_oob_span span = mtd_oob_span(mtd, start);
+
+ ops->len = min_t(u64, ops->len, span.len);
+ ops->ooblen = mtd_oob_span_len(&span, ops);
+ if (!span.len || (oob_left && !ops->ooblen)) {
+ ops->len = 0;
+ return span;
+ }
+ if (ops->len == mtd->erasesize)
+ ops->len -= mtd_oob_page_offset(span.mtd, span.start + ops->len);
+
+ mtdchar_adjust_io_lengths(span.mtd, span.start, ops, oob_left);
+ adjust_oob_length(span.mtd, span.start, ops);
+
+ return span;
+}
+
static noinline_for_stack int
mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
{
@@ -709,6 +787,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
size_t datbuf_len, oobbuf_len;
size_t orig_len, orig_ooblen;
int ret = 0;
+ u64 from;
if (copy_from_user(&req, argp, sizeof(req)))
return -EFAULT;
@@ -748,7 +827,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) {
@@ -757,28 +837,26 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
}
}
- while (req.len > 0 || (!usr_data && req.ooblen > 0)) {
+ from = req.start;
+ while (req.len > 0 || req.ooblen > 0) {
+ struct mtd_oob_span span;
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,
- .oobbuf = oobbuf,
+ .datbuf = req.len ? datbuf : NULL,
+ .oobbuf = req.ooblen ? oobbuf : NULL,
.stats = &stats,
};
- /*
- * Shorten non-page-aligned, eraseblock-sized reads so that the
- * read ends on an eraseblock 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.
- */
- if (ops.len == mtd->erasesize)
- ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
+ span = mtdchar_prepare_chunk(mtd, from, &ops, req.ooblen);
+ if (!ops.len && !ops.ooblen) {
+ ret = -EINVAL;
+ break;
+ }
- ret = mtd_read_oob(mtd, (loff_t)req.start, &ops);
+ ret = mtd_read_oob(mtd, from, &ops);
req.ecc_stats.uncorrectable_errors +=
stats.uncorrectable_errors;
@@ -788,6 +866,15 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
if (ret && !mtd_is_bitflip_or_eccerr(ret))
break;
+ if (ops.retlen > ops.len || ops.oobretlen > ops.ooblen) {
+ ret = -EIO;
+ break;
+ }
+ if (!ops.retlen && !ops.oobretlen) {
+ if (!mtd_is_eccerr(ret))
+ ret = -EIO;
+ break;
+ }
if (copy_to_user(usr_data, ops.datbuf, ops.retlen) ||
copy_to_user(usr_oob, ops.oobbuf, ops.oobretlen)) {
@@ -795,12 +882,18 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
break;
}
+ /* Only main data advances the start address returned to userspace. */
req.start += ops.retlen;
req.len -= ops.retlen;
usr_data += ops.retlen;
req.ooblen -= ops.oobretlen;
usr_oob += ops.oobretlen;
+ if (!mtdchar_next_offset(&span, &from, &ops, req.len, req.ooblen)) {
+ if (!mtd_is_eccerr(ret))
+ ret = -EIO;
+ break;
+ }
}
/*
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 6/9] mtdchar: keep MEMWRITE data and OOB on their backing pages
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (4 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 5/9] mtdchar: keep MEMREAD data and OOB on their backing pages James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 7/9] mtd: nand: check MEMWRITE alignment and raw mode before programming James Hilliard
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A combined MEMWRITE can report success after programming only the spare
bytes that fit alongside the data. An OOB-only request can also repeat
pages because the flash offset advances only by main-data bytes.
Bound each write by the current backing member and use its page column
even after the last spare byte. With an unaligned concat boundary, using
the virtual column for a data-only continuation can turn a valid NAND
subpage write into a misaligned one after the prefix was programmed.
Finish excess OOB on subsequent pages, one at a time for raw NAND and
OneNAND. Keep the OOB buffer on the data tail of a combined write so
DiskOnChip G3 cannot use a spare area staged by a separate request.
Check the returned counts before advancing. Stop a short mixed write
if the remaining streams disagree or a retry would reprogram a page.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 60 ++++++++++++++++++---------------
drivers/mtd/nand/onenand/onenand_base.c | 1 +
drivers/mtd/nand/raw/nand_base.c | 1 +
include/linux/mtd/mtd.h | 2 ++
4 files changed, 37 insertions(+), 27 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index fe875d2e5f7d..5ff2702d3ac1 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -624,18 +624,19 @@ static void mtdchar_adjust_io_lengths(struct mtd_info *mtd, uint64_t start,
}
static bool mtdchar_next_offset(const struct mtd_oob_span *span, u64 *start,
- struct mtd_oob_ops *ops, u64 data_left, u64 oob_left)
+ struct mtd_oob_ops *ops, u64 data_left, u64 oob_left,
+ bool write)
{
struct mtd_info *mtd = span->mtd;
u32 avail, column;
u64 step = ops->retlen;
/* A read with only one stream left can revisit the other stream's page. */
- if (!oob_left) {
+ if (!oob_left && (!write || !ops->oobretlen)) {
*start += step;
return true;
}
- if (!data_left)
+ if (!write && !data_left)
step = 0;
column = mtd_oob_page_offset(mtd, span->start);
avail = mtd_oobavail(mtd, ops);
@@ -645,6 +646,9 @@ static bool mtdchar_next_offset(const struct mtd_oob_span *span, u64 *start,
step = max(step, pages * mtd->writesize - column);
}
if (ops->retlen != ops->len || ops->oobretlen != ops->ooblen) {
+ /* A mixed write must not program the same page twice. */
+ if (write && mtd_mod_by_ws(step + column, mtd))
+ return false;
/* Both remaining streams must resume at this one address. */
if (data_left && step != ops->retlen)
return false;
@@ -659,7 +663,7 @@ static bool mtdchar_next_offset(const struct mtd_oob_span *span, u64 *start,
static struct mtd_oob_span mtdchar_prepare_chunk(struct mtd_info *mtd, u64 start,
struct mtd_oob_ops *ops,
- u64 oob_left)
+ u64 oob_left, bool write)
{
struct mtd_oob_span span = mtd_oob_span(mtd, start);
@@ -674,6 +678,9 @@ static struct mtd_oob_span mtdchar_prepare_chunk(struct mtd_info *mtd, u64 start
mtdchar_adjust_io_lengths(span.mtd, span.start, ops, oob_left);
adjust_oob_length(span.mtd, span.start, ops);
+ if (write && !ops->len && ops->ooblen &&
+ mtd_get_master(span.mtd)->master.oob_write_per_page)
+ ops->ooblen = min_t(size_t, ops->ooblen, mtd_oobavail(span.mtd, ops));
return span;
}
@@ -717,7 +724,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) {
@@ -726,32 +734,22 @@ 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_span span;
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,
+ /* DocG3 uses NULL to select a staged spare area. */
.oobbuf = oobbuf,
};
- /*
- * Shorten non-page-aligned, eraseblock-sized writes so that
- * the write ends on an eraseblock 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);
-
- /*
- * For writes which are not OOB-only, adjust the amount of OOB
- * data written according to the number of data pages written.
- * This is necessary to prevent OOB data from being skipped
- * over in data+OOB writes requiring multiple mtd_write_oob()
- * calls to be completed.
- */
- adjust_oob_length(mtd, req.start, &ops);
+ span = mtdchar_prepare_chunk(mtd, req.start, &ops, req.ooblen, true);
+ if (!ops.len && !ops.ooblen) {
+ ret = -EINVAL;
+ break;
+ }
if (copy_from_user(datbuf, usr_data, ops.len) ||
copy_from_user(oobbuf, usr_oob, ops.ooblen)) {
@@ -762,13 +760,21 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
ret = mtd_write_oob(mtd, req.start, &ops);
if (ret)
break;
+ if ((!ops.retlen && !ops.oobretlen) ||
+ ops.retlen > ops.len || ops.oobretlen > ops.ooblen) {
+ ret = -EIO;
+ break;
+ }
- req.start += ops.retlen;
req.len -= ops.retlen;
usr_data += ops.retlen;
req.ooblen -= ops.oobretlen;
usr_oob += ops.oobretlen;
+ if (!mtdchar_next_offset(&span, &req.start, &ops, req.len, req.ooblen, true)) {
+ ret = -EIO;
+ break;
+ }
}
kvfree(datbuf);
@@ -850,7 +856,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
.stats = &stats,
};
- span = mtdchar_prepare_chunk(mtd, from, &ops, req.ooblen);
+ span = mtdchar_prepare_chunk(mtd, from, &ops, req.ooblen, false);
if (!ops.len && !ops.ooblen) {
ret = -EINVAL;
break;
@@ -889,7 +895,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
req.ooblen -= ops.oobretlen;
usr_oob += ops.oobretlen;
- if (!mtdchar_next_offset(&span, &from, &ops, req.len, req.ooblen)) {
+ if (!mtdchar_next_offset(&span, &from, &ops, req.len, req.ooblen, false)) {
if (!mtd_is_eccerr(ret))
ret = -EIO;
break;
diff --git a/drivers/mtd/nand/onenand/onenand_base.c b/drivers/mtd/nand/onenand/onenand_base.c
index cfd038b5e6cd..8f1f22d3bb19 100644
--- a/drivers/mtd/nand/onenand/onenand_base.c
+++ b/drivers/mtd/nand/onenand/onenand_base.c
@@ -3902,6 +3902,7 @@ int onenand_scan(struct mtd_info *mtd, int maxchips)
mtd->_unpoint = NULL;
mtd->_read_oob = onenand_read_oob;
mtd->_write_oob = onenand_write_oob;
+ mtd->master.oob_write_per_page = true;
mtd->_panic_write = onenand_panic_write;
#ifdef CONFIG_MTD_ONENAND_OTP
mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index fe8c6dcc9c07..29c2098f1514 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -6430,6 +6430,7 @@ static int nand_scan_tail(struct nand_chip *chip)
mtd->_panic_write = panic_nand_write;
mtd->_read_oob = nand_read_oob;
mtd->_write_oob = nand_write_oob;
+ mtd->master.oob_write_per_page = true;
mtd->_sync = nand_sync;
mtd->_lock = nand_lock;
mtd->_unlock = nand_unlock;
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 2992bbe59b89..e96feae8c82b 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,7 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @oob_write_per_page: OOB-only writes must not cross a page boundary
* @check_oob_write: check a write's mode and geometry without performing I/O;
* return 0 or a negative errno. standalone_oob means the entire request
* is OOB-only on this device.
@@ -237,6 +238,7 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ unsigned int oob_write_per_page : 1;
int (*check_oob_write)(struct mtd_info *mtd, u64 to,
struct mtd_oob_ops *ops, bool standalone_oob);
struct mtd_info *(*oob_device)(struct mtd_info *mtd, u64 *start);
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 7/9] mtd: nand: check MEMWRITE alignment and raw mode before programming
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (5 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 6/9] mtdchar: keep MEMWRITE " James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 8/9] mtd: concat: return a short OOB write before advancing to another member James Hilliard
2026-12-06 9:58 ` [PATCH v4 9/9] mtdchar: retain ECC errors across MEMREAD chunks James Hilliard
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A MEMWRITE can program the first pages before a later chunk reaches
a misaligned NAND subpage or a member that cannot perform a raw write.
Checking the final chunk in its driver is too late to reject the
original request without changing flash.
Check raw NAND and OneNAND data alignment for each member before the
first write. Reject unsupported OneNAND modes with -EINVAL. On raw
NAND, preserve -ENOTSUPP for an unsupported raw data-page write;
spare-only writes use a separate operation.
For SPI-NAND, reject an effective raw write with -EOPNOTSUPP when the
active on-die ECC engine forbids disabling ECC. Use the same raw-mode
decision as the write path. Other ECC engines retain their own behavior.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/nand/onenand/onenand_base.c | 24 +++++++++++++++---------
drivers/mtd/nand/raw/nand_base.c | 16 ++++++++++++++++
drivers/mtd/nand/spi/core.c | 19 +++++++++++++++----
include/linux/mtd/mtd.h | 10 ++++++++++
4 files changed, 56 insertions(+), 13 deletions(-)
diff --git a/drivers/mtd/nand/onenand/onenand_base.c b/drivers/mtd/nand/onenand/onenand_base.c
index 8f1f22d3bb19..ab772219786e 100644
--- a/drivers/mtd/nand/onenand/onenand_base.c
+++ b/drivers/mtd/nand/onenand/onenand_base.c
@@ -2028,6 +2028,17 @@ static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
return ret;
}
+static int onenand_check_oob_write(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob)
+{
+ if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB)
+ return -EINVAL;
+ if (!mtd_oob_write_subpage_aligned(mtd, to, ops))
+ return -EINVAL;
+
+ return 0;
+}
+
/**
* onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
* @mtd: MTD device structure
@@ -2039,15 +2050,9 @@ static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
{
int ret;
- switch (ops->mode) {
- case MTD_OPS_PLACE_OOB:
- case MTD_OPS_AUTO_OOB:
- break;
- case MTD_OPS_RAW:
- /* Not implemented yet */
- default:
- return -EINVAL;
- }
+ ret = onenand_check_oob_write(mtd, to, ops, !ops->len);
+ if (ret)
+ return ret;
onenand_get_device(mtd, FL_WRITING);
if (ops->datbuf)
@@ -3903,6 +3908,7 @@ int onenand_scan(struct mtd_info *mtd, int maxchips)
mtd->_read_oob = onenand_read_oob;
mtd->_write_oob = onenand_write_oob;
mtd->master.oob_write_per_page = true;
+ mtd->master.check_oob_write = onenand_check_oob_write;
mtd->_panic_write = onenand_panic_write;
#ifdef CONFIG_MTD_ONENAND_OTP
mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 29c2098f1514..c99377d58712 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -4465,6 +4465,21 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
return ret;
}
+static int nand_check_oob_write(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+
+ if (!mtd_oob_write_subpage_aligned(mtd, to, ops))
+ return -EINVAL;
+ /* Spare-only writes use write_oob_raw, not write_page_raw. */
+ if (ops->len && ops->mode == MTD_OPS_RAW &&
+ chip->ecc.write_page_raw == nand_write_page_raw_notsupp)
+ return -ENOTSUPP;
+
+ return 0;
+}
+
/**
* nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
* @mtd: MTD device structure
@@ -6431,6 +6446,7 @@ static int nand_scan_tail(struct nand_chip *chip)
mtd->_read_oob = nand_read_oob;
mtd->_write_oob = nand_write_oob;
mtd->master.oob_write_per_page = true;
+ mtd->master.check_oob_write = nand_check_oob_write;
mtd->_sync = nand_sync;
mtd->_lock = nand_lock;
mtd->_unlock = nand_unlock;
diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
index 75a012770f63..be3d4accf9de 100644
--- a/drivers/mtd/nand/spi/core.c
+++ b/drivers/mtd/nand/spi/core.c
@@ -1058,18 +1058,26 @@ static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
return ret ? ret : max_bitflips;
}
+static bool spinand_write_is_raw(struct mtd_info *mtd, const struct mtd_oob_ops *ops)
+{
+ return ops->mode == MTD_OPS_RAW || !mtd->ooblayout;
+}
+
+static int spinand_check_oob_write(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob)
+{
+ return spinand_write_is_raw(mtd, ops) ? -EOPNOTSUPP : 0;
+}
+
static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
struct mtd_oob_ops *ops)
{
struct spinand_device *spinand = mtd_to_spinand(mtd);
struct nand_device *nand = mtd_to_nanddev(mtd);
struct nand_io_iter iter;
- bool disable_ecc = false;
+ bool disable_ecc = spinand_write_is_raw(mtd, ops);
int ret = 0;
- if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
- disable_ecc = true;
-
mutex_lock(&spinand->lock);
nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) {
@@ -1983,6 +1991,9 @@ static int spinand_init(struct spinand_device *spinand)
mtd->_read_oob = spinand_mtd_read;
mtd->_write_oob = spinand_mtd_write;
+ if (nand->ecc.engine == &spinand_ondie_ecc_engine &&
+ (spinand->flags & SPINAND_NO_RAW_ACCESS))
+ mtd->master.check_oob_write = spinand_check_oob_write;
mtd->_block_isbad = spinand_mtd_block_isbad;
mtd->_block_markbad = spinand_mtd_block_markbad;
mtd->_block_isreserved = spinand_mtd_block_isreserved;
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index e96feae8c82b..92b591c8873a 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -6,6 +6,7 @@
#ifndef __MTD_MTD_H__
#define __MTD_MTD_H__
+#include <linux/align.h>
#include <linux/types.h>
#include <linux/uio.h>
#include <linux/list.h>
@@ -485,6 +486,15 @@ static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
}
+static inline bool mtd_oob_write_subpage_aligned(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops)
+{
+ unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
+
+ return !ops->len || (IS_ALIGNED(to, subpagesize) &&
+ IS_ALIGNED(ops->len, subpagesize));
+}
+
static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
loff_t ofs, size_t len)
{
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 8/9] mtd: concat: return a short OOB write before advancing to another member
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (6 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 7/9] mtd: nand: check MEMWRITE alignment and raw mode before programming James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
2026-12-06 9:58 ` [PATCH v4 9/9] mtdchar: retain ECC errors across MEMREAD chunks James Hilliard
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
If a member reports a successful but short write, concat_write_oob()
can program the next member with bytes that belonged to the first one.
This can happen even if the original request fits in the first member.
Return the counts accumulated so far when either stream falls short.
Let the caller decide whether it can resume at the reported position.
Fixes: 8593fbc68b0d ("[MTD] Rework the out of band handling completely")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdconcat.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 4dec8cc067cc..648136711e44 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -363,6 +363,10 @@ concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
if (err)
return err;
+ /* Return a short write before moving to the next member. */
+ if (devops.retlen != devops.len || devops.oobretlen != devops.ooblen)
+ return 0;
+
if (devops.datbuf) {
devops.len = ops->len - ops->retlen;
if (!devops.len)
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 9/9] mtdchar: retain ECC errors across MEMREAD chunks
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
` (7 preceding siblings ...)
2026-12-06 9:58 ` [PATCH v4 8/9] mtd: concat: return a short OOB write before advancing to another member James Hilliard
@ 2026-12-06 9:58 ` James Hilliard
8 siblings, 0 replies; 10+ messages in thread
From: James Hilliard @ 2026-12-06 9:58 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Michał Kępień,
Thomas Gleixner, Robert Jarzmik, Kyungmin Park
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A clean read at the end of a MEMREAD request can hide an earlier
uncorrectable error. The ioctl reconstructs the final status from
per-request counters, which need not describe every ECC error returned
by the backing device.
Remember the worst ECC status across backend calls, including errors
reported only through their return value. Keep the actual counters and
let a later I/O or user-copy failure take precedence.
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/mtdchar.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 5ff2702d3ac1..c453b199e2c6 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -792,7 +792,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
uint8_t *datbuf = NULL, *oobbuf = NULL;
size_t datbuf_len, oobbuf_len;
size_t orig_len, orig_ooblen;
- int ret = 0;
+ int ret = 0, ecc_ret = 0;
u64 from;
if (copy_from_user(&req, argp, sizeof(req)))
@@ -864,6 +864,11 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
ret = mtd_read_oob(mtd, from, &ops);
+ if (mtd_is_eccerr(ret) || stats.uncorrectable_errors)
+ ecc_ret = -EBADMSG;
+ else if (!ecc_ret && (mtd_is_bitflip(ret) || stats.corrected_bitflips))
+ ecc_ret = -EUCLEAN;
+
req.ecc_stats.uncorrectable_errors +=
stats.uncorrectable_errors;
req.ecc_stats.corrected_bitflips += stats.corrected_bitflips;
@@ -908,12 +913,8 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
* adjust the final return code to ensure it accounts for all detected
* ECC errors.
*/
- if (!ret || mtd_is_bitflip(ret)) {
- if (req.ecc_stats.uncorrectable_errors > 0)
- ret = -EBADMSG;
- else if (req.ecc_stats.corrected_bitflips > 0)
- ret = -EUCLEAN;
- }
+ if (!ret || mtd_is_bitflip(ret))
+ ret = ecc_ret;
out:
req.len = orig_len - req.len;
--
2.53.0
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-06 9:59 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06 9:58 [PATCH v4 0/9] mtdchar: keep data and OOB aligned across ioctl chunks James Hilliard
2026-12-06 9:58 ` [PATCH v4 1/9] mtdchar: calculate OOB capacity from the backing page column James Hilliard
2026-12-06 9:58 ` [PATCH v4 2/9] mtdchar: validate the full OOB range before splitting requests James Hilliard
2026-12-06 9:58 ` [PATCH v4 3/9] mtd: concat: keep both streams of an OOB read within each member James Hilliard
2026-12-06 9:58 ` [PATCH v4 4/9] mtdchar: check each member before splitting MEMWRITE James Hilliard
2026-12-06 9:58 ` [PATCH v4 5/9] mtdchar: keep MEMREAD data and OOB on their backing pages James Hilliard
2026-12-06 9:58 ` [PATCH v4 6/9] mtdchar: keep MEMWRITE " James Hilliard
2026-12-06 9:58 ` [PATCH v4 7/9] mtd: nand: check MEMWRITE alignment and raw mode before programming James Hilliard
2026-12-06 9:58 ` [PATCH v4 8/9] mtd: concat: return a short OOB write before advancing to another member James Hilliard
2026-12-06 9:58 ` [PATCH v4 9/9] mtdchar: retain ECC errors across MEMREAD chunks James Hilliard
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®