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 --- 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