Skip to contentSkip to conversation

RFC: erofs: memory-backed mount for non-page-aligned ranges

RFC PATCH In review Alberto Ruiz 3 participants 5 messages Jun 18 – Jun 22 EROFS file system

lore
Share & tools
lore
#0 lore raw

cover RFC: erofs: memory-backed mount for non-page-aligned ranges

This series adds a memory-backed ("memback") mode to EROFS that allows mounting an EROFS image directly from a kernel memory region without going through the block layer.

I am sending this as an RFC to get EROFS maintainer feedback on this approach before building the full initramfs series on top of it. The memback mode may also be useful for other use cases beyond initramfs where EROFS images need to be served directly from memory, though it may need further hardening for more general use cases.

Previous review feedback on the initramfs patches raised concerns about the block layer dependency — this series addresses that by providing a direct memory-backed path. It also adds support for non-page-aligned memory ranges, which is the common case for initramfs images packed at arbitrary cpio boundaries. Supporting non-page-aligned ranges is essential for true zero-copy access — requiring page alignment would force a copy into an aligned buffer before the data can be used, defeating the purpose.

The full initramfs integration (init/ changes) that wires up the EROFS initrd detection and layered mount is available for reference here[0]. It implements support for interleaved cpio and EROFS layers, it supports cpio CPU microcode update prefix, and uses overlayfs+tmpfs for write support.

This series was developed with the assistance of Claude Opus 4.6. All patches carry an Assisted-by tag. I am providing the kunit testing and I have tested this change against the initramfs series to boot an EROFS initramfs. I welcome feedback on the approach and implementation.

Patch 1 adds the core memback implementation.
Patch 2 adds a KUnit test that mounts a compressed EROFS image at a non-page-aligned offset and verifies decompressed file contents.

[0] https://github.com/aruiz/linux/tree/wip/erofs-initramfs-memback

---
Alberto Ruiz (2):
erofs: add memory-backed mode for non-page-aligned mount
erofs: add KUnit test for memory-backed compressed mount

 fs/erofs/Kconfig        |  11 ++++
 fs/erofs/Makefile       |   2 +
 fs/erofs/data.c         |  33 +++++++++-
 fs/erofs/internal.h     |  53 +++++++++++----
 fs/erofs/memback.c      | 169 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/memback_test.c | 151 ++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/super.c        |  38 +++++++++--
 fs/erofs/zdata.c        |  16 +++--
 8 files changed, 447 insertions(+), 26 deletions(-)

---
base-commit: 6b5a2b7d9bc156e505f09e698d85d6a1547c1206
change-id: 20260617-erofs-memback-0ae2448ba2cc

Best regards,

Signature · 1 line
Alberto Ruiz <[email protected]>
#1 lore raw

RFC PATCH 1/2 erofs: add memory-backed mode for non-page-aligned mount

Add a new "memback" backing mode to EROFS that reads directly from a contiguous kernel memory region without going through the block layer.

The metadata path (erofs_bread) achieves true zero-copy by using virt_to_page() + kmap_local_page() to access pages directly, bypassing the page cache entirely.

The file data path uses new address_space_operations (erofs_memback_aops) that resolve logical-to-physical offsets via erofs_map_blocks() and memcpy from the backing memory into file page cache folios -- one copy, same as the current block device approach but without any block layer involvement.

For compressed data, erofs_memback_submit_bio() walks bio segments and copies compressed data from memory, following the same dispatch pattern used by the existing fileio and fscache modes.

Non-page-aligned memory regions are handled naturally via offset_in_page() for metadata and byte-granular memcpy for data, allowing EROFS images at arbitrary offsets regardless of page size.

A kernel-internal API erofs_memback_set_pending(data, size) stores the memory region parameters in static globals which erofs_fc_get_tree() consumes on the next mount via get_tree_nodev(), allowing early boot code to trigger a standard EROFS mount without exposing kernel pointers as mount option strings.

Assisted-by Claude:claude-opus-4-6
---
 fs/erofs/Makefile   |   1 +
 fs/erofs/data.c     |  33 +++++++++-
 fs/erofs/internal.h |  53 +++++++++++-----
 fs/erofs/memback.c  | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/super.c    |  38 ++++++++++--
 fs/erofs/zdata.c    |  16 +++--
 6 files changed, 284 insertions(+), 26 deletions(-)
fs/erofs/Makefile +1 -0
index a80e1762b607..8f3b73835328 100644
@@ -10,4 +10,5 @@ erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o
 erofs-$(CONFIG_EROFS_FS_ZIP_ACCEL) += decompressor_crypto.o
 erofs-$(CONFIG_EROFS_FS_BACKED_BY_FILE) += fileio.o
 erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
