Skip to contentSkip to conversation

jffs2: check the data CRC of a node in RAM, not through point()

PATCH In review Orgad Shaneh 2 participants 4 messages Nov 18 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 jffs2: check the data CRC of a node in RAM, not through point()

check_node_data() points at the node data when the MTD supports it and runs crc32() over the flash, which saves a kmalloc() and a copy. That paid off while the generic crc32() read words (slice-by-8). Since commit 5e3c1c48fac3 ("lib/crc32: remove other generic implementations") it reads a byte at a time, and on a CPU without an arch CRC32 and an uncached map, such as a NOR flash on a boot bus, every byte is a bus access of its own, while jffs2_flash_read() copies a word per load. The MTDs that can be pointed at today are cfi_cmdset_0001 (Intel NOR), lpddr, map_ram and map_rom.

On an Octeon board with an 8-bit AMD NOR, with point() added to cfi_cmdset_0002 (under review [1]), the first lookup of a 46.6 MB file checks the data at 2.77 MB/s through point() and at 3.37 MB/s from a buffer (16.8 s against 13.8 s), the rate of a plain read of the partition.

Always read the data into a buffer. MTDs that can be pointed at now pay a kmalloc() and a copy per node, as all others already do.

[1] https://lore.kernel.org/all/[email protected]/

---
 fs/jffs2/readinode.c | 65 +++++++++++++++-----------------------------
 1 file changed, 22 insertions(+), 43 deletions(-)
fs/jffs2/readinode.c +22 -43
index 1caabff9d..18a47b432 100644
@@ -31,7 +31,7 @@
 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
 {
 	struct jffs2_raw_node_ref *ref = tn->fn->raw;
-	int err = 0, pointed = 0;
+	int err = 0;
 	struct jffs2_eraseblock *jeb;
 	unsigned char *buffer;
 	uint32_t crc, ofs, len;
@@ -61,48 +61,32 @@ static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info
 	dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
 		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
 
-#ifndef __ECOS
-	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
-	 * adding and jffs2_flash_read_end() interface. */
-	err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
-	if (!err && retlen < len) {
-		JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
-		mtd_unpoint(c->mtd, ofs, retlen);
-	} else if (err) {
-		if (err != -EOPNOTSUPP)
-			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
-	} else
-		pointed = 1; /* successfully pointed to device */
-#endif
-
-	if (!pointed) {
-		buffer = kmalloc(len, GFP_KERNEL);
-		if (unlikely(!buffer))
-			return -ENOMEM;
+	/*
+	 * Read the data into RAM even when the flash can be pointed at:
+	 * the generic crc32() goes through it a byte at a time, and on an
+	 * uncached map each byte is a bus access of its own, while the
+	 * read copies a word per load.
+	 */
+	buffer = kmalloc(len, GFP_KERNEL);
+	if (unlikely(!buffer))
+		return -ENOMEM;
 
-		/* TODO: this is very frequent pattern, make it a separate
-		 * routine */
-		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
-		if (err) {
-			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
-			goto free_out;
-		}
+	/* TODO: this is very frequent pattern, make it a separate routine */
+	err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
+	if (err) {
+		JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
+		goto free_out;
+	}
 
-		if (retlen != len) {
-			JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
-			err = -EIO;
-			goto free_out;
-		}
+	if (retlen != len) {
+		JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
+		err = -EIO;
+		goto free_out;
 	}
 
 	/* Continue calculating CRC */
 	crc = crc32(tn->partial_crc, buffer, len);
-	if(!pointed)
-		kfree(buffer);
-#ifndef __ECOS
-	else
-		mtd_unpoint(c->mtd, ofs, len);
-#endif
+	kfree(buffer);
 
 	if (crc != tn->data_crc) {
 		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
@@ -133,12 +117,7 @@ static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info
 	return 0;
 
 free_out:
-	if(!pointed)
-		kfree(buffer);
-#ifndef __ECOS
-	else
-		mtd_unpoint(c->mtd, ofs, len);
-#endif
+	kfree(buffer);
 	return err;
 }
 
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 jffs2: reschedule between the data CRC checks of an inode

The first read of an inode checks the data CRC of each of its unchecked nodes, all in one call. On an Octeon board with an 8-bit NOR, a 46.6 MB file takes 13.8 s of kernel time there. Without kernel preemption, nothing else ran on that CPU in the meantime: an RT watchdog thread missed its deadline and the board rebooted. With this, the lookup takes as long, but the watchdog, the per-CPU workqueue and the other tasks of that CPU keep running.

jffs2_get_inode_nodes() already reschedules per node while it reads the headers; do the same before each data check.

---
 fs/jffs2/readinode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/jffs2/readinode.c +7 -0
index 18a47b432..bfee9e57f 100644
@@ -39,6 +39,13 @@ static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info
 
 	BUG_ON(tn->csize == 0);
 
+	/*
+	 * A large inode has thousands of data nodes to check on its first
+	 * read, and a slow flash takes seconds over them; give the CPU
+	 * away between nodes.
+	 */
+	cond_resched();
+
 	/* Calculate how many bytes were already checked */
 	ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
 	len = tn->csize;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 2/2] jffs2: reschedule between the data CRC checks of an inode

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] jffs2: missing Fixes: trailer for lost cond_resched() in readinode --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] jffs2: missing Fixes: trailer for lost cond_resched() in readinode
--

commit dc68a5ef42faf811ab61bd897a8b35c11291c24e
Author: Orgad Shaneh <[email protected]>

Quoted 10 lines

jffs2: reschedule between the data CRC checks of an inode

The first read of an inode checks the data CRC of each of its unchecked nodes, all in one call. On an Octeon board with an 8-bit NOR, a 46.6 MB file takes 13.8 s of kernel time there. Without kernel preemption, nothing else ran on that CPU in the meantime: an RT watchdog thread missed its deadline and the board rebooted. With this, the lookup takes as long, but the watchdog, the per-CPU workqueue and the other tasks of that CPU keep running.

Quoted 4 lines

jffs2_get_inode_nodes() already reschedules per node while it reads the headers; do the same before each data check.

[Severity: Low]
Prior to commit df8e96f39103 ("[JFFS2] Improve read_inode memory usage, v2."), jffs2_do_read_inode_internal() called cond_resched() on each node while checking data CRCs and building the fragtree, which was dropped when that loop moved into jffs2_build_inode_fragtree(). Should this include:

Fixes df8e96f39103 ("[JFFS2] Improve read_inode memory usage, v2.")
Signature · 5 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#3 lore raw

Re: [PATCH 2/2] jffs2: reschedule between the data CRC checks of an inode

Quoted 2 lines

Should this include: Fixes: df8e96f39103 ("[JFFS2] Improve read_inode memory usage, v2.")

Yes. Before that commit, the loop in jffs2_do_read_inode_internal() called cond_resched() before each node it added to the fragtree, data check included; the loop that replaced it in jffs2_build_inode_fragtree() has none.

Richard, can you add the tag when applying, or do you prefer a v2?

Fixes df8e96f39103 ("[JFFS2] Improve read_inode memory usage, v2.")