From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796449779; b=3jW0LQddiodVo+RO4BB7RAHNntdbmgafM+0xsQypiWrM21U0Rr/9q8cdfNcQF8oUa2wz GBUWab5O/FIDPHqTelUj3HrnA0ws3zN6dmIpHt805SSY+V8FMbysdw8OxOw9RIdasu8tI F6VtMnwx8NNGEFczUo0p7SdNipAQ6PYITL+m1K/sA2LeFze1y8vkdrlp1ppZY1W/ZWMlf N+dnSbVBmyXsvNVoNvd59dBMNyjt0J4l2T68e5VIxYqU+Q/bu3oE4jNjBdLQv9WqP4nld Tr+kxgwGM/CeFNmbTshf6cE+3jimfLvq8LcRI3Jexn2vEw2/EbIKEtADbDuPdnyWvLg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796449779; 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=Tde75sz3jnw8ROO19AkzKLCynjD/zEtBxzxxxq1DF9w=; b=nGgAQuTm+HtJf8k+8r39pJJTkk2dFkacHC+ewEzlFg7slwWcSrVMhPrn8jT3jonIIJVV wSVHIpoNA3O2W0daDAp8Y5XD3WDKQsMISsV2ksWVBgww8xGKin2VEatfKfhJaFMV/xl5n qqxMfJF4ihihGoxaHeSppmIocP0komSs4bMw60tllo38DnnB1UWv3jYbc1G+F9VLCuts/ IuSP+Lwfmld6Efcps8JeoR0tZPA/Ax20lvtDkUk0yv9e+8lvopeozIAyiyZFjmGMKIg6b cQPug6FJ/NT6iJvgDhbI6XDKh7eJvZ5H29HVOTBd1bwBVNH/Lgvl448ssUSr5ZXXvVQ== 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=FJZ363nj; arc=none smtp.remote-ip=209.85.210.48 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (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 63988606A686 for ; Sat, 5 Dec 2026 05:49:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 63988606A686 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=FJZ363nj Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-843b6f8e2c7so2475999a34.1 for ; Fri, 04 Dec 2026 21:49:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796449778; x=1797054578; 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=Tde75sz3jnw8ROO19AkzKLCynjD/zEtBxzxxxq1DF9w=; b=FJZ363nj7Rb0e6DfgsoyVR65monA4FQW1xJtrTjV9/+KcQeZrgFazTzDt0XxbVqN19 tldY4P1oxotALsRu215cmn26TLlEoOZSUKyU3nnxUcAELr87u10uOkScm023GH1g8+l8 0t5dufeqCZlWE/4EA+dlerjPQB51DsJNKTrp8yKR0dtt+SQS6ldDrr27b8mQDRqp0xzP 8KtMFzgWbpoLr6qyicWJ7B4CsctY/itoI8wbG3sXk2KI9wxydfH2VTy+3SkuUK5g+44c GBkhA2UOuGspXeXGXrcJ3c0M6AqHb/Mdlko0giKIlQ/0yYze0CcPT4jKLsFfUKAfWBm8 jnuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796449778; x=1797054578; 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=Tde75sz3jnw8ROO19AkzKLCynjD/zEtBxzxxxq1DF9w=; b=rDnsQ7nZjenbsf2Bs363/AfggIWsnLgLMP1JR6IPdTS0aNH9iLFGzpUg4Byo7uVDY8 2gI4wIwPJ08UTXlKpDCvpMZriBxQ2nCSG2RypvNajDbnEgbcpNKWyPSPZL+ueA4u020u SlfWxkUiZGYpT2xoIgxmQWBJx1XyOKlrhUruLpjUF+RrL0O1KL8NKWh1oboRmqCTnxJs eFfC6v+ogJhmHRS75cDVzNbik1iwkklcc/aE0Un98yVD+VORzRtJ3vI48MBSleheW/2g wapAjXPV/9jAhd2t74DfqJ3DaMbEOzqvHByRz7OO6cB59VqIu7ol5ANU3bQr+DLAqwIp ZYHQ== X-Forwarded-Encrypted: i=1; AL+hErGY4XITBvqemYhkRHn/KS0XyvoLvOWUG3WInHOY4IFqeiD5VmPbBTbDjBhaDmYTCGtDspHaLyOiziFUQLo=@vger.kernel.org X-Gm-Message-State: APuU4Z+xIbx3DFidXprRqKc2vgFNXqvCUbnioTc2wKfQdcehszZwgLMR QjqpR19Bbv81NxpeSIS+zP4UbCNm5WGoH0F6jm2DfADREMRDqSsbbixT X-Gm-Gg: AZFdTZfepNqaRdV6N6lz9QPA0MFL9vlh40Mwnz2trwL6cZ0s/KcRZi9HCHxNU85kKxc rAXyFppLl1DJSKK2ohVoBZ9OkYW6ivdIrQY9qk/rN4BDQfJsxas2I30ByvtjapMTiqpXDtOPIHQ R+W/QvMkcSdy/wNfV7V9DGspHh+G0vJAXgUdIkL7ivzW6GOcTipekS9xkWvc8/l/L8CXVEUgzSN HtXp8xEeiDZiPOo9hEs+OU9JwLN3DLDZjHNfRh4uFy0v+9vwsPHI2y/PQjiXwJazJR32k1z2UHH tHOEcAAPZxqc2t8ge7APMGH6Lf9yTsiZ8XQm+NLcgGBkuBGOXwNoUkkD3uoEN8yayUvbZu8/Qdm LGyn4jo+NCxB0N13qDcUdJfYBuacfWpt2d1qGR7tEuR1U9mFju6SoXNHvo17R7r2XEORTfH9k4b 5Y1kGWjFE90hOsSax72Siqa9SziXYFdfXKjtAqGIPKjf41g3LFiAGvAf5ZN0P72wqAIAYMT/i65 boXz1JsuVlhhc6DOfJPq/wuQhwY2FaoV6cg34xMSkFyV5W+z5340DRAmwLhROCIgWT+drZUTvMp oUyuMLpncYDYzWnaSqQEHJ+h/vUJ1TPnOCO2N6mRh2mpJLnIgWE0tL8USp+IBWk5wUmHLLyeUx5 Za/Nsd99Nik1Ggvk5S6B1HA+SaBr7GR4TjMDyj1V/Jfx+KaxJodyC5AGaZNujOeKnb5YFOqXoNe h/kilqLjFfkUzYS8ItX1pauuTCzOQOfkRwsh1BRpUDlmAftf+VM5O+wNM= X-Received: by 2002:a05:6830:4391:b0:80c:4d85:9aae with SMTP id 46e09a7af769-863f89a52bcmr4959921a34.15.1796449778289; Fri, 04 Dec 2026 21:49:38 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 21:49:37 -0800 (PST) From: James Hilliard Date: Fri, 04 Dec 2026 22:49:29 -0700 Subject: [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte 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-3-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 full-page read can report a corrected error in the spare area while returning the wrong byte. The BCH codeword covers 512 data bytes and eight spare bytes, but all corrections are applied relative to the separate data buffer. A spare correction therefore writes past the page data instead of fixing the spare byte. Apply each correction to the appropriate buffer before copying the requested spare bytes. Leave the stored BCH parity outside the data correction range. Fixes: d13d19ece39f ("mtd: docg3: add ECC correction code") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 48 ++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index 5b03a596e100..5c1d667c8f8b 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -620,16 +620,14 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes) /** * doc_ecc_bch_fix_data - Fix if need be read data from flash * @docg3: the device - * @buf: the buffer of read data (512 + 7 + 1 bytes) + * @buf: the main data of the page + * @oob: the physical spare area of the page * @hwecc: the hardware calculated ECC. * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB * area data, and calc_ecc the ECC calculated by the hardware generator. * - * Checks if the received data matches the ECC, and if an error is detected, - * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3 - * understands the (data, ecc, syndroms) in an inverted order in comparison to - * the BCH library, the function reverses the order of bits (ie. bit7 and bit0, - * bit6 and bit 1, ...) for all ECC data. + * BCH protects the main data and the first eight spare bytes. Apply each + * correction to the buffer containing the affected byte. * * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch * algorithm is used to decode this. However the hw operates on page @@ -637,10 +635,10 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes) * requiring that the bits be reversed on the result. Thanks to Ivan * Djelic for his analysis. * - * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit - * errors were detected and cannot be fixed. + * Return: The number of bit errors reported by the decoder, or -EBADMSG + * if the errors cannot be corrected. */ -static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc) +static int doc_ecc_bch_fix_data(struct docg3 *docg3, u8 *buf, u8 *oob, u8 *hwecc) { u8 ecc[DOC_ECC_BCH_SIZE]; int errorpos[DOC_ECC_BCH_T], i, numerrs; @@ -654,12 +652,15 @@ static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc) if (numerrs < 0) goto out; - for (i = 0; i < numerrs; i++) - errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7)); - for (i = 0; i < numerrs; i++) - if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8) - /* error is located in data, correct it */ - change_bit(errorpos[i], buf); + for (i = 0; i < numerrs; i++) { + unsigned int byte = errorpos[i] / 8; + u8 mask = BIT(7 - (errorpos[i] % 8)); + + if (byte < DOC_LAYOUT_PAGE_SIZE) + buf[byte] ^= mask; + else if (byte < DOC_ECC_BCH_COVERED_BYTES) + oob[byte - DOC_LAYOUT_PAGE_SIZE] ^= mask; + } out: doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs); return numerrs; @@ -870,6 +871,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, u8 *oobbuf = ops->oobbuf; u8 *buf = ops->datbuf; size_t len, ooblen, nbdata, nboob; + u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2); u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1; struct mtd_ecc_stats old_stats; int max_bitflips = 0; @@ -917,20 +919,20 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata - skip, NULL, 0, (skip + nbdata) % 2); - ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0); + ret = doc_read_page_getbytes(docg3, nboob, oob, 0, 0); if (ret < nboob) goto err_in_read; doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob, - NULL, 0, nboob % 2); + oob + nboob, 0, nboob % 2); doc_get_bch_hw_ecc(docg3, hwecc); eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1); if (nboob >= DOC_LAYOUT_OOB_SIZE) { - doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf); - doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]); - doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8); - doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]); + doc_dbg("OOB - INFO: %*phC\n", 7, oob); + doc_dbg("OOB - HAMMING: %02x\n", oob[7]); + doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oob + 8); + doc_dbg("OOB - UNUSED: %02x\n", oob[15]); } doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1); doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc); @@ -944,7 +946,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) && (ops->mode != MTD_OPS_RAW) && (nbdata == DOC_LAYOUT_PAGE_SIZE)) { - ret = doc_ecc_bch_fix_data(docg3, buf, hwecc); + ret = doc_ecc_bch_fix_data(docg3, buf, oob, hwecc); if (ret < 0) { mtd->ecc_stats.failed++; ret = -EBADMSG; @@ -956,6 +958,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, } } + if (nboob) + memcpy(oobbuf, oob, nboob); doc_read_page_finish(docg3); ops->retlen += nbdata; ops->oobretlen += nboob; -- 2.53.0