From: zhouminqiang <zhouminqiang2@huawei.com>
To: <linux@armlinux.org.uk>, <vz@mleia.com>,
<piotr.wojtaszczyk@timesys.com>, <maddy@linux.ibm.com>,
<dwmw2@infradead.org>, <richard@nod.at>
Cc: <linux-arm-kernel@lists.infradead.org>,
<linux-kernel@vger.kernel.org>, <linuxppc-dev@lists.ozlabs.org>,
<linux-mtd@lists.infradead.org>, <chengzhihao1@huawei.com>,
<yangerkun@huawei.com>, <yi.zhang@huawei.com>
Subject: [PATCH v2 1/7] jffs2: wbuf: clear wbuf on recovery failure paths
Date: Sat, 29 Aug 2026 14:16:51 +0800 [thread overview]
Message-ID: <20260829061658.306854-2-zhouminqiang2@huawei.com> (raw)
In-Reply-To: <20260829061658.306854-1-zhouminqiang2@huawei.com>
Write verification was introduced by commit a6bc432e296d ("[JFFS2]
Add support for write-buffer verification.") to detect transport or
program-time corruption. When verification fails, jffs2_wbuf_recover()
attempts to recover the data: it first calls jffs2_block_refile()
to mark the old eraseblock's remaining space as REF_OBSOLETE,
then rewrites the old block's data along with the new data still
in the wbuf to a new block. However, when the recovery write
verification also fails, the function returns without clearing
c->wbuf_len, leaving the wbuf still pointing to the refiled old
block, which leads to two kinds of bugs.
Both cases below are illustrated with a filesystem of
erasesize=16KB and wbuf_pagesize=512B.
Case 1: BUG_ON in jffs2_link_node_ref():
User: Two 1KB writes
ref0: [A+0, A+1092) (ri:68B + data:1024B)
ref1: [A+1092, A+2184)
User: append 1KB write
...
jffs2_write_dnode
jffs2_flash_writev
c->wbuf_ofs = A+2048, c->wbuf_len = 512
__jffs2_flush_wbuf
mtd_write -> 0-to-1 bit flip
jffs2_verify_write -> verify failed
jffs2_wbuf_recover
jffs2_block_refile
c->nextblock = NULL
jffs2_link_node_ref -> mark A remaining
ref2: [A+2184, A+16384) space REF_OBSOLETE,
jeb_A->free_size = 0
jffs2_reserve_space_gc
jffs2_do_reserve_space
jffs2_find_nextblock
c->nextblock = B
start = A+1092, end = A+2184
end - start >= c->wbuf_pagesize -> recover data in A
mtd_write
jffs2_verify_write -> verify also failed
return -> c->wbuf_ofs = A+2048
c->wbuf_len = 512
retry
jffs2_flash_writev
if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs))
-> SECTOR(to) = B
-> SECTOR(c->wbuf_ofs) = A
__jffs2_flush_wbuf(c, PAD_NOACCOUNT) -> flush residual wbuf data
wbuf_jeb = A -> c->wbuf_ofs still points
to refiled old block
mtd_write -> succeeds via NAND AND
jffs2_verify_write -> passed
if (pad)
jffs2_link_node_ref
ref_offset(ref) = c->wbuf_ofs + c->wbuf_len = A+2560
jeb_A->offset = A
c->sector_size = 16384
jeb_A->free_size = 0
ref_offset(ref) != jeb_A->offset +
c->sector_size - jeb_A->free_size -> BUG
Case 2: deadlock in jffs2_flush_wbuf_pad():
User: 1KB write
A_ref0: [A+0, A+1092) (ri:68B + data:1024B)
User: append 1K write
...
jffs2_write_dnode
jffs2_flash_writev
c->wbuf_ofs = A+1024, c->wbuf_len = 512
__jffs2_flush_wbuf
mtd_write -> bit flip
jffs2_verify_write -> verify failed
jffs2_wbuf_recover
jffs2_block_refile
c->nextblock = NULL
jffs2_link_node_ref -> mark A remaining
A_ref1: [A+1092, A+16384) space as REF_OBSOLETE
jffs2_reserve_space_gc
jffs2_do_reserve_space
jffs2_find_nextblock
c->nextblock = B
start = A, end = A+1092
end - start >= c->wbuf_pagesize -> recover data in A
mtd_write
jffs2_verify_write -> verify also failed
jffs2_add_physical_node_ref -> mark written area in
B_ref0: [B+0, B+1536) B as REF_OBSOLETE
return -> c->wbuf_ofs = A+1024
c->wbuf_len = 512
retry
jffs2_flash_writev
down_write(&c->wbuf_sem)
if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs))
-> SECTOR(to) = B
-> SECTOR(c->wbuf_ofs) = A
__jffs2_flush_wbuf(c, PAD_NOACCOUNT) -> flush residual wbuf data
wbuf_jeb = A -> c->wbuf_ofs still points
to refiled old block
mtd_write -> bit flip
jffs2_verify_write -> verify failed
jffs2_wbuf_recover
jffs2_block_refile
c->nextblock != jeb -> jeb = A, nextblock = B
jffs2_link_node_ref -> append zero-length ref
A_ref2: [A+16384, A+16384) marked REF_OBSOLETE
after the existing one
end = jeb_A->last_node -> inflates to
eraseblock tail
start = A, end = A+16384
jffs2_reserve_space_gc
minsize = end - start = 16384
jffs2_do_reserve_space
jeb = c->nextblock -> points to B
jeb_B->free_size = 16384 - 1536
minsize > jeb_B->free_size -> first recovery's OBSOLETE
ref reduced free_size
jffs2_wbuf_dirty(c)
jffs2_flush_wbuf_pad(c)
down_write(&c->wbuf_sem) -> already held, deadlock
Fix this by setting c->wbuf_len = 0 when jffs2_verify_write fails in
recovery path, and also in the jffs2_reserve_space_gc() and
jffs2_prealloc_raw_node_refs() failure paths, ensuring subsequent
flushes will not attempt writes on refiled blocks.
Signed-off-by: zhouminqiang <zhouminqiang2@huawei.com>
---
fs/jffs2/wbuf.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/fs/jffs2/wbuf.c b/fs/jffs2/wbuf.c
index 3b7803c75d58..61e3dbd4cd7b 100644
--- a/fs/jffs2/wbuf.c
+++ b/fs/jffs2/wbuf.c
@@ -390,6 +390,7 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
if (ret) {
pr_warn("Failed to allocate space for wbuf recovery. Data loss ensues.\n");
kfree(buf);
+ c->wbuf_len = 0;
return;
}
@@ -400,6 +401,7 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
if (ret) {
pr_warn("Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
kfree(buf);
+ c->wbuf_len = 0;
return;
}
@@ -431,12 +433,13 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) {
/* Argh. We tried. Really we did. */
- pr_crit("Recovery of wbuf failed due to a second write error\n");
+ pr_crit("Recovery of wbuf failed due to a second write error. Data loss ensues.\n");
kfree(buf);
if (retlen)
jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
+ c->wbuf_len = 0;
return;
}
pr_notice("Recovery of wbuf succeeded to %08x\n", ofs);
--
2.52.0
next prev parent reply other threads:[~2026-08-29 6:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-29 6:16 [PATCH v2 0/7] jffs2: extend write verification to all write paths zhouminqiang
2026-08-29 6:16 ` zhouminqiang [this message]
2026-08-29 6:16 ` [PATCH v2 2/7] jffs2: replace per-superblock verify buffer with per-write buffer zhouminqiang
2026-08-29 6:16 ` [PATCH v2 3/7] jffs2: write verify: add byte-by-byte comparison on mismatch zhouminqiang
2026-08-29 6:16 ` [PATCH v2 4/7] jffs2: add write verification to direct page writes in flash_writev zhouminqiang
2026-08-29 6:16 ` [PATCH v2 5/7] jffs2: add write verification to NOR direct write paths zhouminqiang
2026-08-29 6:16 ` [PATCH v2 6/7] jffs2: rename CONFIG_JFFS2_FS_WBUF_VERIFY to CONFIG_JFFS2_FS_WRITE_VERIFY zhouminqiang
2026-08-29 6:16 ` [PATCH v2 7/7] jffs2: add runtime toggle for write verification zhouminqiang
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=20260829061658.306854-2-zhouminqiang2@huawei.com \
--to=zhouminqiang2@huawei.com \
--cc=chengzhihao1@huawei.com \
--cc=dwmw2@infradead.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=linux@armlinux.org.uk \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=piotr.wojtaszczyk@timesys.com \
--cc=richard@nod.at \
--cc=vz@mleia.com \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.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