[PATCH 5/5] mtd: docg3: report the maximum bitflip count across read pages

From: James Hilliard

Date: Sat Dec 05 2026 - 00:49:49 EST


A clean last page can hide earlier bitflips in a multi-page read.
doc_read_oob() tracks the maximum number of bitflips in any page, but
sets its return value to zero each time it starts checking another page.
An MTD caller can therefore miss the indication that the read crossed
the device's bitflip threshold.

Return the maximum after the last successful page. Also record it in
the request statistics so a later failing page does not erase it.

Fixes: edbc4540e02c ("mtd: driver _read() returns max_bitflips; mtd_read() returns -EUCLEAN")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/devices/docg3.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index c00693aba84a..051c6f56d59a 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -861,13 +861,13 @@ static bool doc_oob_mode_valid(unsigned int mode)
/**
* doc_read_oob - Read out of band bytes from flash
* @mtd: the device
- * @from: the offset from first block and first page, in bytes, aligned on page
- * size
+ * @from: byte offset in the device
* @ops: the mtd oob structure
*
* Reads flash memory OOB area of pages.
*
- * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
+ * Return: The maximum number of bitflips in any page read, or a negative
+ * error code.
*/
static int doc_read_oob(struct mtd_info *mtd, loff_t from,
struct mtd_oob_ops *ops)
@@ -960,7 +960,6 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
if (ret > 0) {
mtd->ecc_stats.corrected += ret;
max_bitflips = max(max_bitflips, ret);
- ret = max_bitflips;
}
}

@@ -989,6 +988,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
from += DOC_LAYOUT_PAGE_SIZE;
skip = 0;
}
+ if (ret >= 0)
+ ret = max_bitflips;

out:
if (ops->stats) {
@@ -996,6 +997,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
mtd->ecc_stats.failed - old_stats.failed;
ops->stats->corrected_bitflips +=
mtd->ecc_stats.corrected - old_stats.corrected;
+ ops->stats->max_bitflips = max_bitflips;
}
mutex_unlock(&docg3->cascade->lock);
return ret;

--
2.53.0