jffs2: extend write verification to all write paths
Subsystems (5)
v4 cover jffs2: extend write verification to all write paths
When JFFS2 writes data to flash, corruption can occur during the write transfer (RAM failures, bus errors) or after commit to the medium (bit flips). To distinguish whether the corruption happened before or after the data reached the flash, commit a6bc432e296d ("[JFFS2] Add support for write-buffer verification.") introduced CONFIG_JFFS2_FS_WBUF_VERIFY: reading the data back immediately after a successful write and comparing it with the in-memory source buffer provides the missing observation point for that diagnosis.
However, the current implementation only performs read-back verification on write-buffer flush paths. NOR flash devices write directly through jffs2_flash_direct_write() and jffs2_flash_direct_writev(), with no equivalent check. Data corruption incidents have been observed on NOR-based devices in production environments, yet there is no quick diagnostic tool to isolate whether the corruption occurred during the write transfer or after commit to the medium. A significant number of deployed devices rely on JFFS2 on NOR flash, making this gap a practical concern.
During the investigation of the NOR flash gap, code inspection also revealed that when the write data length exceeds wbuf_pagesize in jffs2_flash_writev(), the excess data bypasses the write buffer and is written directly to flash via mtd_write(), with no verification. Additionally, jffs2_wbuf_recover() does not clear c->wbuf_len on recovery failure, which can lead to BUG_ON in jffs2_link_node_ref() or deadlock in jffs2_flush_wbuf_pad() on a subsequent write.
This series closes these gaps with the following changes:
- Patch 1: fix wbuf recovery failure exit paths to clear
c->wbuf_len, preventing BUG_ON and deadlock
- Patch 2: fix ref_totlen misuse in jffs2_wbuf_recover() failure path
- Patch 3: replace pre-allocated per-superblock wbuf_verify
buffer with on-demand allocation inside jffs2_verify_write()
- Patch 4: add byte-by-byte comparison after memcmp() mismatch to pinpoint the exact mismatch offset
- Patch 5: add verification in jffs2_flash_writev() for the mtd_write() path that bypasses the write buffer
- Patch 6: add verification calls in jffs2_flash_direct_write() and jffs2_flash_direct_writev() for NOR flash devices
- Patch 7: rename CONFIG_JFFS2_FS_WBUF_VERIFY to CONFIG_JFFS2_FS_WRITE_VERIFY and remove the Kconfig dependency on CONFIG_JFFS2_FS_WRITEBUFFER
- Patch 8: add module parameter write_verify for runtime enable/disable of write verification
This series extends the existing write verification mechanism to cover all write paths. It remains off by default and does not alter JFFS2's node CRC integrity checks.
Changes in v4:
- Change the OBSOLETE length in jffs2_wbuf_recover() secondary write
failure path from retlen to end - start, and update the commit message
- Link to v3: https://lore.kernel.org/linux-mtd/[email protected]/T/#t
Changes in v3:
- Add patch to fix incorrect ref_totlen usage in jffs2_wbuf_recover()
secondary write failure path, which could corrupt free_size accounting
- Add __GFP_NOWARN to jffs2_verify_write() kmalloc to suppres
high-order allocation splats on large-erasesize NAND
- Link to v2: https://lore.kernel.org/linux-mtd/[email protected]/T/#t
Changes in v2:
- Add patch to fix wbuf recovery failure exit paths not clearing
c->wbuf_len, preventing BUG_ON and deadlock on subsequent writes
- Use kmalloc() instead of vmalloc() for verify buffer allocation
- Keep memcmp() on the hotpath and add byte-by-byte comparison
only after a mismatch, rather than replacing memcmp() entirely
- In jffs2_flash_direct_write() and jffs2_flash_direct_writev(),
call mtd_write() before jffs2_sum_add_kvec() to prevent
retlen from being uninitialized if jffs2_sum_add_kvec() causes
an early return
- Update defconfig files to rename CONFIG_JFFS2_FS_WBUF_VERIFY to CONFIG_JFFS2_FS_WRITE_VERIFY
- Link to v1: https://lore.kernel.org/linux-mtd/[email protected]/T/#t
zhouminqiang (8):
jffs2: wbuf: clear wbuf on recovery failure paths
jffs2: wbuf: fix OBSOLETE under-coverage on recovery failure
jffs2: replace per-superblock verify buffer with per-write buffer
jffs2: write verify: add byte-by-byte comparison on mismatch
jffs2: add write verification to direct page writes in flash_writev
jffs2: add write verification to NOR direct write paths
jffs2: rename CONFIG_JFFS2_FS_WBUF_VERIFY to
CONFIG_JFFS2_FS_WRITE_VERIFY
jffs2: add runtime toggle for write verification
arch/arm/configs/keystone_defconfig | 2 +- arch/arm/configs/lpc32xx_defconfig | 2 +- arch/arm/configs/pxa3xx_defconfig | 2 +- arch/arm/configs/pxa_defconfig | 2 +- arch/powerpc/configs/44x/fsp2_defconfig | 2 +- fs/jffs2/Kconfig | 31 ++++- fs/jffs2/jffs2_fs_sb.h | 3 - fs/jffs2/os-linux.h | 11 ++ fs/jffs2/wbuf.c | 93 +++------------ fs/jffs2/writev.c | 144 +++++++++++++++++++++++- 10 files changed, 199 insertions(+), 93 deletions(-)
Signature · 1 line
2.52.0
PATCH v4 1/8 jffs2: wbuf: clear wbuf on recovery failure paths
From: zhouminqiang <[email protected]>
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 -> BUGCase 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, deadlockFix 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.
--- fs/jffs2/wbuf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/jffs2/wbuf.c +4 -1
@@ -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);
Signature · 1 line
2.52.0
PATCH v4 2/8 jffs2: wbuf: fix OBSOLETE under-coverage on recovery failure
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
In jffs2_wbuf_recover(), when the recovery write to the new erase block also fails, the code marks the already-written portion as REF_OBSOLETE via jffs2_add_physical_node_ref(). However, the length passed is ref_totlen(c, jeb, first_raw), which is the length of a single node on the old block, rather than the full range of data that was attempted to be written to the new block.
On the recovery target block the layout is:
ref_totlen(first_raw)
|<------------->|
ofs +---------------+-------+-------+ +--------+
| node 1 |node 2 |node 3 | ... |erased |
+---------------+-------+-------+ +--------+
| OBSOLETE | |
|<-towrite (page-aligned)->| |
|<-------- end - start -------->| |
|<-truly free->|When the recovery buffer contains multiple nodes, ref_totlen only accounts for the first node's length, which can be much smaller than the total range. This under-deducts free_size, so the next write lands at the start of node 2, which is already programmed on NAND, and the AND operation corrupts both the old and new data.
With towrite as the OBSOLETE length, the next write lands right after towrite in truly free space. However, node 3's header has been written within the towrite region while its data extends beyond it due to page-alignment truncation. On remount, the scanner finds node 3's header, validates its CRC, and trusts its totlen -- skipping PAD(totlen_node3) bytes. This skip extends past towrite into the area where the subsequent write was placed, creating a shadow zone that causes the newly written data to be silently lost.
Use end - start as the OBSOLETE length, which covers the full range of data that was attempted to be written to the new block, so that neither the NAND AND corruption nor the scanner shadow zone can occur.
--- fs/jffs2/wbuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jffs2/wbuf.c +1 -1
@@ -437,7 +437,7 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)kfree (buf);if (retlen) -jffs2_add_physical_node_ref (c, ofs |REF_OBSOLETE ,ref_totlen (c, jeb, first_raw),NULL ); +jffs2_add_physical_node_ref (c, ofs |REF_OBSOLETE , end-start,NULL ); c->wbuf_len =0 ;return ;
Signature · 1 line
2.52.0
PATCH v4 3/8 jffs2: replace per-superblock verify buffer with per-write buffer
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
Subsequent patches will extend write verification to additional write paths that may execute concurrently. A shared per-superblock buffer would require a coarse lock to serialize all verification, hurting concurrency.
To avoid this contention, remove the wbuf_verify field from jffs2_sb_info, along with the scattered kmalloc/kfree of wbuf_verify in jffs2_nand_flash_setup, jffs2_dataflash_setup, jffs2_nor_wbuf_flash_setup and their corresponding cleanup functions.
Instead, allocate a temporary buffer inside jffs2_verify_write(), giving each invocation its own buffer and eliminating the shared state.
--- fs/jffs2/jffs2_fs_sb.h | 3 --- fs/jffs2/wbuf.c | 61 ++++++++++++++++-------------------------- 2 files changed, 23 insertions(+), 41 deletions(-)
fs/jffs2/jffs2_fs_sb.h +0 -3
@@ -124,9 +124,6 @@ struct jffs2_sb_info {uint32_t wbuf_pagesize;/* 0 for NOR and other flashes with no wbuf */ -#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY -unsigned char *wbuf_verify;/* read-back buffer for verification */ -#endif #ifdef CONFIG_JFFS2_FS_WRITEBUFFER unsigned char *wbuf;/* Write-behind buffer for NAND flash */ uint32_t wbuf_ofs;
fs/jffs2/wbuf.c +23 -38
116 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v4 4/8 jffs2: write verify: add byte-by-byte comparison on mismatch
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
jffs2_verify_write() uses memcmp() to compare the write buffer against data read back from flash. On mismatch, the current code dumps the entire source and read-back data, which will become impractical once the verify path is extended to NOR flash where a single write can span PAGE_SIZE.
Add a byte-by-byte comparison after the memcmp() mismatch path to locate the exact mismatch offset, reporting the first differing offset and dumping up to 128 bytes of both the source and read-back data. memcmp() remains on the hotpath for successful writes, and the byte-by-byte loop only runs when an error is actually detected.
--- fs/jffs2/wbuf.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-)
fs/jffs2/wbuf.c +28 -18
@@ -231,7 +231,7 @@ static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf, uint32_t ofs) {int ret; -size_t retlen; +size_t retlen, i;char *eccstr;void *verify_buf;
@@ -246,10 +246,10 @@ static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf,if (ret && ret != -EUCLEAN && ret != -EBADMSG ) {pr_warn ("%s(): Read back of page at %08x failed: %d\n" , - __func__, c->wbuf_ofs, ret); + __func__, ofs, ret); goto out_free; }else if (retlen != c->wbuf_pagesize) { -pr_warn ("%s(): Read back of page at %08x gave short read: %zd not %d\n" , +pr_warn ("%s(): Read back of page at %08x gave short read: %zu not %d\n" , __func__, ofs, retlen, c->wbuf_pagesize); ret = -EIO ; goto out_free;
@@ -259,24 +259,34 @@ static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf, goto out_free; } -if (ret == -EUCLEAN ) - eccstr ="corrected" ; -else if (ret == -EBADMSG ) - eccstr ="correction failed" ; -else - eccstr ="OK or unused" ; +for (i =0 ; i < c->wbuf_pagesize; i++) { +uint8_t c1 = ((uint8_t *)buf)[i]; +uint8_t c2 = ((uint8_t *)verify_buf)[i]; +int dump_len; -pr_warn ("Write verify error (ECC %s) at %08x. Wrote:\n" , - eccstr, c->wbuf_ofs); -print_hex_dump (KERN_WARNING ,"" ,DUMP_PREFIX_OFFSET ,16 ,1 , - c->wbuf, c->wbuf_pagesize,0 ); +if (c1 == c2) +continue ; -pr_warn ("Read back:\n" ); -print_hex_dump (KERN_WARNING ,"" ,DUMP_PREFIX_OFFSET ,16 ,1 , - verify_buf, c->wbuf_pagesize,0 ); +if (ret == -EUCLEAN ) + eccstr ="corrected" ; +else if (ret == -EBADMSG ) + eccstr ="correction failed" ; +else + eccstr ="OK or unused" ; -kfree (verify_buf); -return -EIO ; + dump_len =min_t (int,128 , c->wbuf_pagesize - i); +pr_warn ("Write verify error (ECC %s) at %08x (+%zu/%d). Wrote:\n" , + eccstr, ofs, i, c->wbuf_pagesize); +print_hex_dump (KERN_WARNING ,"" ,DUMP_PREFIX_OFFSET ,16 ,1 , + buf + i, dump_len,0 ); + +pr_warn ("Read back:\n" ); +print_hex_dump (KERN_WARNING ,"" ,DUMP_PREFIX_OFFSET ,16 ,1 , + verify_buf + i, dump_len,0 ); + + ret = -EIO ; + goto out_free; + } out_free:kfree (verify_buf);
Signature · 1 line
2.52.0
PATCH v4 5/8 jffs2: add write verification to direct page writes in flash_writev
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
jffs2_flash_writev() performs writes in three phases: phase 1 fills the write buffer and flushes it when full, phase 2 writes full pages directly via mtd_write() bypassing the buffer, and phase 3 fills the remaining data into the write buffer.
Phases 1 and 3 both invoke __jffs2_flush_wbuf() when the buffer is full, which includes write-back verification when CONFIG_JFFS2_FS_WBUF_VERIFY is enabled. However phase 2, which handles the bulk of the write data, calls mtd_write() directly without any verification.
Phase 2 direct writes may span multiple wbuf_pagesize pages. Extend jffs2_verify_write() with a len parameter to specify the data length to verify, and add the verification call after the phase 2 mtd_write() to cover this gap. Also add __GFP_NOWARN to the kmalloc in jffs2_verify_write() to suppress high-order allocation warnings when verifying summary writes on large-erasesize NAND.
--- fs/jffs2/wbuf.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
fs/jffs2/wbuf.c +21 -14
@@ -228,38 +228,38 @@ static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY static int jffs2_verify_write (struct jffs2_sb_info *c,unsigned char *buf, -uint32_t ofs) +uint32_t ofs,size_t len) {int ret;size_t retlen, i;char *eccstr;void *verify_buf; - verify_buf =kmalloc (c->wbuf_pagesize,GFP_NOFS ); + verify_buf =kmalloc (len,GFP_NOFS | __GFP_NOWARN);if (!verify_buf) {pr_warn ("%s(): verify buffer allocation failed, skipping verification\n" , __func__);return 0 ; } - ret =mtd_read (c->mtd, ofs, c->wbuf_pagesize, &retlen, verify_buf); + ret =mtd_read (c->mtd, ofs, len, &retlen, verify_buf);if (ret && ret != -EUCLEAN && ret != -EBADMSG ) {pr_warn ("%s(): Read back of page at %08x failed: %d\n" , __func__, ofs, ret); goto out_free; - }else if (retlen != c->wbuf_pagesize) { -pr_warn ("%s(): Read back of page at %08x gave short read: %zu not %d\n" , - __func__, ofs, retlen, c->wbuf_pagesize); + }else if (retlen != len) { +pr_warn ("%s(): Read back of page at %08x gave short read: %zu not %zu\n" , + __func__, ofs, retlen, len); ret = -EIO ; goto out_free; } -if (!memcmp (buf, verify_buf, c->wbuf_pagesize)) { +if (!memcmp (buf, verify_buf, len)) { ret =0 ; goto out_free; } -for (i =0 ; i < c->wbuf_pagesize; i++) { +for (i =0 ; i < len; i++) {uint8_t c1 = ((uint8_t *)buf)[i];uint8_t c2 = ((uint8_t *)verify_buf)[i];int dump_len;
@@ -274,9 +274,9 @@ static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf,else eccstr ="OK or unused" ; - dump_len =min_t (int,128 , c->wbuf_pagesize - i); -pr_warn ("Write verify error (ECC %s) at %08x (+%zu/%d). Wrote:\n" , - eccstr, ofs, i, c->wbuf_pagesize); + dump_len =min_t (int,128 , len - i); +pr_warn ("Write verify error (ECC %s) at %08x (+%zu/%zu). Wrote:\n" , + eccstr, ofs, i, len);print_hex_dump (KERN_WARNING ,"" ,DUMP_PREFIX_OFFSET ,16 ,1 , buf + i, dump_len,0 );
@@ -293,7 +293,7 @@ static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf, return ret; }#else -#define jffs2_verify_write (c,b,o) (0) +#define jffs2_verify_write (c,b,o,l) (0)#endif /* Recover from failure to write wbuf. Recover the nodes up to the
@@ -458,7 +458,7 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c) ret =mtd_write (c->mtd, ofs, towrite, &retlen, rewrite_buf); -if (ret || retlen != towrite ||jffs2_verify_write (c, rewrite_buf, ofs)) { +if (ret || retlen != towrite ||jffs2_verify_write (c, rewrite_buf, ofs, towrite)) {/* Argh. We tried. Really we did. */ pr_crit ("Recovery of wbuf failed due to a second write error. Data loss ensues.\n" );kfree (buf);
@@ -676,7 +676,10 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad) retlen, c->wbuf_pagesize); ret = -EIO ; goto wfail; - }else if ((ret =jffs2_verify_write (c, c->wbuf, c->wbuf_ofs))) { + } + + ret =jffs2_verify_write (c, c->wbuf, c->wbuf_ofs, c->wbuf_pagesize); +if (ret) { wfail:jffs2_wbuf_recover (c);
@@ -908,6 +911,10 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,if (ret <0 || wbuf_retlen !=PAGE_DIV (vlen)) goto outfile; + ret =jffs2_verify_write (c, v, outvec_to,PAGE_DIV (vlen)); +if (ret) + goto outfile; + vlen -= wbuf_retlen; outvec_to += wbuf_retlen; c->wbuf_ofs = outvec_to;
Signature · 1 line
2.52.0
PATCH v4 6/8 jffs2: add write verification to NOR direct write paths
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
NOR Flash and other non-writebuffered devices write directly through jffs2_flash_direct_writev() and jffs2_flash_direct_write() without any write-back verification. If mtd_write() succeeds but the readable medium differs from JFFS2's source buffer, a later node CRC failure cannot distinguish transport/program-time corruption from post-commit media damage.
Move jffs2_verify_write() from wbuf.c to writev.c so it can be shared by both writebuffered and direct write paths. Add jffs2_verify_writev() to iterate over kvec entries and verify each one individually. In both direct write functions, add mtd_write() return value and retlen checks, and invoke verification after a successful complete write.
In jffs2_flash_direct_writev(), move the mtd_writev() call before jffs2_sum_add_kvec() so that *retlen is always set by the MTD layer first. The original ordering let jffs2_sum_add_kvec() return early on error without ever touching *retlen, leaving the caller's retlen check to read an uninitialized value. Keep the same order in jffs2_flash_direct_write().
--- fs/jffs2/os-linux.h | 11 ++++ fs/jffs2/wbuf.c | 70 ------------------------- fs/jffs2/writev.c | 121 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 131 insertions(+), 71 deletions(-)
fs/jffs2/os-linux.h +11 -0
@@ -192,6 +192,17 @@ int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,int jffs2_flash_direct_write (struct jffs2_sb_info *c,loff_t ofs,size_t len,size_t *retlen,const u_char *buf); +#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY +int jffs2_verify_write (struct jffs2_sb_info *c,const unsigned char *buf, +uint32_t ofs,size_t len); +int jffs2_verify_writev (struct jffs2_sb_info *c, +const struct kvec *vecs, +unsigned long count,loff_t to); +#else +#define jffs2_verify_write (c, b, o, l) (0) +#define jffs2_verify_writev (c, v, cnt, t) (0) +#endif +#endif /* __JFFS2_OS_LINUX_H__ */
fs/jffs2/wbuf.c +0 -70
76 lines not on this page · raw message
fs/jffs2/writev.c +120 -1
147 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v4 7/8 jffs2: rename CONFIG_JFFS2_FS_WBUF_VERIFY to CONFIG_JFFS2_FS_WRITE_VERIFY
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
Write verification now covers all write paths including NOR direct writes and write-buffer direct page writes, not just the write-buffer flush path. The Kconfig option also no longer depends on CONFIG_JFFS2_FS_WRITEBUFFER.
Rename the option from CONFIG_JFFS2_FS_WBUF_VERIFY to CONFIG_JFFS2_FS_WRITE_VERIFY to reflect its broader scope and independence from the write-buffer configuration.
Update defconfig files that explicitly enabled the old symbol to use the new name, so that platforms which previously relied on this verification continue to have it enabled.
--- arch/arm/configs/keystone_defconfig | 2 +- arch/arm/configs/lpc32xx_defconfig | 2 +- arch/arm/configs/pxa3xx_defconfig | 2 +- arch/arm/configs/pxa_defconfig | 2 +- arch/powerpc/configs/44x/fsp2_defconfig | 2 +- fs/jffs2/Kconfig | 23 ++++++++++++++++++----- fs/jffs2/os-linux.h | 2 +- fs/jffs2/writev.c | 4 ++-- 8 files changed, 26 insertions(+), 13 deletions(-)
arch/arm/configs/keystone_defconfig +1 -1
@@ -212,7 +212,7 @@ CONFIG_VFAT_FS=y CONFIG_NTFS_FS=y CONFIG_TMPFS=y CONFIG_JFFS2_FS=y -CONFIG_JFFS2_FS_WBUF_VERIFY=y +CONFIG_JFFS2_FS_WRITE_VERIFY=y CONFIG_UBIFS_FS=y CONFIG_CRAMFS=y CONFIG_NFS_FS=y
arch/arm/configs/lpc32xx_defconfig +1 -1
@@ -164,7 +164,7 @@ CONFIG_MSDOS_FS=y CONFIG_VFAT_FS=y CONFIG_TMPFS=y CONFIG_JFFS2_FS=y -CONFIG_JFFS2_FS_WBUF_VERIFY=y +CONFIG_JFFS2_FS_WRITE_VERIFY=y CONFIG_UBIFS_FS=y CONFIG_CRAMFS=y CONFIG_NFS_FS=y
arch/arm/configs/pxa3xx_defconfig +1 -1
@@ -84,7 +84,7 @@ CONFIG_LEDS_TRIGGER_BACKLIGHT=m CONFIG_LEDS_TRIGGER_GPIO=m CONFIG_LEDS_TRIGGER_DEFAULT_ON=m CONFIG_JFFS2_FS=y -CONFIG_JFFS2_FS_WBUF_VERIFY=y +CONFIG_JFFS2_FS_WRITE_VERIFY=y CONFIG_JFFS2_COMPRESSION_OPTIONS=y CONFIG_JFFS2_LZO=y CONFIG_JFFS2_RUBIN=y
arch/arm/configs/pxa_defconfig +1 -1
@@ -596,7 +596,7 @@ CONFIG_TMPFS_POSIX_ACL=y CONFIG_CONFIGFS_FS=y CONFIG_JFFS2_FS=m CONFIG_JFFS2_FS_DEBUG=1 -CONFIG_JFFS2_FS_WBUF_VERIFY=y +CONFIG_JFFS2_FS_WRITE_VERIFY=y CONFIG_JFFS2_SUMMARY=y CONFIG_JFFS2_FS_XATTR=y CONFIG_JFFS2_COMPRESSION_OPTIONS=y
arch/powerpc/configs/44x/fsp2_defconfig +1 -1
@@ -97,7 +97,7 @@ CONFIG_EXT4_FS_SECURITY=y CONFIG_PROC_KCORE=y CONFIG_TMPFS=y CONFIG_JFFS2_FS=y -CONFIG_JFFS2_FS_WBUF_VERIFY=y +CONFIG_JFFS2_FS_WRITE_VERIFY=y CONFIG_JFFS2_SUMMARY=y CONFIG_JFFS2_FS_XATTR=y CONFIG_CRAMFS=y
fs/jffs2/Kconfig +18 -5
@@ -42,13 +42,26 @@ config JFFS2_FS_WRITEBUFFER - NOR flash with transparent ECC - DataFlash -config JFFS2_FS_WBUF_VERIFY - bool "Verify JFFS2 write-buffer reads" - depends on JFFS2_FS_WRITEBUFFER +config JFFS2_FS_WRITE_VERIFY + bool "Verify JFFS2 writes" + depends on JFFS2_FS default n help - This causes JFFS2 to read back every page written through the - write-buffer, and check for errors. + Read back data immediately after flash writes and compare it + with the in-memory image that was written. This covers both + write-buffer flushes and direct writes to non-writebuffered + devices. + + This may catch corruption introduced after node CRCs are + calculated but before/while data is transferred to the flash + controller (e.g. RAM or DMA), where mtd_write() may succeed + while the medium does not match what JFFS2 intended. + + Without an immediate read-back, a later node CRC failure cannot + tell transport/program-time corruption from post-commit media + damage. + + If unsure, say 'N'. config JFFS2_SUMMARY bool "JFFS2 summary support"
fs/jffs2/os-linux.h +1 -1
@@ -192,7 +192,7 @@ int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,int jffs2_flash_direct_write (struct jffs2_sb_info *c,loff_t ofs,size_t len,size_t *retlen,const u_char *buf); -#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY +#ifdef CONFIG_JFFS2_FS_WRITE_VERIFY int jffs2_verify_write (struct jffs2_sb_info *c,const unsigned char *buf,uint32_t ofs,size_t len); int jffs2_verify_writev(struct jffs2_sb_info *c,
fs/jffs2/writev.c +2 -2
@@ -14,7 +14,7 @@#include <linux/mtd/mtd.h> #include "nodelist.h" -#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY +#ifdef CONFIG_JFFS2_FS_WRITE_VERIFY int jffs2_verify_write (struct jffs2_sb_info *c,const unsigned char *buf,uint32_t ofs,size_t len) {
@@ -100,7 +100,7 @@ int jffs2_verify_writev(struct jffs2_sb_info *c, }return 0 ; } -#endif /* CONFIG_JFFS2_FS_WBUF_VERIFY */ +#endif /* CONFIG_JFFS2_FS_WRITE_VERIFY */ int jffs2_flash_direct_writev (struct jffs2_sb_info *c,const struct kvec *vecs,unsigned long count,loff_t to,size_t *retlen)
Signature · 1 line
2.52.0
PATCH v4 8/8 jffs2: add runtime toggle for write verification
In reply to Zhou MinqiangFrom: zhouminqiang <[email protected]>
Write verification is a diagnostic facility that adds read-back overhead to every write. In production, this overhead is undesirable unless fault isolation is required.
Add a module parameter jffs2.write_verify (bool, 0644) that defaults to off when CONFIG_JFFS2_FS_WRITE_VERIFY is enabled. The verification entry checks READ_ONCE(jffs2_write_verify) and returns immediately when disabled, avoiding any overhead. The parameter can be toggled at runtime via /sys/module/jffs2/parameters/write_verify or set at boot/modprobe time.
--- fs/jffs2/Kconfig | 8 ++++++++ fs/jffs2/writev.c | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+)
fs/jffs2/Kconfig +8 -0
@@ -61,6 +61,14 @@ config JFFS2_FS_WRITE_VERIFY tell transport/program-time corruption from post-commit media damage. + Verification defaults to off when this option is selected and can + be enabled at runtime via sysfs: + + /sys/module/jffs2/parameters/write_verify + + Write 0 to disable, 1 to enable. Boot/modprobe parameter + jffs2.write_verify=0|1 is also supported. + If unsure, say 'N'. config JFFS2_SUMMARY
fs/jffs2/writev.c +23 -0
@@ -9,12 +9,32 @@* */ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/kernel.h> +#include <linux/module.h> #include <linux/slab.h> #include <linux/mtd/mtd.h> #include "nodelist.h" #ifdef CONFIG_JFFS2_FS_WRITE_VERIFY +/* +* Optional read-back after writes. +* +* Catch cases where data is corrupted after node CRCs are calculated but +* before it is correctly programmed -- e.g. in RAM or during DMA/bus +* transfer to the flash controller -- so mtd_write() succeeds while the +* medium does not match the in-memory image. +* +* Runtime toggle: /sys/module/jffs2/parameters/write_verify +* (also boot/modprobe: jffs2.write_verify=0|1) +*/ +static bool jffs2_write_verify; +module_param_named (write_verify, jffs2_write_verify, bool,0644 ); +MODULE_PARM_DESC (write_verify, +"Verify flash writes by reading back (default: N)" ); + +int jffs2_verify_write (struct jffs2_sb_info *c,const unsigned char *buf,uint32_t ofs,size_t len) {
@@ -23,6 +43,9 @@ int jffs2_verify_write(struct jffs2_sb_info *c, const unsigned char *buf,char *eccstr;void *verify_buf; +if (!READ_ONCE (jffs2_write_verify)) +return 0 ; + verify_buf =kmalloc (len,GFP_NOFS | __GFP_NOWARN);if (!verify_buf) { pr_warn("%s(): verify buffer allocation failed, skipping verification\n",
Signature · 1 line
2.52.0