Skip to contentSkip to conversation

erofs: inode page cache share feature

RFC PATCH In review Hongzhen Luo 2 participants 12 messages Mar 1, 2025 – Mar 24, 2025 EROFS file system

lore
Share & tools
lore
#0 lore raw

v6 cover erofs: inode page cache share feature

Background
==============
Currently, reading files with different paths (or names) but the same content can consume multiple copies of the page cache, even if the content of these caches is identical. For example, reading identical files (e.g., *.so files) from two different minor versions of container images can result in multiple copies of the same page cache, since different containers have different mount points. Therefore, sharing the page cache for files with the same content can save memory.

Proposal
==============

1. determining file identity
----------------------------
First, a way needs to be found to check whether the content of two files is the same. Here, the xattr values associated with the file fingerprints are assessed for consistency. When creating the EROFS image, users can specify the name of the xattr for file fingerprints, and the corresponding name will be stored in the packfile. The on-disk ishare_key_start indicates the offset of the xattr's name within the packfile:

```
struct erofs_super_block {
 	__le32 xattr_prefix_start;	/* start of long xattr prefixes */
 	__le64 packed_nid;	/* nid of the special packed inode */
 	__u8 xattr_filter_reserved; /* reserved for xattr name filter */
-	__u8 reserved2[23];
+	__le32 ishare_key_start; /* start of ishare key */
+	__u8 reserved2[19];
+};
```

For example, users can specify the first long prefix as the name for the file fingerprint as follows:

```
mkfs.erofs  --ishare_key=trusted.erofs.fingerprint  erofs.img ./dir
```

In this way, trusted.erofs.fingerprint serves as the name of the xattr for the file fingerprint. The relevant patches for erofs-utils will be released later.

At the same time, for security reasons, this patch series only shares files within the same domain, which is achieved by adding "-o domain_id=xxxx" during the mounting process:

```
mount -t erofs -o domain_id=xxx erofs.img /mnt
```

If no domain ID is specified, it will fall back to the non-page cache sharing mode.

2. whose page cache is shared?
------------------------------

2.1. share the page cache of inode_A or inode_B
-----------------------------------------------
For example, we can share the page cache of inode_A, referred to as PGCache_A. When reading file B, we read the contents from PGCache_A to achieve memory savings. Furthermore, if we need to read another file C with the same content, we will still read from PGCache_A. In this way, we fulfill multiple read requests with just a single page cache.

2.2. share the de-duplicated inode's page cache
-----------------------------------------------
Unlike in 2.1, we allocate an internal deduplicated inode and use its page cache as shared. Reads for files with identical content will ultimately be routed to the page cache of the deduplicated inode. In this way, a single page cache satisfies multiple read requests for different files with the same contents.

2.3. discussion of the two solutions
-----------------------------------------------
Although the solution in 2.1 allows for page cache sharing, it has inherent drawbacks. The creation and destruction of inode nodes in the file system mean that when inode_A is destroyed, PGCache_A is also released. Consequently, if we need to read the file content afterward, we must retrieve the data from the disk again. This conflicts with the design philosophy of page cache (caching contents from the disk).

Therefore, I choose to implement the solution in 2.2, which is to allocate an internal deduplicated inode and use its page cache as shared.

3. Implementation
==================

3.1. file open & close
----------------------
When the file is opened, the ->private_data field of file A or file B is set to point to an internal deduplicated file. When the actual read occurs, the page cache of this deduplicated file will be accessed.

When the file is opened, if the corresponding erofs inode is newly created, then perform the following actions:
1. add the erofs inode to the backing list of the deduplicated inode;
2. increase the reference count of the deduplicated inode.

The purpose of step 1 above is to ensure that when a real I/O operation occurs, the deduplicated inode can locate one of the disk devices (as the deduplicated inode itself is not bound to a specific device). Step 2 is for managing the lifecycle of the deduplicated inode.

When the erofs inode is destroyed, the opposite actions mentioned above will be taken.

3.2. file reading
-----------------
Assuming the deduplication inode's page cache is PGCache_dedup, there are two possible scenarios when reading a file:
1) the content being read is already present in PGCache_dedup;
2) the content being read is not present in PGCache_dedup.

In the second scenario, it involves the iomap operation to read from the disk.

3.2.1. reading existing data in PGCache_dedup
-------------------------------------------
In this case, the overall read flowchart is as follows (take ksys_read() for example):

         ksys_read                                               
             │                                                   
             │                                                   
             ▼                                                   
            ...                                                  
             │                                                   
             │                                                   
             ▼                                                   
erofs_ishare_file_read_iter (switch to backing deduplicated file)
             │                                                   
             │                                                   
             ▼                                                   

read PGCache_dedup & return

At this point, the content in PGCache_dedup will be read directly and returned.

3.2.2 reading non-existent content in PGCache_dedup
---------------------------------------------------
In this case, disk I/O operations will be involved. Taking the reading of an uncompressed file as an example, here is the reading process:

         ksys_read                                               
             │                                                   
             │                                                   
             ▼                                                   
            ...                                                  
             │                                                   
             │                                                   
             ▼                                                   
erofs_ishare_file_read_iter (switch to backing deduplicated file)
             │                                                   
             │                                                   
             ▼                                                   
            ... (allocate pages)                                 
             │                                                   
             │                                                   
             ▼                                                   
erofs_read_folio/erofs_readahead                                 
             │                                                   
             │                                                   
             ▼                                                   
            ... (iomap)                                          
             │                                                   
             │                                                   
             ▼                                                   
        erofs_iomap_begin                                        
             │                                                   
             │                                                   
             ▼                                                   
            ...                                                  

Iomap and the layers below will involve disk I/O operations. As described in 3.1, the deduplicated inode itself is not bound to a specific device. The deduplicated inode will select an erofs inode from the backing list (by default, the first one) to complete the corresponding iomap operation.

3.2.3 optimized inode selection
-------------------------------
The inode selection method described in 3.2.2 may select an "inactive" inode. An inactive inode indicates that there may have been no read operations on the inode's device for a long time, and there is a high likelihood that the device may be unmounted. In this case, unmounting the device may experience a slight delay due to other read requests being routed to that device. Therefore, we need to select some "active" inodes for the iomap operation.

To achieve optimized inode selection, an additional processing list has been added. At the beginning of erofs_{read_folio,readahead}(), the corresponding erofs inode will be added to the processing list (because they are active). And it is removed at the end of erofs_{read_folio,readahead}(). In erofs_iomap_begin(), the selected erofs inode's count is incremented, and in erofs_iomap_end(), the count is decremented.

In this way, even after the erofs inode is removed from the processing list, the increment in the reference count can ensure the integrity of the data reading process. This is somewhat similar to RCU (not exactly the same, but similar).

3.3. release page cache
-----------------------
Similar to overlayfs, when dropping the page cache via .fadvise, erofs locates the deduplicated file and applies vfs_fadvise to that specific file.

Effect
==================
I conducted experiments on two aspects across two different minor versions of container images:

1. reading all files in two different minor versions of container images

2. run workloads or use the default entrypoint within the containers^[1]

Below is the memory usage for reading all files in two different minor versions of container images:

