[PATCH v2 2/5] mtd: docg3: invalidate staged OOB on page write and block erase

From: James Hilliard

Date: Sun Dec 06 2026 - 02:10:54 EST


A data-only write can reuse spare bytes from a page's previous program,
even after its block pair has been erased. The slot also starts out
looking valid for page zero, and a concurrent spare-only write can
replace its contents while the page is being programmed.

Start with an empty slot. Hold the cascade lock while staging or using
the spare bytes and their ECC mode. Retire the slot after a successful
program of the matching page and count only pages actually programmed.

Invalidate the slot when issuing the erase command for its block pair.
A timeout or a failure on one plane does not establish that both blocks
are unchanged. If the reset fails before the command, or the erase never
reaches that pair, keep the staged bytes.

Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/devices/docg3.c | 65 ++++++++++++++++++++++++++++-----------------
1 file changed, 41 insertions(+), 24 deletions(-)

diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index b72b2eab5a9a..06de6d43fe32 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1133,13 +1133,15 @@ static int doc_write_erase_wait_status(struct docg3 *docg3)
* @docg3: the device
* @block0: the first block to erase (leftmost plane)
* @block1: the second block to erase (rightmost plane)
+ * @clear_oob: whether the staged spare bytes belong to this block pair
*
* Erase both blocks, and return operation status
*
* Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
* ready for too long
*/
-static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
+static int doc_erase_block(struct docg3 *docg3, int block0, int block1,
+ bool clear_oob)
{
int ret, sector;

@@ -1159,6 +1161,8 @@ static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
doc_setup_addr_sector(docg3, sector);
doc_delay(docg3, 1);

+ if (clear_oob)
+ docg3->oob_write_ofs = -1;
doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
doc_delay(docg3, 2);

@@ -1186,6 +1190,7 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
struct docg3 *docg3 = mtd->priv;
uint64_t len;
int block0, block1, page, ret = 0, ofs = 0;
+ int staged_block = -1;

doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);

@@ -1194,13 +1199,19 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
if (info->addr + info->len > mtd->size || page || ofs)
return -EINVAL;

+ mutex_lock(&docg3->cascade->lock);
+ if (docg3->oob_write_ofs >= 0)
+ calc_block_sector(docg3->oob_write_ofs, &staged_block, &block1,
+ &page, &ofs, docg3->reliable);
calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
docg3->reliable);
- mutex_lock(&docg3->cascade->lock);
doc_set_device_id(docg3, docg3->device_id);
doc_set_reliable_mode(docg3);
- for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
- ret = doc_erase_block(docg3, block0, block1);
+ for (len = info->len; len > 0; len -= mtd->erasesize) {
+ ret = doc_erase_block(docg3, block0, block1,
+ block0 == staged_block);
+ if (ret)
+ break;
block0 += 2;
block1 += 2;
}
@@ -1323,25 +1334,23 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
* @to: the page offset in the chip
* @ops: the OOB size and buffer
*
- * As the docg3 should write a page with its OOB in one pass, and some userland
- * applications do write_oob() to setup the OOB and then write(), store the OOB
- * into a temporary storage. This is very dangerous, as 2 concurrent
- * applications could store an OOB, and then write their pages (which will
- * result into one having its OOB corrupted).
- *
- * The only reliable way would be for userland to call doc_write_oob() with both
- * the page data _and_ the OOB area.
+ * Some applications supply spare bytes before writing the page data.
+ * Another spare-only write can replace those bytes between the two calls.
+ * Callers that need the data and OOB to stay together can supply both in
+ * one write.
*/
static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
{
struct docg3 *docg3 = mtd->priv;

+ mutex_lock(&docg3->cascade->lock);
docg3->oob_write_ofs = to;
docg3->oob_autoecc = ops->mode != MTD_OPS_RAW;
if (ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
else
memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
+ mutex_unlock(&docg3->cascade->lock);
ops->oobretlen = ops->ooblen;
}

@@ -1352,11 +1361,10 @@ static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *
* size
* @ops: the mtd oob structure
*
- * Either write OOB data into a temporary buffer, for the subsequent write
- * page. An OOB-only write supplies one page in the selected mode. If a data
- * buffer is provided as well, issue the page write.
- * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
- * still be filled in if asked for).
+ * An OOB-only write stages one page's spare bytes. Use them only for a
+ * matching write without an OOB buffer. MEMWRITE retains its OOB buffer
+ * in later chunks after the last spare byte; those pages use a zero-filled
+ * spare area and the requested ECC mode.
*
* Returns 0 on success or a negative error code.
*/
@@ -1387,15 +1395,23 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
autoecc = ops->mode != MTD_OPS_RAW;

mutex_lock(&docg3->cascade->lock);
- while (!ret && len > 0) {
+ while (len > 0) {
+ int page_autoecc = autoecc;
+
memset(oob, 0, sizeof(oob));
- if (!ops->oobbuf && ofs == docg3->oob_write_ofs)
- memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
- else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
+ if (!ops->oobbuf && ofs == docg3->oob_write_ofs) {
+ memcpy(oob, docg3->oob_write_buf, sizeof(oob));
+ page_autoecc = docg3->oob_autoecc;
+ } else if (ooblen && ops->mode == MTD_OPS_AUTO_OOB) {
doc_fill_autooob(oob, oobbuf);
- else if (ooblen > 0)
- memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
- ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
+ } else if (ooblen) {
+ memcpy(oob, oobbuf, sizeof(oob));
+ }
+ ret = doc_write_page(docg3, ofs, buf, oob, page_autoecc);
+ if (ret)
+ break;
+ if (ofs == docg3->oob_write_ofs)
+ docg3->oob_write_ofs = -1;

ofs += DOC_LAYOUT_PAGE_SIZE;
len -= DOC_LAYOUT_PAGE_SIZE;
@@ -1730,6 +1746,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
if (docg3->reliable == 2)
mtd->erasesize /= 2;
mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
+ docg3->oob_write_ofs = -1;
mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
mtd->_erase = doc_erase;
mtd->_read_oob = doc_read_oob;

--
2.53.0