Skip to contentSkip to conversation

erofs-utils: lib: use xxh64() for faster filtering first for dedupe

PATCH New Gao Xiang 1 participant 4 messages Sep 22, 2023 – Mar 7, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: lib: use xxh64() for faster filtering first for dedupe

Let's check if xxh64 equals when rolling back on global compressed deduplication.

As a result, it could decrease time by 26.4% (6m57.990s -> 5m7.755s) on a dataset with "-Ededupe -C8192".

---
 include/erofs/defs.h   |  5 +++
 include/erofs/xxhash.h | 27 -------------
 lib/Makefile.am        |  4 +-
 lib/dedupe.c           |  9 +++++
 lib/xattr.c            |  2 +-
 lib/xxhash.c           | 92 +++++++++++++++++++++++++++++++++++++++++-
 lib/xxhash.h           | 40 ++++++++++++++++++
 7 files changed, 147 insertions(+), 32 deletions(-)
 delete mode 100644 include/erofs/xxhash.h
 create mode 100644 lib/xxhash.h
include/erofs/defs.h +5 -0
index fefa7e7..e7384a1 100644
@@ -204,6 +204,11 @@ static inline void put_unaligned_le32(u32 val, void *p)
 	__put_unaligned_t(__le32, cpu_to_le32(val), p);
 }
 
+static inline u32 get_unaligned_le64(const void *p)
+{
+	return le64_to_cpu(__get_unaligned_t(__le64, p));
+}
+
 /**
  * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
  * @n - parameter
include/erofs/xxhash.h deleted +0 -27
deleted file mode 100644
index 5441209..0000000
@@ -1,27 +0,0 @@
-/* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0+ */
-#ifndef __EROFS_XXHASH_H
-#define __EROFS_XXHASH_H
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-#include <stdint.h>
-
-/**
- * xxh32() - calculate the 32-bit hash of the input with a given seed.
- *
- * @input:  The data to hash.
- * @length: The length of the data to hash.
- * @seed:   The seed can be used to alter the result predictably.
- *
- * Return:  The 32-bit hash of the data.
- */
-uint32_t xxh32(const void *input, size_t length, uint32_t seed);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
lib/Makefile.am +2 -2
index 483d410..470e095 100644
@@ -25,9 +25,9 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/include/erofs/xattr.h \
       $(top_srcdir)/include/erofs/compress_hints.h \
       $(top_srcdir)/include/erofs/fragments.h \
-      $(top_srcdir)/include/erofs/xxhash.h \
       $(top_srcdir)/include/erofs/rebuild.h \
-      $(top_srcdir)/lib/liberofs_private.h
+      $(top_srcdir)/lib/liberofs_private.h \
+      $(top_srcdir)/lib/xxhash.h
 
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
lib/dedupe.c +9 -0
index 17da452..2f86f8d 100644
@@ -6,6 +6,7 @@
 #include "erofs/print.h"
 #include "rb_tree.h"
 #include "rolling_hash.h"