+erofs-y += memback.o
 erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += ishare.o
fs/erofs/data.c +31 -2
index 44da21c9d777..26fff07df5bd 100644
@@ -22,7 +22,8 @@ void erofs_put_metabuf(struct erofs_buf *buf)
 	if (!buf->page)
 		return;
 	erofs_unmap_metabuf(buf);
-	folio_put(page_folio(buf->page));
+	if (!buf->memback)
+		folio_put(page_folio(buf->page));
 	buf->page = NULL;
 }
 
@@ -45,6 +46,25 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 			return ERR_PTR(err);
 	}
 
+	if (buf->memback) {
+		void *addr = (char *)buf->mapping + offset;
+		struct page *page;
+
+		if (offset >= buf->memback_size)
+			return ERR_PTR(-EFSCORRUPTED);
+
+		page = virt_to_page(addr);
+		if (buf->page != page) {
+			erofs_unmap_metabuf(buf);
+			buf->page = page;
+		}
+		if (!need_kmap)
+			return NULL;
+		if (!buf->base)
+			buf->base = kmap_local_page(buf->page);
+		return buf->base + offset_in_page(addr);
+	}
+
 	if (buf->page) {
 		folio = page_folio(buf->page);
 		if (folio_file_page(folio, index) != buf->page)
@@ -70,15 +90,24 @@ int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb,
 	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
 	buf->file = NULL;
+	buf->memback = false;
 	if (in_metabox) {
 		if (unlikely(!sbi->metabox_inode))
 			return -EFSCORRUPTED;
 		buf->mapping = sbi->metabox_inode->i_mapping;
 		return 0;
 	}
+	if (erofs_is_memback_mode(sbi)) {
+		buf->memback = true;
+		buf->memback_size = sbi->memback_size;
+		buf->off = 0;
+		/* Reuse the mapping pointer to carry the base address */
+		buf->mapping = (struct address_space *)sbi->memback_data;
+		return 0;
+	}
 	buf->off = sbi->dif0.fsoff;
 	if (erofs_is_fileio_mode(sbi)) {
-		buf->file = sbi->dif0.file;	/* some fs like FUSE needs it */
+		buf->file = sbi->dif0.file; /* some fs like FUSE needs it */
 		buf->mapping = buf->file->f_mapping;
 	} else if (erofs_is_fscache_mode(sb))
 		buf->mapping = sbi->dif0.fscache->inode->i_mapping;
fs/erofs/internal.h +39 -14

104 lines not on this page · raw message

fs/erofs/memback.c new file +169 -0

169 lines not on this page · raw message

fs/erofs/super.c +33 -5
index 802add6652fd..724389d79aac 100644
@@ -625,9 +625,8 @@ static void erofs_set_sysfs_name(struct super_block *sb)
 					     sbi->fsid);
 	else if (sbi->fsid)
 		super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
-	else if (erofs_is_fileio_mode(sbi))
-		super_set_sysfs_name_generic(sb, "%s",
-					     bdi_dev_name(sb->s_bdi));
+	else if (erofs_is_memback_mode(sbi) || erofs_is_fileio_mode(sbi))
+		super_set_sysfs_name_generic(sb, "%s", bdi_dev_name(sb->s_bdi));
 	else
 		super_set_sysfs_name_id(sb);
 }
@@ -708,7 +707,7 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 			return -EINVAL;
 		}
 
-		if (erofs_is_fileio_mode(sbi)) {
+		if (erofs_is_memback_mode(sbi) || erofs_is_fileio_mode(sbi)) {
 			sb->s_blocksize = 1 << sbi->blkszbits;
 			sb->s_blocksize_bits = sbi->blkszbits;
 		} else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
@@ -791,11 +790,39 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 	return 0;
 }
 
