Skip to contentSkip to conversation

erofs-utils: mkfs: Implement 'dsunit' alignment on blobdev

PATCH In review Friendy Su 2 participants 4 messages Aug 23, 2025 – Aug 24, 2025 Library code

lore
Share & tools
lore

Friendy Su

author
#0 lore raw

PATCH v3 1/1 erofs-utils: mkfs: Implement 'dsunit' alignment on blobdev

Set proper 'dsunit' to let file body align on huge page on blobdev,

where 'dsunit' * 'blocksize' = huge page size (2M).

When do mmap() a file mounted with dax=always, aligning on huge page makes kernel map huge page(2M) per page fault exception, compared with mapping normal page(4K) per page fault.

This greatly improves mmap() performance by reducing times of page fault being triggered.

Considering deduplication, 'chunksize' should not be smaller than 'dsunit', then after dedupliation, still align on dsunit.

---
 lib/blobchunk.c  | 18 ++++++++++++++++++
 man/mkfs.erofs.1 | 15 +++++++++++++++
 mkfs/main.c      | 12 ++++++++++++
 3 files changed, 45 insertions(+)
lib/blobchunk.c +18 -0
index bbc69cf..69c70e9 100644
@@ -309,6 +309,24 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 	minextblks = BLK_ROUND_UP(sbi, inode->i_size);
 	interval_start = 0;
 
+	/*
+	 * dsunit <= chunksize, deduplication will not cause unalignment,
+	 * we can do align with confidence
+	 */
+	if (sbi->bmgr->dsunit > 1 &&
+	    sbi->bmgr->dsunit <= 1u << (chunkbits - sbi->blkszbits)) {
+		off_t off = lseek(blobfile, 0, SEEK_CUR);
+
+		off = roundup(off, sbi->bmgr->dsunit * erofs_blksiz(sbi));
+		if (lseek(blobfile, off, SEEK_SET) != off) {
+			ret = -errno;
+			erofs_err("lseek to blobdev 0x%llx error", off);
+			goto err;
+		}
+		erofs_dbg("Align /%s on block #%d (0x%llx)",
+			  erofs_fspath(inode->i_srcpath), erofs_blknr(sbi, off), off);
+	}
+
 	for (pos = 0; pos < inode->i_size; pos += len) {
 #ifdef SEEK_DATA
 		off_t offset = lseek(fd, pos + startoff, SEEK_DATA);
man/mkfs.erofs.1 +15 -0
index 63f7a2f..9075522 100644
@@ -168,6 +168,21 @@ the output filesystem, with no leading /.
 .TP
 .BI "\-\-dsunit=" #
 Align all data block addresses to multiples of #.
+
+If \fBdsunit\fR and \fBchunksize\fR are both set, \fBdsunit\fR will be ignored
+if it is bigger than \fBchunksize\fR.
+
+This is for keeping alignment after deduplication.
+If \fBdsunit\fR is bigger, it contains several chunks,
+
+E.g. \fBblock-size\fR=4096, \fBdsunit\fR=512 (2M), \fBchunksize\fR=4096
+
+Once 1 chunk is deduplicated, the chunks thereafter will not be aligned any
+longer. In order to achieve the best performance, recommend to set \fBdsunit\fR
+same as \fBchunksize\fR.
+
+E.g. \fBblock-size\fR=4096, \fBdsunit\fR=512 (2M), \fBchunksize\fR=$((4096*512))
+
 .TP
 .BI "\-\-exclude-path=" path
 Ignore file that matches the exact literal path.
mkfs/main.c +12 -0
index 30804d1..5ca098b 100644
@@ -1098,6 +1098,18 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		return -EINVAL;
 	}
 