+#include "xxhash.h"
 #include "sha256.h"
 
 unsigned long erofs_memcmp2(const u8 *s1, const u8 *s2,
@@ -66,6 +67,7 @@ static struct rb_tree *dedupe_tree, *dedupe_subtree;
 struct z_erofs_dedupe_item {
 	long long	hash;
 	u8		prefix_sha256[32];
+	u64		prefix_xxh64;
 
 	erofs_blk_t	compressed_blkaddr;
 	unsigned int	compressed_blks;
@@ -102,6 +104,7 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 	for (; cur >= ctx->start; --cur) {
 		struct z_erofs_dedupe_item *e;
 		unsigned int extra;
+		u64 xxh64_csum;
 		u8 sha256[32];
 
 		if (initial) {
@@ -120,6 +123,10 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 				continue;
 		}
 
+		xxh64_csum = xxh64(cur, window_size, 0);
+		if (e->prefix_xxh64 != xxh64_csum)
+			continue;
+
 		erofs_sha256(cur, window_size, sha256);
 		if (memcmp(sha256, e->prefix_sha256, sizeof(sha256)))
 			continue;
@@ -155,6 +162,8 @@ int z_erofs_dedupe_insert(struct z_erofs_inmem_extent *e,
 
 	di->original_length = e->length;
 	erofs_sha256(original_data, window_size, di->prefix_sha256);
+
+	di->prefix_xxh64 = xxh64(original_data, window_size, 0);
 	di->hash = erofs_rolling_hash_init(original_data,
 			window_size, true);
 	memcpy(di->extra_data, original_data + window_size,
lib/xattr.c +1 -1
index 6c8ebf4..77c8c3a 100644
@@ -18,7 +18,7 @@
 #include "erofs/cache.h"
 #include "erofs/io.h"
 #include "erofs/fragments.h"
-#include "erofs/xxhash.h"
+#include "xxhash.h"
 #include "liberofs_private.h"
 
 #ifndef XATTR_SYSTEM_PREFIX
lib/xxhash.c +90 -2

113 lines not on this page · raw message

lib/xxhash.h new file +40 -0
new file mode 100644
index 0000000..723c3a5
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0+ */
+#ifndef __EROFS_LIB_XXHASH_H
+#define __EROFS_LIB_XXHASH_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <stdint.h>
+
+/*
+ * xxh32() - calculate the 32-bit hash of the input with a given seed.
+ *
+ * @input:  The data to hash.
+ * @length: The length of the data to hash.
+ * @seed:   The seed can be used to alter the result predictably.
+ *
+ * Return:  The 32-bit hash of the data.
+ */
+uint32_t xxh32(const void *input, size_t length, uint32_t seed);
+
+/*
+ * xxh64() - calculate the 64-bit hash of the input with a given seed.
+ *
+ * @input:  The data to hash.
+ * @length: The length of the data to hash.
+ * @seed:   The seed can be used to alter the result predictably.
+ *
+ * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
+ *
+ * Return:  The 64-bit hash of the data.
+ */
+uint64_t xxh64(const void *input, const size_t len, const uint64_t seed);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 2/3 erofs-utils: switch dedupe_{sub,}tree[] to a hash table

This rb-tree implementation is too slow and there is no benefits of it.

As a result, it could decrease time by 81.1% (5m7.755s -> 0m58.255s) with the same dataset, sigh.

---
 lib/Makefile.am |   2 +-
 lib/dedupe.c    | 116 +++++------
 lib/rb_tree.c   | 512 ------------------------------------------------
 lib/rb_tree.h   | 104 ----------
 4 files changed, 62 insertions(+), 672 deletions(-)
 delete mode 100644 lib/rb_tree.c
 delete mode 100644 lib/rb_tree.h
lib/Makefile.am +1 -1
index 470e095..54b9c9c 100644
@@ -33,7 +33,7 @@ noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
 		      compress_hints.c hashmap.c sha256.c blobchunk.c dir.c \
-		      fragments.c rb_tree.c dedupe.c uuid_unparse.c uuid.c tar.c \
+		      fragments.c dedupe.c uuid_unparse.c uuid.c tar.c \
 		      block_list.c xxhash.c rebuild.c diskbuf.c
 
 liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include
lib/dedupe.c +61 -55

192 lines not on this page · raw message

lib/rb_tree.c deleted +0 -512

512 lines not on this page · raw message

lib/rb_tree.h deleted +0 -104

104 lines not on this page · raw message

Signature · 1 line
2.39.3
author
#2 lore raw

PATCH 3/3 erofs-utils: lib: drop prefix_sha256 digests

In reply to Gao Xiang

People care more about speed when building. Later, we could also try to use file-backed memory to avoid memory pressure.

As a result, it could decrease time by 50% (0m58.255s -> 0m29.120s).

---
 lib/dedupe.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
lib/dedupe.c +5 -10
index 4b0edbf..19a1c8d 100644
@@ -69,7 +69,6 @@ struct z_erofs_dedupe_item {
 	struct list_head list;
 	struct z_erofs_dedupe_item *chain;
 	long long	hash;
-	u8		prefix_sha256[32];
 	u64		prefix_xxh64;
 
 	erofs_blk_t	compressed_blkaddr;
@@ -77,7 +76,7 @@ struct z_erofs_dedupe_item {
 
 	int		original_length;
 	bool		partial, raw;
-	u8		extra_data[];
+	u8		payload[];
 };
 
 int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
@@ -101,7 +100,6 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 
 		unsigned int extra = 0;
 		u64 xxh64_csum;
-		u8 sha256[32];
 
 		if (initial) {
 			/* initial try */
@@ -127,13 +125,12 @@ int z_erofs_dedupe_match(struct z_erofs_dedupe_ctx *ctx)
 		if (&e->list == p)
 			continue;
 
-		erofs_sha256(cur, window_size, sha256);
-		if (memcmp(sha256, e->prefix_sha256, sizeof(sha256)))
+		if (memcmp(cur, e->payload, window_size))
 			continue;
 
 		extra = min_t(unsigned int, ctx->end - cur - window_size,
 			      e->original_length - window_size);
-		extra = erofs_memcmp2(cur + window_size, e->extra_data, extra);
+		extra = erofs_memcmp2(cur + window_size, e->payload + window_size, extra);
 		if (window_size + extra <= ctx->cur - cur)
 			continue;
 		ctx->cur = cur;
@@ -157,18 +154,16 @@ int z_erofs_dedupe_insert(struct z_erofs_inmem_extent *e,
 	if (!window_size || e->length < window_size)
 		return 0;
 
-	di = malloc(sizeof(*di) + e->length - window_size);
+	di = malloc(sizeof(*di) + e->length);
 	if (!di)
 		return -ENOMEM;
 
 	di->original_length = e->length;
-	erofs_sha256(original_data, window_size, di->prefix_sha256);
 
 	di->prefix_xxh64 = xxh64(original_data, window_size, 0);
 	di->hash = erofs_rolling_hash_init(original_data,
 			window_size, true);
-	memcpy(di->extra_data, original_data + window_size,
-	       e->length - window_size);
+	memcpy(di->payload, original_data, e->length);
 	di->compressed_blkaddr = e->blkaddr;
 	di->compressed_blks = e->compressedblks;
 	di->partial = e->partial;
Signature · 1 line
2.39.3
author
#3 lore raw

PATCH v2 1/1 erofs-utils: lib: error out if fragment_off is crafted

Found in some fuzzed images.

Fixes f511cfbbc0da ("erofs-utils: introduce fragment cache")
---
 lib/fragments.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/fragments.c +5 -0
index 2f5fbf9..05bbf0d 100644
@@ -524,6 +524,11 @@ int erofs_packedfile_read(struct erofs_sb_info *sbi,
 			erofs_blk_t bnr = erofs_blknr(sbi, pos);
 			bool uptodate;
 
+			if (__erofs_unlikely(bnr > (epi->uptodate_size << 3))) {
+				erofs_err("packed inode EOF exceeded @ %llu",
+					  pos | 0ULL);
+				return -EFSCORRUPTED;
+			}
 			map.m_la = round_down(pos, bsz);
 			len = min_t(erofs_off_t, bsz - (pos & (bsz - 1)),
 				    end - pos);
Signature · 1 line
2.43.5