+-------------------+------------------+-------------+---------------+
|       Image       | Page Cache Share | Memory (MB) |    Memory     |
|                   |                  |             | Reduction (%) |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     241     |       -       |
|       redis       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     163     |      33%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     872     |       -       |
|      postgres     +------------------+-------------+---------------+
|    16.1 & 16.2    |        Yes       |     630     |      28%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     2771    |       -       |
|     tensorflow    +------------------+-------------+---------------+
|  2.11.0 & 2.11.1  |        Yes       |     2340    |      16%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     926     |       -       |
|       mysql       +------------------+-------------+---------------+
|  8.0.11 & 8.0.12  |        Yes       |     735     |      21%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     390     |       -       |
|       nginx       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     219     |      44%      |
+-------------------+------------------+-------------+---------------+
|       tomcat      |        No        |     924     |       -       |
| 10.1.25 & 10.1.26 +------------------+-------------+---------------+
|                   |        Yes       |     474     |      49%      |
+-------------------+------------------+-------------+---------------+

Additionally, the table below shows the runtime memory usage of the container:

+-------------------+------------------+-------------+---------------+
|       Image       | Page Cache Share | Memory (MB) |    Memory     |
|                   |                  |             | Reduction (%) |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     34.9    |       -       |
|       redis       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     33.6    |       4%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |    149.1    |       -       |
|      postgres     +------------------+-------------+---------------+
|    16.1 & 16.2    |        Yes       |      95     |      37%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |    1027.9   |       -       |
|     tensorflow    +------------------+-------------+---------------+
|  2.11.0 & 2.11.1  |        Yes       |    934.3    |      10%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |    155.0    |       -       |
|       mysql       +------------------+-------------+---------------+
|  8.0.11 & 8.0.12  |        Yes       |    139.1    |      11%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     25.4    |       -       |
|       nginx       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     18.8    |      26%      |
+-------------------+------------------+-------------+---------------+
|       tomcat      |        No        |     186     |       -       |
| 10.1.25 & 10.1.26 +------------------+-------------+---------------+
|                   |        Yes       |      99     |      47%      |
+-------------------+------------------+-------------+---------------+

It can be observed that when reading all the files in the image, the reduced memory usage varies from 16% to 49%, depending on the specific image. Additionally, the container's runtime memory usage reduction ranges from 4% to 47%.

[1] Below are the workload for these images:
- redis: redis-benchmark
- postgres: sysbench
- tensorflow: app.py of tensorflow.python.platform
- mysql: sysbench
- nginx: wrk
- tomcat: default entrypoint

The patch in this version has made the following changes compared to the previous versionv(patch v5):

- support user-defined fingerprint name;
- support domain-specific page cache share;
- adjusted the code style;
- adjustments in code implementation, etc.

v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Hongzhen Luo (7):
erofs: move struct erofs_anon_fs_type to super.c
erofs: support user-defined fingerprint name
erofs: support domain-specific page cache share
erofs: introduce the page cache share feature
erofs: support unencoded inodes for page cache share
erofs: support compressed inodes for page cache share
erofs: implement .fadvise for page cache share

 fs/erofs/Kconfig    |  10 ++
 fs/erofs/Makefile   |   1 +
 fs/erofs/data.c     |  82 ++++++++++-
 fs/erofs/erofs_fs.h |   9 +-
 fs/erofs/fscache.c  |  13 --
 fs/erofs/inode.c    |   5 +
 fs/erofs/internal.h |  29 ++++
 fs/erofs/ishare.c   | 330 ++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/ishare.h   |  55 ++++++++
 fs/erofs/super.c    |  66 ++++++++-
 fs/erofs/xattr.c    |  49 +++++++
 fs/erofs/xattr.h    |   6 +
 fs/erofs/zdata.c    |  57 ++++++--
 13 files changed, 675 insertions(+), 37 deletions(-)
 create mode 100644 fs/erofs/ishare.c
 create mode 100644 fs/erofs/ishare.h
Signature · 1 line
2.43.5
#1 lore raw

RFC PATCH v6 1/7 erofs: move `struct erofs_anon_fs_type` to super.c

Move the struct erofs_anon_fs_type to the super.c and expose it in preparation for the upcoming page cache share feature.

---
 fs/erofs/fscache.c  | 13 -------------
 fs/erofs/internal.h |  2 ++
 fs/erofs/super.c    | 13 +++++++++++++
 3 files changed, 15 insertions(+), 13 deletions(-)
fs/erofs/fscache.c +0 -13
index ce3d8737df85..ae7bd9ebff38 100644
@@ -3,7 +3,6 @@
  * Copyright (C) 2022, Alibaba Cloud
  * Copyright (C) 2022, Bytedance Inc. All rights reserved.
  */
-#include <linux/pseudo_fs.h>
 #include <linux/fscache.h>
 #include "internal.h"
 
@@ -13,18 +12,6 @@ static LIST_HEAD(erofs_domain_list);
 static LIST_HEAD(erofs_domain_cookies_list);
 static struct vfsmount *erofs_pseudo_mnt;
 