+	/*
+	 * once align data on dsunit, in order to keep alignment after deduplication
+	 * chunksize should be equal to or bigger than dsunit.
+	 * if chunksize is smaller than dsunit, e.g. chunksize=4k, dsunit=2M,
+	 * once a chunk is deduplicated, all data thereafter will be unaligned.
+	 * so show warning msg here, then NOT do alignment when write file data.
+	 */
+	if (cfg.c_chunkbits && dsunit && 1u << (cfg.c_chunkbits - g_sbi.blkszbits) < dsunit) {
+		erofs_warn("chunksize %u bytes is smaller than dsunit %u blocks, ignore dsunit !",
+			   1u << cfg.c_chunkbits, dsunit);
+	}
+
 	if (pclustersize_packed) {
 		if (pclustersize_packed < erofs_blksiz(&g_sbi) ||
 		    pclustersize_packed % erofs_blksiz(&g_sbi)) {
Signature · 1 line
2.34.1
author Reviewed-by Yuezhang Mo Reviewed-by Daniel Palmer on v3 1/1
#1 lore raw

+ if (sbi->bmgr->dsunit > 1 &&
+ sbi->bmgr->dsunit <= 1u << (chunkbits - sbi->blkszbits)) {

I remained "sbi->bmgr->dsunit > 1" since it is 0 as default.

-----Original Message-----
From: Su, Friendy <[email protected]>
Sent: Saturday, August 23, 2025 4:35 PM
To: [email protected]
Cc: Su, Friendy <[email protected]>; Mo, Yuezhang <[email protected]>; Palmer, Daniel (SGC) <[email protected]>
Subject: [PATCH v3] erofs-utils: mkfs: Implement 'dsunit' alignment on blobdev

Set proper 'dsunit' to let file body align on huge page on blobdev,

where 'dsunit' * 'blocksize' = huge page size (2M).

When do mmap() a file mounted with dax=always, aligning on huge page makes kernel map huge page(2M) per page fault exception, compared with mapping normal page(4K) per page fault.

This greatly improves mmap() performance by reducing times of page fault being triggered.

Considering deduplication, 'chunksize' should not be smaller than 'dsunit', then after dedupliation, still align on dsunit.

---
 lib/blobchunk.c  | 18 ++++++++++++++++++
 man/mkfs.erofs.1 | 15 +++++++++++++++
 mkfs/main.c      | 12 ++++++++++++
 3 files changed, 45 insertions(+)
lib/blobchunk.c +18 -0
index bbc69cf..69c70e9 100644
@@ -309,6 +309,24 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
 	minextblks = BLK_ROUND_UP(sbi, inode->i_size);
 	interval_start = 0;
 
+	/*
+	 * dsunit <= chunksize, deduplication will not cause unalignment,
+	 * we can do align with confidence
+	 */
+	if (sbi->bmgr->dsunit > 1 &&
+	    sbi->bmgr->dsunit <= 1u << (chunkbits - sbi->blkszbits)) {
+		off_t off = lseek(blobfile, 0, SEEK_CUR);
+
+		off = roundup(off, sbi->bmgr->dsunit * erofs_blksiz(sbi));
+		if (lseek(blobfile, off, SEEK_SET) != off) {
+			ret = -errno;
+			erofs_err("lseek to blobdev 0x%llx error", off);
+			goto err;
+		}
+		erofs_dbg("Align /%s on block #%d (0x%llx)",
+			  erofs_fspath(inode->i_srcpath), erofs_blknr(sbi, off), off);
+	}
+
 	for (pos = 0; pos < inode->i_size; pos += len) {
 #ifdef SEEK_DATA
 		off_t offset = lseek(fd, pos + startoff, SEEK_DATA);
man/mkfs.erofs.1 +15 -0
index 63f7a2f..9075522 100644
@@ -168,6 +168,21 @@ the output filesystem, with no leading /.
 .TP
 .BI "\-\-dsunit=" #
 Align all data block addresses to multiples of #.
+
+If \fBdsunit\fR and \fBchunksize\fR are both set, \fBdsunit\fR will be ignored
+if it is bigger than \fBchunksize\fR.
+
+This is for keeping alignment after deduplication.
+If \fBdsunit\fR is bigger, it contains several chunks,
+
+E.g. \fBblock-size\fR=4096, \fBdsunit\fR=512 (2M), \fBchunksize\fR=4096
+
+Once 1 chunk is deduplicated, the chunks thereafter will not be aligned any
+longer. In order to achieve the best performance, recommend to set \fBdsunit\fR
+same as \fBchunksize\fR.
+
+E.g. \fBblock-size\fR=4096, \fBdsunit\fR=512 (2M), \fBchunksize\fR=$((4096*512))
+
 .TP
 .BI "\-\-exclude-path=" path
 Ignore file that matches the exact literal path.
mkfs/main.c +12 -0
index 30804d1..5ca098b 100644
@@ -1098,6 +1098,18 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		return -EINVAL;
 	}
 
+	/*
+	 * once align data on dsunit, in order to keep alignment after deduplication
+	 * chunksize should be equal to or bigger than dsunit.
+	 * if chunksize is smaller than dsunit, e.g. chunksize=4k, dsunit=2M,
+	 * once a chunk is deduplicated, all data thereafter will be unaligned.
+	 * so show warning msg here, then NOT do alignment when write file data.
+	 */
+	if (cfg.c_chunkbits && dsunit && 1u << (cfg.c_chunkbits - g_sbi.blkszbits) < dsunit) {
+		erofs_warn("chunksize %u bytes is smaller than dsunit %u blocks, ignore dsunit !",
+			   1u << cfg.c_chunkbits, dsunit);
+	}
+
 	if (pclustersize_packed) {
 		if (pclustersize_packed < erofs_blksiz(&g_sbi) ||
 		    pclustersize_packed % erofs_blksiz(&g_sbi)) {
Signature · 1 line
2.34.1
#2 lore raw
In reply to Friendy Su
Quoted 28 lines · Friendy Su

Set proper 'dsunit' to let file body align on huge page on blobdev,

where 'dsunit' * 'blocksize' = huge page size (2M).

When do mmap() a file mounted with dax=always, aligning on huge page makes kernel map huge page(2M) per page fault exception, compared with mapping normal page(4K) per page fault.

This greatly improves mmap() performance by reducing times of page fault being triggered.

Considering deduplication, 'chunksize' should not be smaller than 'dsunit', then after dedupliation, still align on dsunit.

---
  lib/blobchunk.c  | 18 ++++++++++++++++++
  man/mkfs.erofs.1 | 15 +++++++++++++++
  mkfs/main.c      | 12 ++++++++++++
  3 files changed, 45 insertions(+)
lib/blobchunk.c +0 -0
index bbc69cf..69c70e9 100644

lib/blobchunk.c int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd, · line 317

Quoted 9 lines
lib/blobchunk.c +6 -0
@@ -309,6 +309,24 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
  	minextblks = BLK_ROUND_UP(sbi, inode->i_size);
  	interval_start = 0;
 
+	/*
+	 * dsunit <= chunksize, deduplication will not cause unalignment,
+	 * we can do align with confidence
+	 */
+	if (sbi->bmgr->dsunit > 1 &&
+	    sbi->bmgr->dsunit <= 1u << (chunkbits - sbi->blkszbits)) {

Sigh, I meant (sbi->bmgr->dsunit >= 1u << (chunkbits - sbi->blkszbits))

Let's ignore sbi->bmgr->dsunit < 1u << (chunkbits - sbi->blkszbits).

Thanks,
Gao Xiang

#3 lore raw

Hi Friendy,

Quoted 2 lines · Gao Xiang

...

Quoted 22 lines
Quoted 11 lines

---
  lib/blobchunk.c  | 18 ++++++++++++++++++
  man/mkfs.erofs.1 | 15 +++++++++++++++
  mkfs/main.c      | 12 ++++++++++++

  3 files changed, 45 insertions(+)
lib/blobchunk.c +0 -0
index bbc69cf..69c70e9 100644

lib/blobchunk.c int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd, · line 316

Quoted 8 lines
lib/blobchunk.c +6 -0
@@ -309,6 +309,24 @@ int erofs_blob_write_chunked_file(struct erofs_inode *inode, int fd,
       minextblks = BLK_ROUND_UP(sbi, inode->i_size);
       interval_start = 0;
+    /*
+     * dsunit <= chunksize, deduplication will not cause unalignment,
+     * we can do align with confidence
+     */
+    if (sbi->bmgr->dsunit > 1 &&
+        sbi->bmgr->dsunit <= 1u << (chunkbits - sbi->blkszbits)) {

Sigh, I meant (sbi->bmgr->dsunit >= 1u << (chunkbits - sbi->blkszbits))

Quoted 1 line

Let's ignore sbi->bmgr->dsunit < 1u << (chunkbits - sbi->blkszbits).

Sorry, your patch is absolutely correct.

I was looking at this in rush, sorry for the noise.

I will submit this later soon. (busy in other personal stuffs this weekend.)

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Gao Xiang