+/*
+ * Pending memback parameters, consumed during erofs_fc_get_tree().
+ * Only used by single-threaded __init code, no synchronization needed.
+ */
+static void *erofs_memback_pending_data;
+static unsigned long erofs_memback_pending_size;
+
+void __init erofs_memback_set_pending(void *data, unsigned long size)
+{
+	erofs_memback_pending_data = data;
+	erofs_memback_pending_size = size;
+}
+
+static void erofs_memback_consume_pending(struct erofs_sb_info *sbi)
+{
+	sbi->memback_data = erofs_memback_pending_data;
+	sbi->memback_size = erofs_memback_pending_size;
+	erofs_memback_pending_data = NULL;
+	erofs_memback_pending_size = 0;
+}
+
 static int erofs_fc_get_tree(struct fs_context *fc)
 {
 	struct erofs_sb_info *sbi = fc->s_fs_info;
 	int ret;
 
+	if (erofs_memback_pending_data) {
+		erofs_memback_consume_pending(sbi);
+		return get_tree_nodev(fc, erofs_fc_fill_super);
+	}
+	if (erofs_is_memback_mode(sbi))
+		return get_tree_nodev(fc, erofs_fc_fill_super);
+
 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
 		return get_tree_nodev(fc, erofs_fc_fill_super);
 
@@ -928,7 +955,8 @@ static void erofs_kill_sb(struct super_block *sb)
 {
 	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
-	if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
+	if (erofs_is_memback_mode(sbi) ||
+	    (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
 	    sbi->dif0.file)
 		kill_anon_super(sb);
 	else
fs/erofs/zdata.c +11 -5
index c6240dccbb0f..af4ddb492660 100644
@@ -1709,10 +1709,12 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,
 			       sb->s_blocksize);
 		do {
 			bvec.bv_page = NULL;
-			if (bio && (cur != last_pa ||
-				    bio->bi_bdev != mdev.m_bdev)) {
+			if (bio &&
+			    (cur != last_pa || bio->bi_bdev != mdev.m_bdev)) {
 drain_io:
-				if (erofs_is_fileio_mode(EROFS_SB(sb)))
+				if (erofs_is_memback_mode(EROFS_SB(sb)))
+					erofs_memback_submit_bio(bio);
+				else if (erofs_is_fileio_mode(EROFS_SB(sb)))
 					erofs_fileio_submit_bio(bio);
 				else if (erofs_is_fscache_mode(sb))
 					erofs_fscache_submit_bio(bio);
@@ -1742,7 +1744,9 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,
 			}
 
 			if (!bio) {
-				if (erofs_is_fileio_mode(EROFS_SB(sb)))
+				if (erofs_is_memback_mode(EROFS_SB(sb)))
+					bio = erofs_memback_bio_alloc(&mdev);
+				else if (erofs_is_fileio_mode(EROFS_SB(sb)))
 					bio = erofs_fileio_bio_alloc(&mdev);
 				else if (erofs_is_fscache_mode(sb))
 					bio = erofs_fscache_bio_alloc(&mdev);
@@ -1772,7 +1776,9 @@ static void z_erofs_submit_queue(struct z_erofs_frontend *f,
 	} while (next != Z_EROFS_PCLUSTER_TAIL);
 
 	if (bio) {
-		if (erofs_is_fileio_mode(EROFS_SB(sb)))
+		if (erofs_is_memback_mode(EROFS_SB(sb)))
+			erofs_memback_submit_bio(bio);
+		else if (erofs_is_fileio_mode(EROFS_SB(sb)))
 			erofs_fileio_submit_bio(bio);
 		else if (erofs_is_fscache_mode(sb))
 			erofs_fscache_submit_bio(bio);
Signature · 1 line
2.53.0
#2 lore raw

RFC PATCH 2/2 erofs: add KUnit test for memory-backed compressed mount

In reply to Alberto Ruiz

Add a KUnit test that exercises the EROFS memback mount path with a compressed (LZ4) image. The test embeds a minimal 8 KiB EROFS image containing a single file with known content ("The quick brown fox. " repeated 250 times), mounts it via erofs_memback_set_pending(), reads back the decompressed file data, and verifies it byte-for-byte.

Since erofs_memback_set_pending() is __init, the test uses kunit_test_init_section_suites() so it runs during kernel init.

Assisted-by Claude:claude-opus-4-6
---
 fs/erofs/Kconfig        |  11 ++++
 fs/erofs/Makefile       |   1 +
 fs/erofs/memback_test.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 163 insertions(+)
fs/erofs/Kconfig +11 -0
index 97c48ebe8458..adf26e67924c 100644
@@ -213,3 +213,14 @@ config EROFS_FS_PAGE_CACHE_SHARE
 	  content fingerprints on the same machine.
 
 	  If unsure, say N.
+
+config EROFS_FS_MEMBACK_KUNIT_TEST
+	bool "Test EROFS memory-backed mount" if !KUNIT_ALL_TESTS
+	depends on EROFS_FS=y && EROFS_FS_ZIP && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  KUnit test for the EROFS memory-backed mount path. Embeds a small
+	  LZ4-compressed EROFS image and verifies that it can be mounted and
+	  read via the memback interface during kernel init.
+
+	  Say N unless you are running KUnit tests.
fs/erofs/Makefile +1 -0
index 8f3b73835328..b98c95e96e85 100644
@@ -12,3 +12,4 @@ erofs-$(CONFIG_EROFS_FS_BACKED_BY_FILE) += fileio.o
 erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
 erofs-y += memback.o
 erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += ishare.o
+obj-$(CONFIG_EROFS_FS_MEMBACK_KUNIT_TEST) += memback_test.o
fs/erofs/memback_test.c new file +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH RFC 2/2] erofs: add KUnit test for memory-backed compressed mount

kernel test robot noticed the following build warnings:

Hi Alberto,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on 6b5a2b7d9bc156e505f09e698d85d6a1547c1206]

url:    https://github.com/intel-lab-lkp/linux/commits/Alberto-Ruiz-via-B4-Relay/erofs-add-memory-backed-mode-for-non-page-aligned-mount/20260622-162856
base:   6b5a2b7d9bc156e505f09e698d85d6a1547c1206
patch link:    https://lore.kernel.org/r/20260618-erofs-memback-v1-2-5aa7006a241a%40redhat.com
patch subject: [PATCH RFC 2/2] erofs: add KUnit test for memory-backed compressed mount
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260622/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260622/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

fs/erofs/memback.c: In function 'erofs_memback_scan_folio':

>> fs/erofs/memback.c:80:61: warning: variable 'attached' set but not used [-Wunused-but-set-variable]
      80 |         unsigned int cur = 0, end = folio_size(folio), len, attached = 0;
         |                                                             ^~~~~~~~

vim +/attached +80 fs/erofs/memback.c

    74	
    75	static int erofs_memback_scan_folio(struct erofs_memback_io *io,
    76					    struct inode *inode, struct folio *folio)
    77	{
    78		struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
    79		struct erofs_map_blocks *map = &io->map;
  > 80		unsigned int cur = 0, end = folio_size(folio), len, attached = 0;
    81		loff_t pos = folio_pos(folio), ofs;
    82		int err = 0;
    83	
    84		erofs_onlinefolio_init(folio);
    85		while (cur < end) {
    86			if (!in_range(pos + cur, map->m_la, map->m_llen)) {
    87				map->m_la = pos + cur;
    88				map->m_llen = end - cur;
    89				err = erofs_map_blocks(inode, map);
    90				if (err)
    91					break;
    92			}
    93	
    94			ofs = pos + cur - map->m_la;
    95			len = min_t(loff_t, map->m_llen - ofs, end - cur);
    96			if (map->m_flags & EROFS_MAP_META) {
    97				struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
    98				void *src;
    99	

100 src = erofs_read_metabuf(&buf, inode->i_sb,
101 map->m_pa + ofs,
102 erofs_inode_in_metabox(inode));
103 if (IS_ERR(src)) {
104 err = PTR_ERR(src);
105 break;
106 }
107 memcpy_to_folio(folio, cur, src, len);
108 erofs_put_metabuf(&buf);
109 } else if (!(map->m_flags & EROFS_MAP_MAPPED)) {
110 folio_zero_segment(folio, cur, cur + len);
111 attached = 0;
112 } else {
113 loff_t pa = map->m_pa + ofs;
114
115 if (pa + len > sbi->memback_size) {
116 err = -EFSCORRUPTED;
117 break;
118 }
119 memcpy_to_folio(folio, cur,
120 (char *)sbi->memback_data + pa, len);
121 attached = 1;
122 }
123 cur += len;
124 }
125 erofs_onlinefolio_end(folio, err, false);
126 return err;
127 }
128

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#4 lore raw
In reply to Alberto Ruiz

Hi Alberto,

Quoted 7 lines · On 2026/6/19 00:13, Alberto Ruiz via B4 Relay wrote:

This series adds a memory-backed ("memback") mode to EROFS that allows mounting an EROFS image directly from a kernel memory region without going through the block layer.

I am sending this as an RFC to get EROFS maintainer feedback on this approach before building the full initramfs series on top of it. The memback mode may also be useful for other use cases beyond

First, thanks for your interest and efforts. On my side I'm glad if EROFS can be used for the initramfs use case (for many years!).

Quoted 6 lines

initramfs where EROFS images need to be served directly from memory, though it may need further hardening for more general use cases.

Previous review feedback on the initramfs patches raised concerns about the block layer dependency — this series addresses that by

Could you have a pointer where "concerns about the block layer dependency" comes from? I don't notice that on the mailing list.

Quoted 1 line

providing a direct memory-backed path. It also adds support for

But I think EROFS subsystem can not decide itself if we could land this functionality or not, in any case we should have a discussion with VFS/MM folks first to know if the rough direction is acceptable or not. And we also need to know what's the concerns before we draft a version.

There may have some barriers on my side to land this feature:

- erofs_memback_set_pending() can be used to set an arbitary kernel virt address. I don't think it's a good direction to be honest, for example, previously cramfs gets some arbitary address using mtd_point() and implement XIP feature but I think it's quite outdated.

Yes, I've seen erofs_memback_set_pending() can only be used for specific cases since it has __init annotation, but it also limits _the other potential use cases_.

- currently modern memory backends (memory devices like PMEM and CXL) all use Linux DAX infrastructure, which means we shouldn't bypass the DAX infrastructure to operate arbitary memory address directly.

That is my own opinion at least, but if MM/FS folks think EROFS can work on arbitary kernel virt address instead, I'm fine, but I think it's highly impossible.

So at least on this part, I wonder if we should enhance ramdax (drivers/nvdimm/ramdax.c) to support initramfs use cases and make EROFS to use ramdax + DAX infrastrure to mount initramfs instead.

Quoted 2 lines

non-page-aligned memory ranges, which is the common case for initramfs images packed at arbitrary cpio boundaries. Supporting

That is another issue I would like to discuss:

If EROFS filesystem data is page aligned, we can just use the initramfs memory directly and so that no page cache is needed.

And in that cases, no double cache anymore (see "ramfs and
ramdisk" section in https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt).

Quoted 1 line

non-page-aligned ranges is essential for true zero-copy access —

I guess that is not true: I've looked the code, basically the code uses the page cache (double caching), and the unaligned data will be copied into page cache (aligned data) first.

Since mmap use cases need page aligned buffers, and in practice of the linux kernel, it will be page cache or DAX. So in order to do that, unaligned initramfs needs do double caching and an extra copy at least to fulfill the requirement.

Quoted 2 lines

requiring page alignment would force a copy into an aligned buffer before the data can be used, defeating the purpose.

same as above.

Anyway, I wonder if we could avoid non-page-aligned initramfs so that ramdax can be enhanced to be used directly.

Quoted 6 lines

The full initramfs integration (init/ changes) that wires up the EROFS initrd detection and layered mount is available for reference here[0]. It implements support for interleaved cpio and EROFS layers, it supports cpio CPU microcode update prefix, and uses overlayfs+tmpfs for write support.

If that's why EROFS is unaligned, and we just add some padding between cpio and erofs image (I think the padding is small than 4k)? or can cpio CPU microcode update prefix placed after the erofs image?

Quoted 6 lines

This series was developed with the assistance of Claude Opus 4.6. All patches carry an Assisted-by tag. I am providing the kunit testing and I have tested this change against the initramfs series to boot an EROFS initramfs. I welcome feedback on the approach and implementation.

Yes, the generated code can work but IMHO it doesn't mean it's well suitable for upstream...

It's just my own humble opinion, but I expect VFS/MM folks could have similar thoughts. However, I'm also interested if EROFS can be used for initramfs, as long as we have a proper way to implement that.

Thanks,
Gao Xiang

Quoted 30 lines

Patch 1 adds the core memback implementation.
Patch 2 adds a KUnit test that mounts a compressed EROFS image at a non-page-aligned offset and verifies decompressed file contents.

[0] https://github.com/aruiz/linux/tree/wip/erofs-initramfs-memback

---
Alberto Ruiz (2):
erofs: add memory-backed mode for non-page-aligned mount
erofs: add KUnit test for memory-backed compressed mount

  fs/erofs/Kconfig        |  11 ++++
  fs/erofs/Makefile       |   2 +
  fs/erofs/data.c         |  33 +++++++++-
  fs/erofs/internal.h     |  53 +++++++++++----
  fs/erofs/memback.c      | 169 ++++++++++++++++++++++++++++++++++++++++++++++++
  fs/erofs/memback_test.c | 151 ++++++++++++++++++++++++++++++++++++++++++
  fs/erofs/super.c        |  38 +++++++++--
  fs/erofs/zdata.c        |  16 +++--
  8 files changed, 447 insertions(+), 26 deletions(-)

---
base-commit: 6b5a2b7d9bc156e505f09e698d85d6a1547c1206
change-id: 20260617-erofs-memback-0ae2448ba2cc

Best regards,
--
Alberto Ruiz <[email protected]>