-static int erofs_anon_init_fs_context(struct fs_context *fc)
-{
-	return init_pseudo(fc, EROFS_SUPER_MAGIC) ? 0 : -ENOMEM;
-}
-
-static struct file_system_type erofs_anon_fs_type = {
-	.owner		= THIS_MODULE,
-	.name           = "pseudo_erofs",
-	.init_fs_context = erofs_anon_init_fs_context,
-	.kill_sb        = kill_anon_super,
-};
-
 struct erofs_fscache_io {
 	struct netfs_cache_resources cres;
 	struct iov_iter		iter;
fs/erofs/internal.h +2 -0
index 686d835eb533..47004eb89838 100644
@@ -379,6 +379,8 @@ extern const struct file_operations erofs_dir_fops;
 
 extern const struct iomap_ops z_erofs_iomap_report_ops;
 
+extern struct file_system_type erofs_anon_fs_type;
+
 /* flags for erofs_fscache_register_cookie() */
 #define EROFS_REG_COOKIE_SHARE		0x0001
 #define EROFS_REG_COOKIE_NEED_NOEXIST	0x0002
fs/erofs/super.c +13 -0
index 827b62665649..eb052a770088 100644
@@ -11,6 +11,7 @@
 #include <linux/fs_parser.h>
 #include <linux/exportfs.h>
 #include <linux/backing-dev.h>
+#include <linux/pseudo_fs.h>
 #include "xattr.h"
 
 #define CREATE_TRACE_POINTS
@@ -850,6 +851,18 @@ static struct file_system_type erofs_fs_type = {
 };
 MODULE_ALIAS_FS("erofs");
 
+static int erofs_anon_init_fs_context(struct fs_context *fc)
+{
+	return init_pseudo(fc, EROFS_SUPER_MAGIC) ? 0 : -ENOMEM;
+}
+
+struct file_system_type erofs_anon_fs_type = {
+	.owner		= THIS_MODULE,
+	.name           = "pseudo_erofs",
+	.init_fs_context = erofs_anon_init_fs_context,
+	.kill_sb        = kill_anon_super,
+};
+
 static int __init erofs_module_init(void)
 {
 	int err;
Signature · 1 line
2.43.5
Reviewed-by on v6 1/7
#2 lore raw

Re: [RFC PATCH v6 1/7] erofs: move `struct erofs_anon_fs_type` to super.c

Quoted 5 lines · Hongzhen Luo

Move the struct erofs_anon_fs_type to the super.c and expose it in preparation for the upcoming page cache share feature.

Looks good. Feel free to add:

Quoted 81 lines
---
  fs/erofs/fscache.c  | 13 -------------
  fs/erofs/internal.h |  2 ++
  fs/erofs/super.c    | 13 +++++++++++++
  3 files changed, 15 insertions(+), 13 deletions(-)
fs/erofs/fscache.c +0 -13
index ce3d8737df85..ae7bd9ebff38 100644
@@ -3,7 +3,6 @@
   * Copyright (C) 2022, Alibaba Cloud
   * Copyright (C) 2022, Bytedance Inc. All rights reserved.
   */
-#include <linux/pseudo_fs.h>
  #include <linux/fscache.h>
  #include "internal.h"
 
@@ -13,18 +12,6 @@ static LIST_HEAD(erofs_domain_list);
  static LIST_HEAD(erofs_domain_cookies_list);
  static struct vfsmount *erofs_pseudo_mnt;
 
-static int erofs_anon_init_fs_context(struct fs_context *fc)
-{
-	return init_pseudo(fc, EROFS_SUPER_MAGIC) ? 0 : -ENOMEM;
-}
-
-static struct file_system_type erofs_anon_fs_type = {
-	.owner		= THIS_MODULE,
-	.name           = "pseudo_erofs",
-	.init_fs_context = erofs_anon_init_fs_context,
-	.kill_sb        = kill_anon_super,
-};
-
  struct erofs_fscache_io {
  	struct netfs_cache_resources cres;
  	struct iov_iter		iter;
fs/erofs/internal.h +2 -0
index 686d835eb533..47004eb89838 100644
@@ -379,6 +379,8 @@ extern const struct file_operations erofs_dir_fops;
 
  extern const struct iomap_ops z_erofs_iomap_report_ops;
 
+extern struct file_system_type erofs_anon_fs_type;
+
  /* flags for erofs_fscache_register_cookie() */
  #define EROFS_REG_COOKIE_SHARE		0x0001
  #define EROFS_REG_COOKIE_NEED_NOEXIST	0x0002
fs/erofs/super.c +13 -0
index 827b62665649..eb052a770088 100644
@@ -11,6 +11,7 @@
  #include <linux/fs_parser.h>
  #include <linux/exportfs.h>
  #include <linux/backing-dev.h>
+#include <linux/pseudo_fs.h>
  #include "xattr.h"
 
  #define CREATE_TRACE_POINTS
@@ -850,6 +851,18 @@ static struct file_system_type erofs_fs_type = {
  };
  MODULE_ALIAS_FS("erofs");
 
+static int erofs_anon_init_fs_context(struct fs_context *fc)
+{
+	return init_pseudo(fc, EROFS_SUPER_MAGIC) ? 0 : -ENOMEM;
+}
+
+struct file_system_type erofs_anon_fs_type = {
+	.owner		= THIS_MODULE,
+	.name           = "pseudo_erofs",
+	.init_fs_context = erofs_anon_init_fs_context,
+	.kill_sb        = kill_anon_super,
+};
+
  static int __init erofs_module_init(void)
  {
  	int err;
#3 lore raw

RFC PATCH v6 2/7 erofs: support user-defined fingerprint name

In reply to Hongzhen Luo

When creating the EROFS image, users can specify the fingerprint name. This is to prepare for the upcoming inode page cache share.

---
 fs/erofs/Kconfig    | 10 +++++++++
 fs/erofs/erofs_fs.h |  9 ++++++---
 fs/erofs/internal.h |  6 ++++++
 fs/erofs/super.c    |  4 ++++
 fs/erofs/xattr.c    | 49 +++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/xattr.h    |  6 ++++++
 6 files changed, 81 insertions(+), 3 deletions(-)
fs/erofs/Kconfig +10 -0
index 6ea60661fa55..d2416d35035a 100644
@@ -178,3 +178,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI
 	  at higher priority.
 
 	  If unsure, say N.
+
+config EROFS_FS_INODE_SHARE
+	bool "EROFS inode page cache share support"
+	depends on EROFS_FS && EROFS_FS_XATTR
+	default n
+	help
+	  This permits EROFS to share page cache for files with same
+	  fingerprints.
+
+	  If unsure, say N.
fs/erofs/erofs_fs.h +6 -3
index 199395ed1c1f..261bd9bd47c4 100644
@@ -30,6 +30,7 @@
 #define EROFS_FEATURE_INCOMPAT_FRAGMENTS	0x00000020
 #define EROFS_FEATURE_INCOMPAT_DEDUPE		0x00000020
 #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES	0x00000040
+#define EROFS_FEATURE_INCOMPAT_ISHARE_KEY	0x00000080
 #define EROFS_ALL_FEATURE_INCOMPAT		\
 	(EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
 	 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
@@ -40,7 +41,8 @@
 	 EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
 	 EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
 	 EROFS_FEATURE_INCOMPAT_DEDUPE | \
-	 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES)
+	 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES | \
+	 EROFS_FEATURE_INCOMPAT_ISHARE_KEY)
 
 #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -84,8 +86,9 @@ struct erofs_super_block {
 	__le32 xattr_prefix_start;	/* start of long xattr prefixes */
 	__le64 packed_nid;	/* nid of the special packed inode */
 	__u8 xattr_filter_reserved; /* reserved for xattr name filter */
-	__u8 reserved2[23];
-};
+	__le32 ishare_key_start; /* start of ishare key */
+	__u8 reserved2[19];
+} __packed;
 
 /*
  * EROFS inode datalayout (i_format in on-disk inode):
fs/erofs/internal.h +6 -0
index 47004eb89838..21bf9b694048 100644
@@ -166,6 +166,11 @@ struct erofs_sb_info {
 	struct erofs_domain *domain;
 	char *fsid;
 	char *domain_id;
+
+	/* inode page cache share support */
+	u32 ishare_key_start;
+	int ishare_key_idx;
+	char *ishare_key;
 };
 
 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
@@ -233,6 +238,7 @@ EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
+EROFS_FEATURE_FUNCS(ishare_key, incompat, INCOMPAT_ISHARE_KEY)
 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 
fs/erofs/super.c +4 -0
index eb052a770088..6af02cc8b8c6 100644
@@ -313,6 +313,8 @@ static int erofs_read_superblock(struct super_block *sb)
 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
 	sbi->inos = le64_to_cpu(dsb->inos);
 
+	sbi->ishare_key_start = le32_to_cpu(dsb->ishare_key_start);
+
 	sbi->build_time = le64_to_cpu(dsb->build_time);
 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
 
@@ -676,6 +678,8 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		return err;
 
+	erofs_xattr_set_ishare_key(sb);
+
 	erofs_set_sysfs_name(sb);
 	err = erofs_register_sysfs(sb);
 	if (err)
fs/erofs/xattr.c +49 -0

52 lines not on this page · raw message

fs/erofs/xattr.h +6 -0
index b246cd0e135e..24a243165417 100644
@@ -70,4 +70,10 @@ struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu);
 #define erofs_get_acl	(NULL)
 #endif
 
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+void erofs_xattr_set_ishare_key(struct super_block *sb);
+#else
+static inline void erofs_xattr_set_ishare_key(struct super_block *sb) {}
+#endif
+
 #endif
Signature · 1 line
2.43.5
#4 lore raw

Re: [RFC PATCH v6 2/7] erofs: support user-defined fingerprint name

Quoted 135 lines · Hongzhen Luo

When creating the EROFS image, users can specify the fingerprint name. This is to prepare for the upcoming inode page cache share.

