From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796449778; b=2OnnS9544z8dXezRShAf++7L6YdS95HIQ+hz/tw96xiqzdktzhtD9Obf9STp7zRem2UD lyulHSklqG63VJHoIXBnxI4ERJ24spr5DGpllHuf2NJ3kWkEVj3fA5tUsMw9qF8sD1pFr +w6+O5zDksbb1x09XjKb14fdW5I9wg99Cn62fVm8a4hUrayR9trqYzP6Z/r96gkFVjtnJ ysrCCP8meJ7uI2236upWkJ5ECphnJ/8sHBEx4BNYLePQ4WMslnGbjsq0k8hSowbpB6iTB HuUwS6KbLfANdawjc67mKjFLKmddM8mGMhcrVgw/yLBHdwNJynqYGvnShuVICcKvvoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796449778; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=VdNWYTaivwfWqlet+ifclW49XcjOxSr1pZYE5C2FGyw=; b=uZf/urkNUQFPM7pOIl5UbKgAtnWeMyuT3yt/VyUowugQhj0YS/pu8qXqF3jdsb/Ybjle equAhAXEVOraLmm38WYctAhw3oxIehB/cJ53YcAX0aAWHideuZWvPPKm/Y+7VCGUPHCFj aXL52trZ0ef3Zhi41LnoPZjYotqoVn3QuEZu4XS7Gxxouhi9PD4CnZj6kjZkIzwFwaxf4 OKQEH8xRVsekj4cnDrjR6/Y3/XKvVvsY+RXG5bIxU91YeyrON5cDiwmU8Bx+0Vfic+JwN E+HQp/XdDM0KquBEv4N3VqAyP4vnEBA6PY/rMKsW7IuXloTemvU3F+Av//jFUuCMSlw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=drjXns4r; arc=none smtp.remote-ip=209.85.210.47 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B1DB606A33D for ; Sat, 5 Dec 2026 05:49:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2B1DB606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=drjXns4r Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-8629fc8ad64so934909a34.1 for ; Fri, 04 Dec 2026 21:49:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796449777; x=1797054577; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=VdNWYTaivwfWqlet+ifclW49XcjOxSr1pZYE5C2FGyw=; b=drjXns4r118DNCiWeJ/pxkDyXArW9G2drkfazk9apS5/NeVYbIDkI8NOpJML8PB2gx 2dd3wfPh0mQFxutOWoCLDafq1dMXdXKKc6JmDOneYXhs4CcKu9RT9JeNdAcfybsKig1o 0XSDRE86JfwSy+fer6GBNYFQg10TSe4yOpMbfNMYuPYRkL1/UULUc9Xa4TsE0iW8Ldya J0z+yOcv9KEMOOyyrES24RRVRgWxMTaPnaAIwr/Ew7OyfKmQOmsrdoQAbABWWayqgX1q C1P7waSJTIOR/ZQc8Z5nxkeffZubga6CYTShWZP71zEmCGQ94jx4jIJzHKQV1ssY+vyi q3rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796449777; x=1797054577; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=VdNWYTaivwfWqlet+ifclW49XcjOxSr1pZYE5C2FGyw=; b=KIzDnLd0IBG5RZJ8cKOhZ5bsfg8Fe0o2A2zjFIy/i2S13lZRhu+VoYyDKS9ScRL4HT X2rx0bfxWM5PxxuYLMS9Y3iIjAT32ILIX2fhK0Pggmw41zrwQYNyOabNbtkwICB7TZEm SVC+KL8l/xBTYRNZ287dba8g+bpcTs+C7lSfe26ye8K9T8XXC/3NkoIRWvtFg2PfK+HC x2/MtZiAhBrwPNdy+TeODj1uCsD8DxuwTKTNnnWD05aROvK/5zBa8QJzvWn1QdPwbyXF nQH1sTrcguyh0AhCBPWuSB6HoG4YCUZXCd0Wk+wpE0Cz21LuNy/48X9bnac2l6aPUuzx 4Org== X-Forwarded-Encrypted: i=1; AL+hErFkw0DaQQF9oAxX1eAAGXlTFq1/m7T7egN6cMZLcDGi3+jv5zLfvX9SxLI8/0/BlPmUTgshvOJZVcb9Osw=@vger.kernel.org X-Gm-Message-State: APuU4Z8aHGvsUIPyc8/XBKws7Z21nbghS5SuWZT8qkyc3qzqtGHYHxFo /8VWNC57mXh+4iq4M2BvltvbRNTRKCghsBG8+fWdlhmHtS8PzeVN4yX1 X-Gm-Gg: AZFdTZe5yXMUhT8GMGpnFabRd5EAmMcPiqm7cZTzE7ZO6xgzEwfR/f/Hb2/AifLjJ26 PbVuk4P9GQSnUTjs4v1gI6nehFFoR51cVdaSMFj73Bckf8USkApBaFJhIyPghOnj3PAGdnTEGKL Ae/cwG8JH26pZ5NgslUidadE6IsT3MMY/rABjRvDnvgEYBpR8qppf9WbQtha9kHbenWNcgaVDE6 ddi5pEOFwGGncHVqACqQNiCoTo05jmSpy/24i+/atBlr+N9kaX1JnBY2hSUD3ckphdJrPu6dQWO r/xxulPbnqEuSrUQeVfh4IQ+vNfLZ8wf1agQc5BZn/Wi1a2P3GLAWMU3jWWWzQB9UrsmVq+iDbD 6pWymxduAIZ7tMRYpXxAk8BD2jYQlbmkON2+QetHIKwlRlDpTAitMVkbbdrPW6na641M4pqNVIt muQR/BI5HMCY8gmoPPRVg8zCEq2xPMQ4+kWrqDCJzp36838FqnKvacYjp8sMXCXo17PdvSnkI3J MVGGiiA8goYECpc0UAxlkKJX30nCytH5edpiZ3U+MpHk2CFEQM3BjMtJNyHu82TApN7tiD5XcOE TXNjdDVUwH1KLnlBhZtZen/K4jfqCEIno4Z5uL70Cz9E1Wak2D+ykEt9+UvbDulrbQZVBhEE4G8 FzodonmoTDN/yGSr4idduzgoQmcRjzPXi556Ti7jo7t3ay9OgeFrIg5YHW9Aqz0zG79SXHWLGKJ +sFKy/JiY/u/QDSBIPCFMEXbRzEiYAKXyw7C7HJZGzzfhhIYs6QiquX2c= X-Received: by 2002:a05:6830:2318:b0:81e:b506:592d with SMTP id 46e09a7af769-863f8a5242fmr5664299a34.27.1796449777097; Fri, 04 Dec 2026 21:49:37 -0800 (PST) Received: from [127.0.1.1] (174-29-20-82.hlrn.qwest.net. [174.29.20.82]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8331d0csm3931160a34.21.2026.12.04.21.49.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 21:49:36 -0800 (PST) From: James Hilliard Date: Fri, 04 Dec 2026 22:49:28 -0700 Subject: [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-submit-docg3-oob-fixes-v1-v1-2-ea0bf6c535ca@gmail.com> References: <20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com> In-Reply-To: <20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com> To: Robert Jarzmik , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Ivan Djelic , David Woodhouse , Mike Dunn , Brian Norris , Artem Bityutskiy Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, James Hilliard , stable@vger.kernel.org X-Mailer: b4 0.17-dev 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