From: James Hilliard <james.hilliard1@gmail.com>
To: Robert Jarzmik <robert.jarzmik@free.fr>,
Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>,
Vignesh Raghavendra <vigneshr@ti.com>,
Ivan Djelic <ivan.djelic@parrot.com>,
David Woodhouse <David.Woodhouse@intel.com>,
Mike Dunn <mikedunn@newsguy.com>,
Brian Norris <computersforpeace@gmail.com>,
Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
James Hilliard <james.hilliard1@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase
Date: Fri, 04 Dec 2026 22:49:28 -0700 [thread overview]
Message-ID: <20261204-submit-docg3-oob-fixes-v1-v1-2-ea0bf6c535ca@gmail.com> (raw)
In-Reply-To: <20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com>
A combined write at offset zero can program a zero-filled spare area
instead of the caller's bytes because the staging slot initially looks
valid. The slot can also outlive a page program or a block erase, so a
later data-only write can reuse spare bytes meant for an earlier write.
Start with an empty slot and give explicit OOB precedence. Protect the
staged address, bytes and ECC mode with the same lock as programming.
Use them only for a data-only write to the matching page. Retire the
slot when that page is programmed or its physical block pair is erased.
Keep the slot on a failed program and count only successfully
programmed pages in the returned data and OOB lengths.
Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 55 ++++++++++++++++++++++++++++-----------------
1 file changed, 34 insertions(+), 21 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 9617b1ebba0f..5b03a596e100 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1186,6 +1186,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 +1195,20 @@ 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) {
+ for (len = info->len; len > 0; len -= mtd->erasesize) {
ret = doc_erase_block(docg3, block0, block1);
+ if (ret)
+ break;
+ if (block0 == staged_block)
+ docg3->oob_write_ofs = -1;
block0 += 2;
block1 += 2;
}
@@ -1323,25 +1331,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 +1358,9 @@ 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. A data-only write uses
+ * staged bytes for the matching page, or a zero-filled spare area otherwise.
+ * A combined write uses the spare bytes supplied with the data.
*
* Returns 0 on success or a negative error code.
*/
@@ -1387,15 +1391,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 (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 (ooblen && ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(oob, oobbuf);
- else if (ooblen > 0)
+ else if (ooblen)
memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
- ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
+ else if (ofs == docg3->oob_write_ofs) {
+ memcpy(oob, docg3->oob_write_buf, sizeof(oob));
+ page_autoecc = docg3->oob_autoecc;
+ }
+ 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 +1742,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
next prev parent reply other threads:[~2026-12-05 5:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 5:49 [PATCH 0/5] mtd: docg3: fix spare-area transfers and ECC reporting James Hilliard
2026-12-05 5:49 ` [PATCH 1/5] mtd: docg3: check whole-page OOB writes in the selected mode James Hilliard
2026-12-05 5:49 ` James Hilliard [this message]
2026-12-05 5:49 ` [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte James Hilliard
2026-12-05 5:49 ` [PATCH 4/5] mtd: docg3: return the requested spare bytes in the selected OOB mode James Hilliard
2026-12-05 5:49 ` [PATCH 5/5] mtd: docg3: report the maximum bitflip count across read pages James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204-submit-docg3-oob-fixes-v1-v1-2-ea0bf6c535ca@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=David.Woodhouse@intel.com \
--cc=artem.bityutskiy@linux.intel.com \
--cc=computersforpeace@gmail.com \
--cc=ivan.djelic@parrot.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=mikedunn@newsguy.com \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=robert.jarzmik@free.fr \
--cc=stable@vger.kernel.org \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®