---
  fs/erofs/Kconfig    | 10 +++++++++
  fs/erofs/erofs_fs.h |  9 ++++++---
  fs/erofs/internal.h |  6 ++++++
  fs/erofs/super.c    |  4 ++++
  fs/erofs/xattr.c    | 49 +++++++++++++++++++++++++++++++++++++++++++++
  fs/erofs/xattr.h    |  6 ++++++
  6 files changed, 81 insertions(+), 3 deletions(-)
fs/erofs/Kconfig +10 -0
index 6ea60661fa55..d2416d35035a 100644
@@ -178,3 +178,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI
  	  at higher priority.
 
  	  If unsure, say N.
+
+config EROFS_FS_INODE_SHARE
+	bool "EROFS inode page cache share support"
+	depends on EROFS_FS && EROFS_FS_XATTR
+	default n
+	help
+	  This permits EROFS to share page cache for files with same
+	  fingerprints.
+
+	  If unsure, say N.
fs/erofs/erofs_fs.h +6 -3
index 199395ed1c1f..261bd9bd47c4 100644
@@ -30,6 +30,7 @@
  #define EROFS_FEATURE_INCOMPAT_FRAGMENTS	0x00000020
  #define EROFS_FEATURE_INCOMPAT_DEDUPE		0x00000020
  #define EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES	0x00000040
+#define EROFS_FEATURE_INCOMPAT_ISHARE_KEY	0x00000080
  #define EROFS_ALL_FEATURE_INCOMPAT		\
  	(EROFS_FEATURE_INCOMPAT_ZERO_PADDING | \
  	 EROFS_FEATURE_INCOMPAT_COMPR_CFGS | \
@@ -40,7 +41,8 @@
  	 EROFS_FEATURE_INCOMPAT_ZTAILPACKING | \
  	 EROFS_FEATURE_INCOMPAT_FRAGMENTS | \
  	 EROFS_FEATURE_INCOMPAT_DEDUPE | \
-	 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES)
+	 EROFS_FEATURE_INCOMPAT_XATTR_PREFIXES | \
+	 EROFS_FEATURE_INCOMPAT_ISHARE_KEY)
 
  #define EROFS_SB_EXTSLOT_SIZE	16
 
@@ -84,8 +86,9 @@ struct erofs_super_block {
  	__le32 xattr_prefix_start;	/* start of long xattr prefixes */
  	__le64 packed_nid;	/* nid of the special packed inode */
  	__u8 xattr_filter_reserved; /* reserved for xattr name filter */
-	__u8 reserved2[23];
-};
+	__le32 ishare_key_start; /* start of ishare key */
+	__u8 reserved2[19];
+} __packed;
 
  /*
   * EROFS inode datalayout (i_format in on-disk inode):
fs/erofs/internal.h +6 -0
index 47004eb89838..21bf9b694048 100644
@@ -166,6 +166,11 @@ struct erofs_sb_info {
  	struct erofs_domain *domain;
  	char *fsid;
  	char *domain_id;
+
+	/* inode page cache share support */
+	u32 ishare_key_start;
+	int ishare_key_idx;
+	char *ishare_key;
  };
 
  #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
@@ -233,6 +238,7 @@ EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
  EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
  EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
  EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
+EROFS_FEATURE_FUNCS(ishare_key, incompat, INCOMPAT_ISHARE_KEY)
  EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
  EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
 
fs/erofs/super.c +4 -0
index eb052a770088..6af02cc8b8c6 100644
@@ -313,6 +313,8 @@ static int erofs_read_superblock(struct super_block *sb)
  	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
  	sbi->inos = le64_to_cpu(dsb->inos);
 
+	sbi->ishare_key_start = le32_to_cpu(dsb->ishare_key_start);
+
  	sbi->build_time = le64_to_cpu(dsb->build_time);
  	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
 
@@ -676,6 +678,8 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
  	if (err)
  		return err;
 
+	erofs_xattr_set_ishare_key(sb);
+
  	erofs_set_sysfs_name(sb);
  	err = erofs_register_sysfs(sb);
  	if (err)
fs/erofs/xattr.c +16 -0
index 7940241d9355..30a64ac3239a 100644
@@ -549,3 +549,52 @@ struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
  	return acl;
  }
  #endif
+
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+void erofs_xattr_set_ishare_key(struct super_block *sb)
+{
+	struct erofs_sb_info *sbi = EROFS_SB(sb);
+	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
+	struct xattr_handler const *handler;
+	erofs_off_t pos;
+	char *key;
+	int len, i;
+	void *ptr;
+
+	if (!erofs_sb_has_fragments(sbi) || !erofs_sb_has_ishare_key(sbi) ||
+	    !sbi->packed_inode)
+		return;
+

fs/erofs/xattr.c struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu) · line 577

Quoted 10 lines
fs/erofs/xattr.c +10 -0
+	buf.mapping = sbi->packed_inode->i_mapping;
+	pos = sbi->ishare_key_start << 2;
+	ptr = erofs_read_metadata(sb, &buf, &pos, &len);
+
+	if (IS_ERR(ptr)) {
+		erofs_put_metabuf(&buf);
+		return;
+	}
+
+	for (i = 0; ARRAY_SIZE(erofs_xattr_handlers); i++) {

ARRAY_SIZE will get the length of array erofs_xattr_handlers, here you must forget it. So here it should be i < ARRAY_SIZE(erofs_xattr_handlers) - 1. :)

fs/erofs/xattr.c struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu) · line 586

Quoted 9 lines
fs/erofs/xattr.c +9 -0
+		handler = erofs_xattr_handlers[i];
+		if (!handler)
+			break;
+		if (!memcmp(handler->prefix, ptr, strlen(handler->prefix)))
+			break;
+	}
+
+	if (!handler)
+		return;

This exception branch lacks a call to erofs_put_metabuf(&buf).

Quoted 29 lines
fs/erofs/xattr.c +14 -0
+
+	len -= strlen(handler->prefix);
+	key = kzalloc(len + 1, GFP_KERNEL);
+	if (!key) {
+		erofs_put_metabuf(&buf);
+		return;
+	}
+
+	memcpy(key, ptr + strlen(handler->prefix), len);
+	sbi->ishare_key = key;
+	sbi->ishare_key_idx = handler->flags;
+	erofs_put_metabuf(&buf);
+}
+#endif
fs/erofs/xattr.h +6 -0
index b246cd0e135e..24a243165417 100644
@@ -70,4 +70,10 @@ struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu);
  #define erofs_get_acl	(NULL)
  #endif
 
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+void erofs_xattr_set_ishare_key(struct super_block *sb);
+#else
+static inline void erofs_xattr_set_ishare_key(struct super_block *sb) {}
+#endif
+
  #endif
#5 lore raw

RFC PATCH v6 3/7 erofs: support domain-specific page cache share

In reply to Hongzhen Luo

Only files in the same domain will share the page cache. Also modify the sysfs related content in preparation for the upcoming page cache share feature.

---
 fs/erofs/super.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
fs/erofs/super.c +8 -4
index 6af02cc8b8c6..ceab0c29b061 100644
@@ -489,6 +489,8 @@ static int erofs_fc_parse_param(struct fs_context *fc,
 		if (!sbi->fsid)
 			return -ENOMEM;
 		break;
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_INODE_SHARE)
 	case Opt_domain_id:
 		kfree(sbi->domain_id);
 		sbi->domain_id = kstrdup(param->string, GFP_KERNEL);
@@ -558,16 +560,16 @@ static void erofs_set_sysfs_name(struct super_block *sb)
 {
 	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
-	if (sbi->domain_id)
+	if (sbi->domain_id && !sbi->ishare_key)
 		super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
 					     sbi->fsid);
 	else if (sbi->fsid)
 		super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
-	else if (erofs_is_fileio_mode(sbi))
+	else if (!sb->s_bdi || !sb->s_bdi->dev)
+		super_set_sysfs_name_id(sb);
+	else
 		super_set_sysfs_name_generic(sb, "%s",
 					     bdi_dev_name(sb->s_bdi));
-	else
-		super_set_sysfs_name_id(sb);
 }
 
 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
@@ -965,6 +967,8 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)
 #ifdef CONFIG_EROFS_FS_ONDEMAND
 	if (sbi->fsid)
 		seq_printf(seq, ",fsid=%s", sbi->fsid);
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_INODE_SHARE)
 	if (sbi->domain_id)
 		seq_printf(seq, ",domain_id=%s", sbi->domain_id);
 #endif
Signature · 1 line
2.43.5
#6 lore raw

Re: [RFC PATCH v6 3/7] erofs: support domain-specific page cache share

Quoted 23 lines · Hongzhen Luo

Only files in the same domain will share the page cache. Also modify the sysfs related content in preparation for the upcoming page cache share feature.

---
  fs/erofs/super.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
fs/erofs/super.c +2 -0
index 6af02cc8b8c6..ceab0c29b061 100644
@@ -489,6 +489,8 @@ static int erofs_fc_parse_param(struct fs_context *fc,
  		if (!sbi->fsid)
  			return -ENOMEM;
  		break;
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_INODE_SHARE)
  	case Opt_domain_id:
  		kfree(sbi->domain_id);
  		sbi->domain_id = kstrdup(param->string, GFP_KERNEL);

fs/erofs/super.c static void erofs_set_sysfs_name(struct super_block *sb) · line 566

Quoted 10 lines
fs/erofs/super.c +1 -2
@@ -558,16 +560,16 @@ static void erofs_set_sysfs_name(struct super_block *sb)
  {
  	struct erofs_sb_info *sbi = EROFS_SB(sb);
 
-	if (sbi->domain_id)
+	if (sbi->domain_id && !sbi->ishare_key)
  		super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
  					     sbi->fsid);
  	else if (sbi->fsid)
  		super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
-	else if (erofs_is_fileio_mode(sbi))

I think there is no need to change this, because the inode page cache is just a mode for reading, not like a super block type.

Quoted 19 lines
fs/erofs/super.c +5 -2
+	else if (!sb->s_bdi || !sb->s_bdi->dev)
+		super_set_sysfs_name_id(sb);
+	else
  		super_set_sysfs_name_generic(sb, "%s",
  					     bdi_dev_name(sb->s_bdi));
-	else
-		super_set_sysfs_name_id(sb);
  }
 
  static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
@@ -965,6 +967,8 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)
  #ifdef CONFIG_EROFS_FS_ONDEMAND
  	if (sbi->fsid)
  		seq_printf(seq, ",fsid=%s", sbi->fsid);
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || defined(CONFIG_EROFS_FS_INODE_SHARE)
  	if (sbi->domain_id)
  		seq_printf(seq, ",domain_id=%s", sbi->domain_id);
  #endif
#7 lore raw

Re: [RFC PATCH v6 3/7] erofs: support domain-specific page cache share

Quoted 31 lines · Hongbo Li
Quoted 28 lines · Hongzhen Luo

Only files in the same domain will share the page cache. Also modify the sysfs related content in preparation for the upcoming page cache share feature.

---
  fs/erofs/super.c | 12 ++++++++----

  1 file changed, 8 insertions(+), 4 deletions(-)
fs/erofs/super.c +2 -0
index 6af02cc8b8c6..ceab0c29b061 100644
@@ -489,6 +489,8 @@ static int erofs_fc_parse_param(struct fs_context 
 *fc,
           if (!sbi->fsid)
               return -ENOMEM;
           break;
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || 
 defined(CONFIG_EROFS_FS_INODE_SHARE)

      case Opt_domain_id:
          kfree(sbi->domain_id);
          sbi->domain_id = kstrdup(param->string, GFP_KERNEL);

fs/erofs/super.c +0 -0
@@ -558,16 +560,16 @@ static void erofs_set_sysfs_name(struct 
 super_block *sb)
   {
       struct erofs_sb_info *sbi = EROFS_SB(sb);
Quoted 8 lines

fs/erofs/super.c static void erofs_set_sysfs_name(struct super_block *sb) · line 566

Quoted 7 lines
fs/erofs/super.c +1 -1
   -    if (sbi->domain_id)
+    if (sbi->domain_id && !sbi->ishare_key)
           super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
                            sbi->fsid);
       else if (sbi->fsid)
           super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
-    else if (erofs_is_fileio_mode(sbi))

I think there is no need to change this, because the inode page cache is just a mode for reading, not like a super block type.

Apologies for the late reply, I've been occupied with other tasks recently.

Yeah, let me take a look and I'll make changes afterward.

Thanks,

Hongzhen

Quoted 20 lines
Quoted 20 lines
fs/erofs/super.c +5 -2
+    else if (!sb->s_bdi || !sb->s_bdi->dev)
+        super_set_sysfs_name_id(sb);
+    else
           super_set_sysfs_name_generic(sb, "%s",
                            bdi_dev_name(sb->s_bdi));
-    else
-        super_set_sysfs_name_id(sb);
   }
     static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
@@ -965,6 +967,8 @@ static int erofs_show_options(struct seq_file 
 *seq, struct dentry *root)
   #ifdef CONFIG_EROFS_FS_ONDEMAND
       if (sbi->fsid)
           seq_printf(seq, ",fsid=%s", sbi->fsid);
+#endif
+#if defined(CONFIG_EROFS_FS_ONDEMAND) || 
 defined(CONFIG_EROFS_FS_INODE_SHARE)
       if (sbi->domain_id)

          seq_printf(seq, ",domain_id=%s", sbi->domain_id); #endif

#8 lore raw

RFC PATCH v6 4/7 erofs: introduce the page cache share feature

In reply to Hongzhen Luo

Currently, reading files with different paths (or names) but the same content will consume multiple copies of the page cache, even if the content of these page caches is the same. For example, reading identical files (e.g., *.so files) from two different minor versions of container images will cost multiple copies of the same page cache, since different containers have different mount points. Therefore, sharing the page cache for files with the same content can save memory.

This introduces the page cache share feature in erofs. It allocate a deduplicated inode and use its page cache as shared. Reads for files with identical content will ultimately be routed to the page cache of the deduplicated inode. In this way, a single page cache satisfies multiple read requests for different files with the same contents.

---
 fs/erofs/Makefile   |   1 +
 fs/erofs/internal.h |  17 ++++
 fs/erofs/ishare.c   | 238 ++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/ishare.h   |  28 ++++++
 fs/erofs/super.c    |  28 +++++-
 5 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 fs/erofs/ishare.c
 create mode 100644 fs/erofs/ishare.h
fs/erofs/Makefile +1 -0
index 4331d53c7109..2eb35bec8472 100644
@@ -9,3 +9,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o
 erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o
 erofs-$(CONFIG_EROFS_FS_BACKED_BY_FILE) += fileio.o
 erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
+erofs-$(CONFIG_EROFS_FS_INODE_SHARE) += ishare.o
fs/erofs/internal.h +17 -0
index 21bf9b694048..49613b257a6a 100644
@@ -286,6 +286,22 @@ struct erofs_inode {
 		};
 #endif	/* CONFIG_EROFS_FS_ZIP */
 	};
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+	union {
+		/* internal dedup inode */
+		struct {
+			char *fingerprint;
+			spinlock_t lock;
+			/* all backing inodes */
+			struct list_head backing_head;
+		};
+
+		struct {
+			struct inode *ishare;
+			struct list_head backing_link;
+		};
+	};
+#endif
 	/* the corresponding vfs inode */
 	struct inode vfs_inode;
 };
@@ -382,6 +398,7 @@ extern const struct inode_operations erofs_dir_iops;
 
 extern const struct file_operations erofs_file_fops;
 extern const struct file_operations erofs_dir_fops;
+extern const struct file_operations erofs_ishare_fops;
 
 extern const struct iomap_ops z_erofs_iomap_report_ops;
 
fs/erofs/ishare.c new file +238 -0

238 lines not on this page · raw message

fs/erofs/ishare.h new file +28 -0
new file mode 100644
index 000000000000..54f2251c8179
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2024, Alibaba Cloud
+ */
+#ifndef __EROFS_ISHARE_H
+#define __EROFS_ISHARE_H
+
+#include <linux/fs.h>
+#include <linux/spinlock.h>
+#include "internal.h"
+
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+
+int erofs_ishare_init(struct super_block *sb);
+void erofs_ishare_exit(struct super_block *sb);
+bool erofs_ishare_fill_inode(struct inode *inode);
+void erofs_ishare_free_inode(struct inode *inode);
+
+#else
+
+static inline int erofs_ishare_init(struct super_block *sb) { return 0; }
+static inline void erofs_ishare_exit(struct super_block *sb) {}
+static inline bool erofs_ishare_fill_inode(struct inode *inode) { return false; }
+static inline void erofs_ishare_free_inode(struct inode *inode) {}
+
+#endif // CONFIG_EROFS_FS_INODE_SHARE
+
+#endif
fs/erofs/super.c +27 -1
index ceab0c29b061..98d8b58afe5e 100644
@@ -13,6 +13,7 @@
 #include <linux/backing-dev.h>
 #include <linux/pseudo_fs.h>
 #include "xattr.h"
+#include "ishare.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/erofs.h>
@@ -857,9 +858,34 @@ static struct file_system_type erofs_fs_type = {
 };
 MODULE_ALIAS_FS("erofs");
 
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+static void erofs_free_dedup_inode(struct inode *inode)
+{
+	struct erofs_inode *vi = EROFS_I(inode);
+
+	kfree(vi->fingerprint);
+	kmem_cache_free(erofs_inode_cachep, vi);
+};
+#else
+#define erofs_free_dedup_inode NULL
+#endif
+
+static const struct super_operations erofs_anon_sops = {
+	.statfs = simple_statfs,
+	.alloc_inode = erofs_alloc_inode,
+	.free_inode = erofs_free_dedup_inode,
+};
+
+
 static int erofs_anon_init_fs_context(struct fs_context *fc)
 {
-	return init_pseudo(fc, EROFS_SUPER_MAGIC) ? 0 : -ENOMEM;
+	struct pseudo_fs_context *ctx;
+
+	ctx = init_pseudo(fc, EROFS_SUPER_MAGIC);
+	if (ctx)
+		ctx->ops = &erofs_anon_sops;
+
+	return ctx ? 0 : -ENOMEM;
 }
 
 struct file_system_type erofs_anon_fs_type = {
Signature · 1 line
2.43.5
#9 lore raw

RFC PATCH v6 5/7 erofs: support unencoded inodes for page cache share

In reply to Hongzhen Luo

This patch adds inode page cache sharing functionality for unencoded files.

I conducted experiments in the container environment. Below is the memory usage for reading all files in two different minor versions of container images:

+-------------------+------------------+-------------+---------------+
|       Image       | Page Cache Share | Memory (MB) |    Memory     |
|                   |                  |             | Reduction (%) |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     241     |       -       |
|       redis       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     163     |      33%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     872     |       -       |
|      postgres     +------------------+-------------+---------------+
|    16.1 & 16.2    |        Yes       |     630     |      28%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     2771    |       -       |
|     tensorflow    +------------------+-------------+---------------+
|  2.11.0 & 2.11.1  |        Yes       |     2340    |      16%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     926     |       -       |
|       mysql       +------------------+-------------+---------------+
|  8.0.11 & 8.0.12  |        Yes       |     735     |      21%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     390     |       -       |
|       nginx       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |     219     |      44%      |
+-------------------+------------------+-------------+---------------+
|       tomcat      |        No        |     924     |       -       |
| 10.1.25 & 10.1.26 +------------------+-------------+---------------+
|                   |        Yes       |     474     |      49%      |
+-------------------+------------------+-------------+---------------+

Additionally, the table below shows the runtime memory usage of the container:

+-------------------+------------------+-------------+---------------+
|       Image       | Page Cache Share | Memory (MB) |    Memory     |
|                   |                  |             | Reduction (%) |
+-------------------+------------------+-------------+---------------+
|                   |        No        |      35     |       -       |
|       redis       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |      28     |      20%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     149     |       -       |
|      postgres     +------------------+-------------+---------------+
|    16.1 & 16.2    |        Yes       |      95     |      37%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     1028    |       -       |
|     tensorflow    +------------------+-------------+---------------+
|  2.11.0 & 2.11.1  |        Yes       |     930     |      10%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |     155     |       -       |
|       mysql       +------------------+-------------+---------------+
|  8.0.11 & 8.0.12  |        Yes       |     132     |      15%      |
+-------------------+------------------+-------------+---------------+
|                   |        No        |      25     |       -       |
|       nginx       +------------------+-------------+---------------+
|   7.2.4 & 7.2.5   |        Yes       |      20     |      20%      |
+-------------------+------------------+-------------+---------------+
|       tomcat      |        No        |     186     |       -       |
| 10.1.25 & 10.1.26 +------------------+-------------+---------------+
|                   |        Yes       |      98     |      48%      |
+-------------------+------------------+-------------+---------------+
---
 fs/erofs/data.c     | 82 ++++++++++++++++++++++++++++++++++++++++----
 fs/erofs/inode.c    |  5 +++
 fs/erofs/internal.h |  4 +++
 fs/erofs/ishare.c   | 83 +++++++++++++++++++++++++++++++++++++++++++++
 fs/erofs/ishare.h   | 27 +++++++++++++++
 fs/erofs/super.c    | 11 ++++++
 6 files changed, 205 insertions(+), 7 deletions(-)
fs/erofs/data.c +75 -7
index 0cd6b5c4df98..b7d7e67832eb 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2021, Alibaba Cloud
  */
 #include "internal.h"
+#include "ishare.h"
 #include <linux/sched/mm.h>
 #include <trace/events/erofs.h>
 
@@ -267,18 +268,44 @@ void erofs_onlinefolio_end(struct folio *folio, int err)
 	folio_end_read(folio, !(v & EROFS_ONLINEFOLIO_EIO));
 }
 
+struct erofs_iomap {
+	void *base;
+	struct inode *realinode;
+};
+
 static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
 {
 	int ret;
-	struct super_block *sb = inode->i_sb;
+	struct super_block *sb;
 	struct erofs_map_blocks map;
 	struct erofs_map_dev mdev;
+	struct inode *realinode = inode;
+	struct erofs_iomap *erofs_iomap;
+	bool is_ishare = erofs_is_ishare_inode(inode);
+
+	if (is_ishare) {
+		if (!iomap->private) {
+			erofs_iomap = kzalloc(sizeof(*erofs_iomap),
+					      GFP_KERNEL);
+			if (!erofs_iomap)
+				return -ENOMEM;
+			erofs_iomap->realinode = erofs_ishare_iget(inode);
+			if (!erofs_iomap->realinode) {
+				kfree(erofs_iomap);
+				return -EINVAL;
+			}
+			iomap->private = erofs_iomap;
+		}
+		erofs_iomap = iomap->private;
+		realinode = erofs_iomap->realinode;
+	}
 
+	sb = realinode->i_sb;
 	map.m_la = offset;
 	map.m_llen = length;
 
-	ret = erofs_map_blocks(inode, &map);
+	ret = erofs_map_blocks(realinode, &map);
 	if (ret < 0)
 		return ret;
 
@@ -297,7 +324,11 @@ static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		iomap->bdev = mdev.m_bdev;
 	iomap->length = map.m_llen;
 	iomap->flags = 0;
-	iomap->private = NULL;
+
+	if (is_ishare)
+		erofs_iomap->base = NULL;
+	else
+		iomap->private = NULL;
 
 	if (!(map.m_flags & EROFS_MAP_MAPPED)) {
 		iomap->type = IOMAP_HOLE;
@@ -316,7 +347,10 @@ static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		if (IS_ERR(ptr))
 			return PTR_ERR(ptr);
 		iomap->inline_data = ptr;
-		iomap->private = buf.base;
+		if (is_ishare)
+			erofs_iomap->base = buf.base;
+		else
+			iomap->private = buf.base;
 	} else {
 		iomap->type = IOMAP_MAPPED;
 		iomap->addr = mdev.m_pa;
@@ -329,7 +363,17 @@ static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
 		ssize_t written, unsigned int flags, struct iomap *iomap)
 {
-	void *ptr = iomap->private;
+	struct erofs_iomap *erofs_iomap;
+	bool is_ishare;
+	void *ptr;
+
+	is_ishare = erofs_is_ishare_inode(inode);
+	if (is_ishare) {
+		erofs_iomap = iomap->private;
+		ptr = erofs_iomap->base;
+	} else {
+		ptr = iomap->private;
+	}
 
 	if (ptr) {
 		struct erofs_buf buf = {
@@ -342,6 +386,13 @@ static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
 	} else {
 		DBG_BUGON(iomap->type == IOMAP_INLINE);
 	}
+
+	if (is_ishare) {
+		erofs_ishare_iput(erofs_iomap->realinode);
+		kfree(erofs_iomap);
+		iomap->private = NULL;
+	}
+
 	return written;
 }
 
@@ -370,12 +421,29 @@ int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  */
 static int erofs_read_folio(struct file *file, struct folio *folio)
 {
-	return iomap_read_folio(folio, &erofs_iomap_ops);
+	struct erofs_read_ctx rdctx = {
+		.file = file,
+		.inode = folio_inode(folio),
+	};
+	int ret;
+
+	erofs_read_begin(&rdctx);
+	ret = iomap_read_folio(folio, &erofs_iomap_ops);
+	erofs_read_end(&rdctx);
+
+	return ret;
 }
 
 static void erofs_readahead(struct readahead_control *rac)
 {
-	return iomap_readahead(rac, &erofs_iomap_ops);
+	struct erofs_read_ctx rdctx = {
+		.file = rac->file,
+		.inode = rac->mapping->host,
+	};
+
+	erofs_read_begin(&rdctx);
+	iomap_readahead(rac, &erofs_iomap_ops);
+	erofs_read_end(&rdctx);
 }
 
 static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
fs/erofs/inode.c +5 -0
index d4b89407822a..77893761d773 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2021, Alibaba Cloud
  */
 #include "xattr.h"
+#include "ishare.h"
 #include <trace/events/erofs.h>
 
 static int erofs_fill_symlink(struct inode *inode, void *kaddr,
@@ -216,6 +217,10 @@ static int erofs_fill_inode(struct inode *inode)
 			inode->i_fop = &generic_ro_fops;
 		else
 			inode->i_fop = &erofs_file_fops;
+#ifdef CONFIG_EROFS_FS_INODE_SHARE
+		if (erofs_ishare_fill_inode(inode))
+			inode->i_fop = &erofs_ishare_fops;
+#endif
 		break;
 	case S_IFDIR:
 		inode->i_op = &erofs_dir_iops;
fs/erofs/internal.h +4 -0
index 49613b257a6a..d736e4b57701 100644
@@ -294,11 +294,15 @@ struct erofs_inode {
 			spinlock_t lock;
 			/* all backing inodes */
 			struct list_head backing_head;
+			/* processing list */
+			struct list_head processing_head;
 		};
 
 		struct {
 			struct inode *ishare;
 			struct list_head backing_link;
+			struct list_head processing_link;
+			atomic_t processing_count;
 		};
 	};
 #endif
fs/erofs/ishare.c +83 -0

99 lines not on this page · raw message

fs/erofs/ishare.h +27 -0
index 54f2251c8179..a0ff9403511b 100644
@@ -9,6 +9,11 @@
 #include <linux/spinlock.h>
 #include "internal.h"
 
+struct erofs_read_ctx {
+	struct file *file; /* may be NULL */
+	struct inode *inode;
+};
+
 #ifdef CONFIG_EROFS_FS_INODE_SHARE
 
 int erofs_ishare_init(struct super_block *sb);
@@ -16,6 +21,20 @@ void erofs_ishare_exit(struct super_block *sb);
 bool erofs_ishare_fill_inode(struct inode *inode);
 void erofs_ishare_free_inode(struct inode *inode);
 
+static inline bool erofs_is_ishare_inode(struct inode *inode)
+{
+
+	return !erofs_is_fscache_mode(inode->i_sb) &&
+	       inode->i_sb->s_type == &erofs_anon_fs_type;
+}
+
+/* read/readahead */
+void erofs_read_begin(struct erofs_read_ctx *rdctx);
+void erofs_read_end(struct erofs_read_ctx *rdctx);
+
+struct inode *erofs_ishare_iget(struct inode *inode);
+void erofs_ishare_iput(struct inode *realinode);
+
 #else
 
 static inline int erofs_ishare_init(struct super_block *sb) { return 0; }
@@ -23,6 +42,14 @@ static inline void erofs_ishare_exit(struct super_block *sb) {}
 static inline bool erofs_ishare_fill_inode(struct inode *inode) { return false; }
 static inline void erofs_ishare_free_inode(struct inode *inode) {}
 
+static inline bool erofs_is_ishare_inode(struct inode *inode) { return false; }
+
+static inline void erofs_read_begin(struct erofs_read_ctx *rdctx) {}
+static inline void erofs_read_end(struct erofs_read_ctx *rdctx) {}
+
+static inline struct inode *erofs_ishare_iget(struct inode *inode) { return inode; }
+static inline void erofs_ishare_iput(struct inode *realinode) {}
+
 #endif // CONFIG_EROFS_FS_INODE_SHARE
 
 #endif
fs/erofs/super.c +11 -0
index 98d8b58afe5e..64ef5c78fedd 100644
@@ -82,6 +82,7 @@ static void erofs_free_inode(struct inode *inode)
 {
 	struct erofs_inode *vi = EROFS_I(inode);
 
+	erofs_ishare_free_inode(inode);
 	if (inode->i_op == &erofs_fast_symlink_iops)
 		kfree(inode->i_link);
 	kfree(vi->xattr_shared_xattrs);
@@ -688,6 +689,12 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		return err;
 
+	if (sbi->ishare_key) {
+		err = erofs_ishare_init(sb);
+		if (err)
+			return err;
+	}
+
 	erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
 	return 0;
 }
@@ -823,6 +830,10 @@ static void erofs_kill_sb(struct super_block *sb)
 		kill_anon_super(sb);
 	else
 		kill_block_super(sb);
+
+	if (sbi->ishare_key)
+		erofs_ishare_exit(sb);
+
 	fs_put_dax(sbi->dif0.dax_dev, NULL);
 	erofs_fscache_unregister_fs(sb);
 	erofs_sb_free(sbi);
Signature · 1 line
2.43.5
#10 lore raw

RFC PATCH v6 6/7 erofs: support compressed inodes for page cache share

In reply to Hongzhen Luo

This patch adds page cache sharing functionality for compressed inodes.

---
 fs/erofs/zdata.c | 57 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 47 insertions(+), 10 deletions(-)
fs/erofs/zdata.c +47 -10
index aff09f94afb2..b6383cb26acb 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2022 Alibaba Cloud
  */
 #include "compress.h"
+#include "ishare.h"
 #include <linux/psi.h>
 #include <linux/cpuhotplug.h>
 #include <trace/events/erofs.h>
@@ -474,7 +475,7 @@ enum z_erofs_pclustermode {
 };
 
 struct z_erofs_frontend {
-	struct inode *const inode;
+	struct inode *inode;
 	struct erofs_map_blocks map;
 	struct z_erofs_bvec_iter biter;
 
@@ -1835,11 +1836,24 @@ static void z_erofs_pcluster_readmore(struct z_erofs_frontend *f,
 
 static int z_erofs_read_folio(struct file *file, struct folio *folio)
 {
-	struct inode *const inode = folio->mapping->host;
-	Z_EROFS_DEFINE_FRONTEND(f, inode, folio_pos(folio));
+	struct inode *const inode = folio->mapping->host, *realinode;
+	Z_EROFS_DEFINE_FRONTEND(f, NULL, folio_pos(folio));
+	struct erofs_read_ctx rdctx = {
+		.file = file,
+		.inode = inode,
+	};
 	int err;
 
 	trace_erofs_read_folio(folio, false);
+
+	erofs_read_begin(&rdctx);
+
+	if (erofs_is_ishare_inode(inode))
+		realinode = erofs_ishare_iget(inode);
+	else
+		realinode = inode;
+
+	f.inode = realinode;
 	z_erofs_pcluster_readmore(&f, NULL, true);
 	err = z_erofs_scan_folio(&f, folio, false);
 	z_erofs_pcluster_readmore(&f, NULL, false);
@@ -1848,25 +1862,43 @@ static int z_erofs_read_folio(struct file *file, struct folio *folio)
 	/* if some pclusters are ready, need submit them anyway */
 	err = z_erofs_runqueue(&f, 0) ?: err;
 	if (err && err != -EINTR)
-		erofs_err(inode->i_sb, "read error %d @ %lu of nid %llu",
-			  err, folio->index, EROFS_I(inode)->nid);
+		erofs_err(realinode->i_sb, "read error %d @ %lu of nid %llu",
+			  err, folio->index, EROFS_I(realinode)->nid);
 
 	erofs_put_metabuf(&f.map.buf);
 	erofs_release_pages(&f.pagepool);
+
+	if (erofs_is_ishare_inode(inode))
+		erofs_ishare_iput(realinode);
+
+	erofs_read_end(&rdctx);
+
 	return err;
 }
 
 static void z_erofs_readahead(struct readahead_control *rac)
 {
-	struct inode *const inode = rac->mapping->host;
-	Z_EROFS_DEFINE_FRONTEND(f, inode, readahead_pos(rac));
+	struct inode *const inode = rac->mapping->host, *realinode;
+	Z_EROFS_DEFINE_FRONTEND(f, NULL, readahead_pos(rac));
 	struct folio *head = NULL, *folio;
 	unsigned int nrpages = readahead_count(rac);
+	struct erofs_read_ctx rdctx = {
+		.file = rac->file,
+		.inode = inode,
+	};
 	int err;
 
+	erofs_read_begin(&rdctx);
+
+	if (erofs_is_ishare_inode(inode))
+		realinode = erofs_ishare_iget(inode);
+	else
+		realinode = inode;
+
+	f.inode = realinode;
 	z_erofs_pcluster_readmore(&f, rac, true);
 	nrpages = readahead_count(rac);
-	trace_erofs_readpages(inode, readahead_index(rac), nrpages, false);
+	trace_erofs_readpages(realinode, readahead_index(rac), nrpages, false);
 	while ((folio = readahead_folio(rac))) {
 		folio->private = head;
 		head = folio;
@@ -1879,8 +1911,8 @@ static void z_erofs_readahead(struct readahead_control *rac)
 
 		err = z_erofs_scan_folio(&f, folio, true);
 		if (err && err != -EINTR)
-			erofs_err(inode->i_sb, "readahead error at folio %lu @ nid %llu",
-				  folio->index, EROFS_I(inode)->nid);
+			erofs_err(realinode->i_sb, "readahead error at folio %lu @ nid %llu",
+				  folio->index, EROFS_I(realinode)->nid);
 	}
 	z_erofs_pcluster_readmore(&f, rac, false);
 	z_erofs_pcluster_end(&f);
@@ -1888,6 +1920,11 @@ static void z_erofs_readahead(struct readahead_control *rac)
 	(void)z_erofs_runqueue(&f, nrpages);
 	erofs_put_metabuf(&f.map.buf);
 	erofs_release_pages(&f.pagepool);
+
+	if (erofs_is_ishare_inode(inode))
+		erofs_ishare_iput(realinode);
+
+	erofs_read_end(&rdctx);
 }
 
 const struct address_space_operations z_erofs_aops = {
Signature · 1 line
2.43.5
#11 lore raw

RFC PATCH v6 7/7 erofs: implement .fadvise for page cache share

In reply to Hongzhen Luo

This patch implements the .fadvise interface for page cache share. Similar to overlayfs, it drops those clean, unused pages through vfs_fadvise().

---
 fs/erofs/ishare.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/erofs/ishare.c +9 -0
index e68bb1a6cf4b..a7c2be5d6f25 100644
@@ -230,6 +230,14 @@ static int erofs_ishare_mmap(struct file *file, struct vm_area_struct *vma)
 	return 0;
 }
 
+static int erofs_ishare_fadvice(struct file *realfile, loff_t offset, loff_t len,
+				int advice)
+{
+	struct file *file = realfile->private_data;
+
+	return vfs_fadvise(file, offset, len, advice);
+}
+
 const struct file_operations erofs_ishare_fops = {
 	.open		= erofs_ishare_file_open,
 	.llseek		= generic_file_llseek,
@@ -238,6 +246,7 @@ const struct file_operations erofs_ishare_fops = {
 	.release	= erofs_ishare_file_release,
 	.get_unmapped_area = thp_get_unmapped_area,
 	.splice_read	= filemap_splice_read,
+	.fadvise	= erofs_ishare_fadvice,
 };
 
 void erofs_read_begin(struct erofs_read_ctx *rdctx)
Signature · 1 line
2.43.5