erofs: introduce page cache share feature
cover erofs: introduce page cache share feature
[Background]
================
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.
[Implementation]
================
During the mkfs phase, file content is hashed and the hash value is stored in the user.fingerprint extended attribute. Inodes of files with the same user.fingerprint are mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it. The following describes the relationship between the anonymous inode and inodes with the same content:
page cache
┌────┬────┬────┬──────┐
┌────►│ │ │ ...│ │
│ └────┴────┴────┴──────┘
│
│
│ i_private
┌──┴────────┬───┐
┌─►│ ano_inode │ │
│ └───────────┴─┬─┘
│ │
│ ┌────────┘
mapped │ ▼
to │ ┌──────────┬───┬─────┐
│ │erofs_pcs │cur│ list│
│ └──────────┴─┬─┴───┬─┘
│ │ │
│ ┌─────────┘ │
│ │ │
│ │ ┌──────────┘
│ │ │
│ ▼ ▼
│ ┌────────┐ ┌────────┐ ┌────────┐
└──┤ │ ────► │ │ ───► ──► │ │
│ │ │ │ ... │ │
└────────┘ ◄──── └────────┘ ◄─── ◄── └────────┘ inode_1 inode_2 inode_n
In the above diagram, the i_private (protected by i_lock) field of the anonymous inode points to the struct erofs_pcs structure:
struct erofs_pcs {
struct erofs_inode *cur;
struct rw_semaphore rw_sem;
struct mutex list_mutex;
struct list_head list;
};where the list field points to a list of inodes that are mapped to the anonymous inode and has the same user.fingerprint field. The cur field points to the first inode in the inode list, which is used for I/O mapping (iomap) related operations.
When an inode is created, it is added to the inode list pointed to by the erofs_pcs structure corresponding to the anonymous inode; similarly, when the inode is destroyed, it is removed from the inode list. Note that if the inode is the one pointed to by cur, then it is necessary to acquire the read-write semaphore rw_sem to maintain synchronization, in case the inode is being used for iomap operations elsewhere.
[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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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
Hongzhen Luo (4):
erofs: move struct erofs_anon_fs_type to super.c
erofs: expose erofs_iomap_{begin, end}
erofs: introduce page cache share feature
erofs: apply the page cache share feature
fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/data.c | 9 +- fs/erofs/fscache.c | 13 +- fs/erofs/inode.c | 17 ++ fs/erofs/internal.h | 8 + fs/erofs/pagecache_share.c | 318 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 23 +++ fs/erofs/super.c | 40 +++++ 9 files changed, 425 insertions(+), 14 deletions(-) create mode 100644 fs/erofs/pagecache_share.c create mode 100644 fs/erofs/pagecache_share.h
Signature · 1 line
2.43.5
RFC PATCH 1/4 erofs: move `struct erofs_anon_fs_type` to super.c
Prepare 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
@@ -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
@@ -387,6 +387,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
@@ -10,6 +10,7 @@#include <linux/fs_context.h> #include <linux/fs_parser.h> #include <linux/exportfs.h> +#include <linux/pseudo_fs.h> #include "xattr.h" #define CREATE_TRACE_POINTS
@@ -832,6 +833,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
RFC PATCH 2/4 erofs: expose erofs_iomap_{begin, end}
In reply to Hongzhen LuoExpose erofs_iomap_{begin, end} to prepare for the implementation of the page cache share feature.
--- fs/erofs/data.c | 4 ++-- fs/erofs/internal.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-)
fs/erofs/data.c +2 -2
@@ -249,7 +249,7 @@ int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)return 0 ; } -static int erofs_iomap_begin (struct inode *inode,loff_t offset,loff_t length, +int erofs_iomap_begin (struct inode *inode,loff_t offset,loff_t length,unsigned int flags,struct iomap *iomap,struct iomap *srcmap) {int ret;
@@ -308,7 +308,7 @@ static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,return 0 ; } -static int erofs_iomap_end (struct inode *inode,loff_t pos,loff_t length, +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;
fs/erofs/internal.h +5 -0
@@ -389,6 +389,11 @@ extern const struct iomap_ops z_erofs_iomap_report_ops;extern struct file_system_type erofs_anon_fs_type; +extern int erofs_iomap_begin (struct inode *inode,loff_t offset,loff_t length, +unsigned int flags,struct iomap *iomap,struct iomap *srcmap); +extern int erofs_iomap_end (struct inode *inode,loff_t pos,loff_t length, +ssize_t written,unsigned int flags,struct iomap *iomap); +/* flags for erofs_fscache_register_cookie() */ #define EROFS_REG_COOKIE_SHARE 0x0001#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
Signature · 1 line
2.43.5
RFC PATCH 3/4 erofs: introduce page cache share feature
In reply to Hongzhen LuoCurrently, 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. During the mkfs phase, file content is hashed and the hash value is stored in the user.fingerprint extended attribute. Inodes of files with the same user.fingerprint are mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.
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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+
--- fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/internal.h | 8 + fs/erofs/pagecache_share.c | 313 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 20 +++ 5 files changed, 352 insertions(+) create mode 100644 fs/erofs/pagecache_share.c create mode 100644 fs/erofs/pagecache_share.h
fs/erofs/Kconfig +10 -0
@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI at higher priority. If unsure, say N. + +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.
fs/erofs/Makefile +1 -0
@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o +erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.o
fs/erofs/internal.h +8 -0
@@ -288,6 +288,12 @@ struct erofs_inode { };#endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct list_head pcs_list; +char *fprt; +int fprt_len; +unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };
@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops;extern const struct address_space_operations erofs_raw_access_aops;extern const struct address_space_operations z_erofs_aops;extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops;extern const struct inode_operations erofs_generic_iops;extern const struct inode_operations erofs_symlink_iops;
@@ -384,6 +391,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_pcs_file_fops;extern const struct iomap_ops z_erofs_iomap_report_ops;
fs/erofs/pagecache_share.c new file +313 -0
313 lines not on this page · raw message
fs/erofs/pagecache_share.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#ifndef __EROFS_PAGECACHE_SHARE_H +#define __EROFS_PAGECACHE_SHARE_H + +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/rwlock.h> +#include <linux/mutex.h> +#include "internal.h" + +int erofs_pcs_init_mnt (void ); +int erofs_pcs_free_mnt (void ); +void erofs_pcs_fill_inode (struct inode *inode); +int erofs_pcs_add (struct inode *inode); +int erofs_pcs_remove (struct inode *inode); + +#endif
Signature · 1 line
2.43.5
Quoted 11 lines · 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. During the mkfs phase, file content is hashed and the hash value is stored in the
user.fingerprintextended attribute. Inodes of files with the same
Does this mean the hash is calculated at the file granularity?
Quoted 150 lines
user.fingerprintare mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>--- fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/internal.h | 8 + fs/erofs/pagecache_share.c | 313 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 20 +++ 5 files changed, 352 insertions(+)create mode 100644
fs/erofs/pagecache_share.c
create mode 100644fs/erofs/pagecache_share.hfs/erofs/Kconfig +10
-0@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI at higher priority. If unsure, say N. + +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.fs/erofs/Makefile +1
-0@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o +erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.ofs/erofs/internal.h +8
-0@@ -288,6 +288,12 @@ struct erofs_inode { };#endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct list_head pcs_list; +char *fprt; +int fprt_len; +unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops;extern const struct address_space_operations erofs_raw_access_aops;extern const struct address_space_operations z_erofs_aops;extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops;extern const struct inode_operations erofs_generic_iops;extern const struct inode_operations erofs_symlink_iops;@@ -384,6 +391,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_pcs_file_fops;extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +5
-0@@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xxhash.h>
fs/erofs/pagecache_share.c line 15
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+#include "pagecache_share.h" +#include "internal.h" +#include "xattr.h" + +#define PCS_FPRT_IDX 1 +#define PCS_FPRT_NAME "fingerprint" +#define PCS_FPRT_MAXLEN 1024 + +struct erofs_pcs { +struct erofs_inode *cur;
Why we should cur to pointer the inode? I think we only need to record the fprt of the content. When we need inode, we can check the list_empty, and to get the any inode to use.
Quoted 5 lines
fs/erofs/pagecache_share.c new file +5
-0+struct rw_semaphore rw_sem; +struct mutex list_mutex; +struct list_head list; +}; +
fs/erofs/pagecache_share.c line 30
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+static DEFINE_MUTEX (pseudo_mnt_lock); +static unsigned long pseudo_mnt_count; +static struct vfsmount *erofs_pcs_mnt; + +int erofs_pcs_init_mnt (void ) +{ +mutex_lock (&pseudo_mnt_lock); +if (!erofs_pcs_mnt) { + erofs_pcs_mnt =kern_mount (&erofs_anon_fs_type); +if (IS_ERR (erofs_pcs_mnt))
Not unlock here.
Quoted 37 lines
fs/erofs/pagecache_share.c new file +37
-0+return PTR_ERR (erofs_pcs_mnt); + pseudo_mnt_count =1 ; + }else + pseudo_mnt_count +=1 ; +mutex_unlock (&pseudo_mnt_lock); +return 0 ; +} + +int erofs_pcs_free_mnt (void ) +{ +mutex_lock (&pseudo_mnt_lock); + pseudo_mnt_count -=1 ; +if (pseudo_mnt_count <0 ) +return -EINVAL ; +if (pseudo_mnt_count ==0 ) { +kern_unmount (erofs_pcs_mnt); + erofs_pcs_mnt =NULL ; + } +mutex_unlock (&pseudo_mnt_lock); +return 0 ; +} + +void erofs_pcs_fill_inode (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode); +char fprt[PCS_FPRT_MAXLEN ]; + + vi->fprt_len =erofs_getxattr (inode,PCS_FPRT_IDX ,PCS_FPRT_NAME , fprt, +PCS_FPRT_MAXLEN ); +if (vi->fprt_len >0 && vi->fprt_len <=PCS_FPRT_MAXLEN ) { + vi->fprt =kmalloc (vi->fprt_len,GFP_KERNEL ); +if (IS_ERR (vi->fprt)) { + vi->fprt_len =-1 ; +return ; + } +memcpy (vi->fprt, fprt, vi->fprt_len); + vi->fprt_hash =xxh32 (vi->fprt, vi->fprt_len,0 );
fs/erofs/pagecache_share.c line 77
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ } +} + +static int erofs_pcs_eq (struct inode *ano_inode,void *data) +{ +struct erofs_pcs *pcs = ano_inode->i_private; +struct erofs_inode *vi = (struct erofs_inode *)data; + +return pcs && pcs->cur && pcs->cur->fprt_len == vi->fprt_len && +memcmp (pcs->cur->fprt, vi->fprt, vi->fprt_len) ==0 ?1 :0 ;
May be we just need compare vi->fprt_hash, no need memcmp?
Quoted 11 lines
fs/erofs/pagecache_share.c new file +11
-0+} + +static int erofs_pcs_set_aops (struct inode *ano_inode,void *data) +{ + ano_inode->i_mapping->a_ops = &erofs_pcs_aops; + ano_inode->i_size = ((struct erofs_inode *)data)->vfs_inode.i_size; +return 0 ; +} + +int erofs_pcs_add (struct inode *inode) +{
fs/erofs/pagecache_share.c line 98
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+struct erofs_inode *vi =EROFS_I (inode); +struct inode *ano_inode; +struct erofs_pcs *pcs; + + ano_inode =iget5_locked (erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); +spin_lock (&ano_inode->i_lock); +if (!ano_inode->i_private) { + pcs =kzalloc (sizeof (struct erofs_pcs ),GFP_KERNEL ); +if (IS_ERR (pcs))
should call spin_unlock when failed
fs/erofs/pagecache_share.c line 106
Quoted 8 lines
fs/erofs/pagecache_share.c new file +8
-0+return -ENOMEM ; +INIT_LIST_HEAD (&pcs->list); +mutex_init (&pcs->list_mutex); +init_rwsem (&pcs->rw_sem); + ano_inode->i_private = pcs; + } +spin_unlock (&ano_inode->i_lock); + pcs = ano_inode->i_private;
pcs = ano_inode->i_private;spin_lock(&ano_inode->i_lock);
if (!pcs) {
xxx
}spin_unlock(&ano_inode->i_lock);
Quoted 82 lines
fs/erofs/pagecache_share.c new file +82
-0+mutex_lock (&pcs->list_mutex); +list_add_tail (&vi->pcs_list, &pcs->list); +if (!pcs->cur) { + pcs->cur =list_first_entry (&pcs->list, + struct erofs_inode, pcs_list); + } +mutex_unlock (&pcs->list_mutex); +if (ano_inode->i_state &I_NEW ) +unlock_new_inode (ano_inode); +return 0 ; +} + +int erofs_pcs_remove (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode), *p, *tmp; +struct inode *ano_inode; +struct erofs_pcs *pcs; + + ano_inode =iget5_locked (erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); +iput (ano_inode); + pcs = ano_inode->i_private; +if (!pcs) +return -EINVAL ; +mutex_lock (&pcs->list_mutex); +if (vi == pcs->cur) { +/* synchronize with reads using pcs->cur */ +down_write (&pcs->rw_sem); +list_del (&pcs->cur->pcs_list); +if (list_empty (&pcs->list)) { + pcs->cur =NULL ; +up_write (&pcs->rw_sem); + goto free_ano_inode; + } + pcs->cur = list_first_entry(&pcs->list, struct erofs_inode, + pcs_list); +up_write (&pcs->rw_sem); + }else { +list_for_each_entry_safe (p, tmp, &pcs->list, pcs_list) { +if (p == vi) { +list_del (&p->pcs_list); +break ; + } + } + } +iput (ano_inode); +mutex_unlock (&pcs->list_mutex); +return 0 ; + +free_ano_inode: +mutex_unlock (&pcs->list_mutex); +spin_lock (&ano_inode->i_lock); +if (atomic_read (&ano_inode->i_count) ==1 ) { + ano_inode->i_private =NULL ; +kfree (pcs); + } +spin_unlock (&ano_inode->i_lock); +iput (ano_inode); +return 0 ; +} + +static int erofs_pcs_iomap_begin (struct inode *ano_inode,loff_t offset, +loff_t length,unsigned int flags, +struct iomap *iomap,struct iomap *srcmap) +{ +struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; +if (!pcs || !pcs->cur) +return -EINVAL ; + +return erofs_iomap_begin (&pcs->cur->vfs_inode, offset, length, flags, + iomap, srcmap); +} + +static int erofs_pcs_iomap_end (struct inode *ano_inode,loff_t pos, +loff_t length,ssize_t written, +unsigned int flags,struct iomap *iomap) +{ +struct erofs_pcs *pcs; + + pcs = ano_inode->i_private;
fs/erofs/pagecache_share.c line 198
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+if (!pcs) +return -EINVAL ; + +return erofs_iomap_end (&pcs->cur->vfs_inode, pos, length, written, + flags, iomap); +} + +const struct iomap_ops erofs_pcs_iomap_ops = { + .iomap_begin = erofs_pcs_iomap_begin, + .iomap_end = erofs_pcs_iomap_end,
Does this work on on-demand mode over fscache? As I know, this io path is only for erofs, not for erofs over fscache.
Quoted 131 lines
fs/erofs/pagecache_share.c new file +115
-0+}; + +static int erofs_pcs_read_folio (struct file *file,struct folio *folio) +{ +int err; +struct erofs_pcs *pcs; + + pcs = file->f_inode->i_private; +if (!pcs) +return -EINVAL ; +down_read (&pcs->rw_sem); + err =iomap_read_folio (folio, &erofs_pcs_iomap_ops); +up_read (&pcs->rw_sem); +return err; +} + +static void erofs_pcs_readahead (struct readahead_control *rac) +{ +struct erofs_pcs *pcs; + + pcs = rac->file->f_inode->i_private; +if (!pcs) +return ; +down_read (&pcs->rw_sem); +iomap_readahead (rac, &erofs_pcs_iomap_ops); +up_read (&pcs->rw_sem); +} + +const struct address_space_operations erofs_pcs_aops = { + .read_folio = erofs_pcs_read_folio, + .readahead = erofs_pcs_readahead, +}; + +static struct file *erofs_pcs_alloc_file (struct file *file, +struct inode *ano_inode) +{ +struct file *ano_file; + + ano_file =alloc_file_pseudo (ano_inode, erofs_pcs_mnt,"[erofs_pcs_f]" , +O_RDONLY , &erofs_file_fops); +file_ra_state_init (&ano_file->f_ra, file->f_mapping); +return ano_file; +} + +static int erofs_pcs_file_open (struct inode *inode,struct file *file) +{ +struct file *ano_file; +struct inode *ano_inode; +struct erofs_inode *vi =EROFS_I (inode); + + ano_inode =iget5_locked (erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); +if (!ano_inode) +return -EINVAL ; + ano_file =erofs_pcs_alloc_file (file, ano_inode); + file->private_data = (void *)ano_file; +return 0 ; +} + +static int erofs_pcs_file_release (struct inode *inode,struct file *file) +{ +if (!file->private_data) +return -EINVAL ; +fput ((struct file *)file->private_data); + file->private_data =NULL ; +return 0 ; +} + +static ssize_t erofs_pcs_file_read_iter (struct kiocb *iocb, +struct iov_iter *to) +{ +struct file *file, *ano_file; +struct kiocb ano_iocb; +ssize_t res; + +memcpy (&ano_iocb, iocb,sizeof (struct kiocb )); + file = iocb->ki_filp; + ano_file = file->private_data; +if (!ano_file) +return -EINVAL ; + ano_iocb.ki_filp = ano_file; + res =filemap_read (&ano_iocb, to,0 ); +memcpy (iocb, &ano_iocb,sizeof (struct kiocb )); + iocb->ki_filp = file; +file_accessed (file); +return res; +} + +static vm_fault_t erofs_pcs_fault (struct vm_fault *vmf) +{ +return filemap_fault (vmf); +} + +static const struct vm_operations_struct erofs_pcs_file_vm_ops = { + .fault = erofs_pcs_fault, +}; + +static int erofs_pcs_mmap (struct file *file,struct vm_area_struct *vma) +{ +struct file *ano_file = file->private_data; + +vma_set_file (vma, ano_file); + vma->vm_ops = &erofs_pcs_file_vm_ops; +return 0 ; +} + +const struct file_operations erofs_pcs_file_fops = { + .open = erofs_pcs_file_open, + .llseek = generic_file_llseek, + .read_iter = erofs_pcs_file_read_iter, + .mmap = erofs_pcs_mmap, + .release = erofs_pcs_file_release, + .get_unmapped_area = thp_get_unmapped_area, + .splice_read = filemap_splice_read, +};fs/erofs/pagecache_share.h new file +10
-0@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#ifndef __EROFS_PAGECACHE_SHARE_H +#define __EROFS_PAGECACHE_SHARE_H + +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/rwlock.h>
fs/erofs/pagecache_share.h line 20
Quoted 10 lines
fs/erofs/pagecache_share.h new file +10
-0+#include <linux/mutex.h> +#include "internal.h" + +int erofs_pcs_init_mnt (void ); +int erofs_pcs_free_mnt (void ); +void erofs_pcs_fill_inode (struct inode *inode); +int erofs_pcs_add (struct inode *inode); +int erofs_pcs_remove (struct inode *inode); + +#endif
Thanks,
Hongbo
Hi all,
Quoted 11 lines · Hongbo Li via Linux-erofs
Quoted 8 lines · 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.
Quoted 5 lines
Quoted 3 lines
This introduces the page cache share feature in erofs. During the mkfs phase, file content is hashed and the hash value is stored in the
user.fingerprintextended attribute. Inodes of files with the sameDoes this mean the hash is calculated at the file granularity?
Yes, it should share in the file granularity due to various MM limitations, but for strictly immutable fs like EROFS, that is pretty easy to go like this. Also it doesn't matter since such fingerprint is just an extended attribute so even Linux future versions support finer granularity, it should backward compatiable.
But to Hongzhen, I think you need to rename it as "trusted.erofs.fingerprint" instead since user xattrs are unacceptable for this.
Also I don't have enough time to review this version (working on other stuffs now) this month, there are many details needs to be reworked.
So Hongbo, if you have interest and time to review and improve this work, I greatly appreciate it.
Quoted 156 lines
Quoted 145 lines
user.fingerprintare mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
fs/erofs/Kconfig | 10 ++
fs/erofs/Makefile | 1 +
fs/erofs/internal.h | 8 +fs/erofs/pagecache_share.c| 313 +++++++++++++++++++++++++++++++++++++fs/erofs/pagecache_share.h| 20 +++5 files changed, 352 insertions(+)create mode 100644
fs/erofs/pagecache_share.c
create mode 100644fs/erofs/pagecache_share.hfs/erofs/Kconfig +0
-0@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRIat higher priority.
If unsure, say N.fs/erofs/Kconfig +10
-0+ +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.fs/erofs/Makefile +0
-0@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.oerofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/Makefile +1
-0+erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.ofs/erofs/internal.h +8
-0@@ -288,6 +288,12 @@ struct erofs_inode { }; #endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + struct list_head pcs_list; + char *fprt; + int fprt_len; + unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops; extern const struct address_space_operations erofs_raw_access_aops; extern const struct address_space_operations z_erofs_aops; extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops; extern const struct inode_operations erofs_generic_iops; extern const struct inode_operations erofs_symlink_iops;@@ -384,6 +391,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_pcs_file_fops; extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +5
-0@@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xxhash.h> fs/erofs/pagecache_share.c line 15
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+#include "pagecache_share.h" +#include "internal.h" +#include "xattr.h" + +#define PCS_FPRT_IDX 1 +#define PCS_FPRT_NAME "fingerprint" +#define PCS_FPRT_MAXLEN 1024 + +struct erofs_pcs { + struct erofs_inode *cur;Why we should cur to pointer the inode? I think we only need to record the fprt of the content. When we need inode, we can check the
list_empty, and to get the any inode to use.
Quoted 16 lines
Quoted 5 lines
fs/erofs/pagecache_share.c new file +5
-0+ struct rw_semaphore rw_sem; + struct mutex list_mutex; + struct list_head list; +}; +fs/erofs/pagecache_share.c line 30
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+static DEFINE_MUTEX (pseudo_mnt_lock); +static unsigned long pseudo_mnt_count; +static struct vfsmount *erofs_pcs_mnt; + +int erofs_pcs_init_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + if (!erofs_pcs_mnt) { + erofs_pcs_mnt = kern_mount(&erofs_anon_fs_type); + if (IS_ERR(erofs_pcs_mnt))Not unlock here.
Also I don't think it needs another kern_mount just for this.
You need to refactor the existing code in advance.
Quoted 169 lines
Quoted 37 lines
fs/erofs/pagecache_share.c new file +37
-0+ return PTR_ERR(erofs_pcs_mnt); + pseudo_mnt_count = 1; + } else + pseudo_mnt_count += 1; + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +int erofs_pcs_free_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + pseudo_mnt_count -= 1; + if (pseudo_mnt_count < 0) + return -EINVAL; + if (pseudo_mnt_count == 0) { + kern_unmount(erofs_pcs_mnt); + erofs_pcs_mnt = NULL; + } + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +void erofs_pcs_fill_inode (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + char fprt[PCS_FPRT_MAXLEN]; + + vi->fprt_len = erofs_getxattr(inode, PCS_FPRT_IDX, PCS_FPRT_NAME, fprt, + PCS_FPRT_MAXLEN); + if (vi->fprt_len > 0 && vi->fprt_len <= PCS_FPRT_MAXLEN) { + vi->fprt = kmalloc(vi->fprt_len, GFP_KERNEL); + if (IS_ERR(vi->fprt)) { + vi->fprt_len = -1; + return; + } + memcpy(vi->fprt, fprt, vi->fprt_len); + vi->fprt_hash = xxh32(vi->fprt, vi->fprt_len, 0);fs/erofs/pagecache_share.c line 77
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ } +} + +static int erofs_pcs_eq (struct inode *ano_inode,void *data) +{ + struct erofs_pcs *pcs = ano_inode->i_private; + struct erofs_inode *vi = (struct erofs_inode *)data; + + return pcs && pcs->cur && pcs->cur->fprt_len == vi->fprt_len && + memcmp(pcs->cur->fprt, vi->fprt, vi->fprt_len) == 0 ? 1 : 0;May be we just need compare
vi->fprt_hash, no need memcmp?Quoted 11 lines
fs/erofs/pagecache_share.c new file +11
-0+} + +static int erofs_pcs_set_aops (struct inode *ano_inode,void *data) +{ + ano_inode->i_mapping->a_ops = &erofs_pcs_aops; + ano_inode->i_size = ((struct erofs_inode *)data)->vfs_inode.i_size; + return 0; +} + +int erofs_pcs_add (struct inode *inode) +{fs/erofs/pagecache_share.c line 98
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ struct erofs_inode *vi = EROFS_I(inode); + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + spin_lock(&ano_inode->i_lock); + if (!ano_inode->i_private) { + pcs = kzalloc(sizeof(struct erofs_pcs), GFP_KERNEL); + if (IS_ERR(pcs))should call
spin_unlockwhen failedfs/erofs/pagecache_share.c line 106
Quoted 8 lines
fs/erofs/pagecache_share.c new file +8
-0+ return -ENOMEM; + INIT_LIST_HEAD(&pcs->list); + mutex_init(&pcs->list_mutex); + init_rwsem(&pcs->rw_sem); + ano_inode->i_private = pcs; + } + spin_unlock(&ano_inode->i_lock); + pcs = ano_inode->i_private;pcs =
ano_inode->i_private;spin_lock(&ano_inode->i_lock);
if (!pcs) {
xxx
}spin_unlock(&ano_inode->i_lock);Quoted 85 lines
fs/erofs/pagecache_share.c new file +85
-0+ mutex_lock(&pcs->list_mutex); + list_add_tail(&vi->pcs_list, &pcs->list); + if (!pcs->cur) { + pcs->cur = list_first_entry(&pcs->list, + struct erofs_inode, pcs_list); + } + mutex_unlock(&pcs->list_mutex); + if (ano_inode->i_state & I_NEW) + unlock_new_inode(ano_inode); + return 0; +} + +int erofs_pcs_remove (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode), *p, *tmp; + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + iput(ano_inode); + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; + mutex_lock(&pcs->list_mutex); + if (vi == pcs->cur) { + /* synchronize with reads using pcs->cur */ + down_write(&pcs->rw_sem); + list_del(&pcs->cur->pcs_list); + if (list_empty(&pcs->list)) { + pcs->cur = NULL; + up_write(&pcs->rw_sem); + goto free_ano_inode; + } + pcs->cur = list_first_entry(&pcs->list, struct erofs_inode, + pcs_list); + up_write(&pcs->rw_sem); + } else { + list_for_each_entry_safe(p, tmp, &pcs->list, pcs_list) { + if (p == vi) { + list_del(&p->pcs_list); + break; + } + } + } + iput(ano_inode); + mutex_unlock(&pcs->list_mutex); + return 0; + +free_ano_inode: + mutex_unlock(&pcs->list_mutex); + spin_lock(&ano_inode->i_lock); + if (atomic_read(&ano_inode->i_count) == 1) { + ano_inode->i_private = NULL; + kfree(pcs); + } + spin_unlock(&ano_inode->i_lock); + iput(ano_inode); + return 0; +} + +static int erofs_pcs_iomap_begin (struct inode *ano_inode,loff_t offset, + loff_t length, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs || !pcs->cur) + return -EINVAL; + + return erofs_iomap_begin(&pcs->cur->vfs_inode, offset, length, flags, + iomap, srcmap); +} + +static int erofs_pcs_iomap_end (struct inode *ano_inode,loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; +
Quoted 8 lines
fs/erofs/pagecache_share.c line 198
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+ return erofs_iomap_end(&pcs->cur->vfs_inode, pos, length, written, + flags, iomap); +} + +const struct iomap_ops erofs_pcs_iomap_ops = { + .iomap_begin = erofs_pcs_iomap_begin, + .iomap_end = erofs_pcs_iomap_end,Does this work on on-demand mode over fscache? As I know, this io path is only for erofs, not for erofs over fscache.
That is why I think this implementation is far from upstreaming:
- over fscache
- compression
are all needed to be implemented.
I don't think it needs to add any extra helpers just for this, you need to handle page cache sharing in the original paths to find the share inodes instead.
Thanks,
Gao Xiang
Quoted 30 lines · Gao Xiang
Hi all,
Quoted 11 lines · Hongbo Li via Linux-erofs
Quoted 8 lines · 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.
Quoted 5 lines
Quoted 3 lines
This introduces the page cache share feature in erofs. During the mkfs phase, file content is hashed and the hash value is stored in the
user.fingerprintextended attribute. Inodes of files with the sameDoes this mean the hash is calculated at the file granularity?
Yes, it should share in the file granularity due to various MM limitations, but for strictly immutable fs like EROFS, that is pretty easy to go like this. Also it doesn't matter since such fingerprint is just an extended attribute so even Linux future versions support finer granularity, it should backward compatiable.
But to Hongzhen, I think you need to rename it as "trusted.erofs.fingerprint" instead since user xattrs are unacceptable for this.
Quoted 6 lines
Also I don't have enough time to review this version (working on other stuffs now) this month, there are many details needs to be reworked.
So Hongbo, if you have interest and time to review and improve this work, I greatly appreciate it.
ok, I will follow this.
Quoted 359 lines
Quoted 156 lines
Quoted 145 lines
user.fingerprintare mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
fs/erofs/Kconfig | 10 ++
fs/erofs/Makefile | 1 +
fs/erofs/internal.h | 8 +fs/erofs/pagecache_share.c| 313 +++++++++++++++++++++++++++++++++++++fs/erofs/pagecache_share.h| 20 +++5 files changed, 352 insertions(+)create mode 100644
fs/erofs/pagecache_share.c
create mode 100644fs/erofs/pagecache_share.hfs/erofs/Kconfig +0
-0@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRIat higher priority.
If unsure, say N.fs/erofs/Kconfig +10
-0+ +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.fs/erofs/Makefile +0
-0@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.oerofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/Makefile +1
-0+erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.ofs/erofs/internal.h +8
-0@@ -288,6 +288,12 @@ struct erofs_inode { }; #endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + struct list_head pcs_list; + char *fprt; + int fprt_len; + unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops; extern const struct address_space_operations erofs_raw_access_aops; extern const struct address_space_operations z_erofs_aops; extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops; extern const struct inode_operations erofs_generic_iops; extern const struct inode_operations erofs_symlink_iops;@@ -384,6 +391,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_pcs_file_fops; extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +5
-0@@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xxhash.h> fs/erofs/pagecache_share.c line 15
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+#include "pagecache_share.h" +#include "internal.h" +#include "xattr.h" + +#define PCS_FPRT_IDX 1 +#define PCS_FPRT_NAME "fingerprint" +#define PCS_FPRT_MAXLEN 1024 + +struct erofs_pcs { + struct erofs_inode *cur;Why we should cur to pointer the inode? I think we only need to record the fprt of the content. When we need inode, we can check the
list_empty, and to get the any inode to use.Quoted 16 lines
Quoted 5 lines
fs/erofs/pagecache_share.c new file +5
-0+ struct rw_semaphore rw_sem; + struct mutex list_mutex; + struct list_head list; +}; +fs/erofs/pagecache_share.c line 30
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+static DEFINE_MUTEX (pseudo_mnt_lock); +static unsigned long pseudo_mnt_count; +static struct vfsmount *erofs_pcs_mnt; + +int erofs_pcs_init_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + if (!erofs_pcs_mnt) { + erofs_pcs_mnt = kern_mount(&erofs_anon_fs_type); + if (IS_ERR(erofs_pcs_mnt))Not unlock here.
Also I don't think it needs another
kern_mountjust for this.You need to refactor the existing code in advance.
Quoted 172 lines
Quoted 39 lines
fs/erofs/pagecache_share.c new file +28
-0+ return PTR_ERR(erofs_pcs_mnt); + pseudo_mnt_count = 1; + } else + pseudo_mnt_count += 1; + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +int erofs_pcs_free_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + pseudo_mnt_count -= 1; + if (pseudo_mnt_count < 0) + return -EINVAL; + if (pseudo_mnt_count == 0) { + kern_unmount(erofs_pcs_mnt); + erofs_pcs_mnt = NULL; + } + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +void erofs_pcs_fill_inode (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + char fprt[PCS_FPRT_MAXLEN]; + + vi->fprt_len = erofs_getxattr(inode, PCS_FPRT_IDX,
PCS_FPRT_NAME, fprt,
+PCS_FPRT_MAXLEN);
+ if (vi->fprt_len> 0 &&vi->fprt_len<=PCS_FPRT_MAXLEN) { +vi->fprt=kmalloc(vi->fprt_len, GFP_KERNEL); + if (IS_ERR(vi->fprt)) {
+vi->fprt_len= -1;
+ return;
+ }
+memcpy(vi->fprt, fprt, vi->fprt_len);
+vi->fprt_hash=xxh32(vi->fprt, vi->fprt_len, 0); + }fs/erofs/pagecache_share.c line 77
Quoted 9 lines
fs/erofs/pagecache_share.c new file +9
-0+} + +static int erofs_pcs_eq (struct inode *ano_inode,void *data) +{ + struct erofs_pcs *pcs = ano_inode->i_private; + struct erofs_inode *vi = (struct erofs_inode *)data; + + return pcs && pcs->cur && pcs->cur->fprt_len == vi->fprt_len && + memcmp(pcs->cur->fprt, vi->fprt, vi->fprt_len) == 0 ? 1 : 0;May be we just need compare
vi->fprt_hash, no need memcmp?Quoted 11 lines
fs/erofs/pagecache_share.c new file +11
-0+} + +static int erofs_pcs_set_aops (struct inode *ano_inode,void *data) +{ + ano_inode->i_mapping->a_ops = &erofs_pcs_aops; + ano_inode->i_size = ((struct erofs_inode *)data)->vfs_inode.i_size; + return 0; +} + +int erofs_pcs_add (struct inode *inode) +{fs/erofs/pagecache_share.c line 98
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ struct erofs_inode *vi = EROFS_I(inode); + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + spin_lock(&ano_inode->i_lock); + if (!ano_inode->i_private) { + pcs = kzalloc(sizeof(struct erofs_pcs), GFP_KERNEL); + if (IS_ERR(pcs))should call
spin_unlockwhen failedQuoted 8 lines
+ return
-ENOMEM;
+INIT_LIST_HEAD(&pcs->list);
+mutex_init(&pcs->list_mutex);
+init_rwsem(&pcs->rw_sem);
+ano_inode->i_private= pcs;
+ }
+spin_unlock(&ano_inode->i_lock);
+ pcs =ano_inode->i_private;pcs =
ano_inode->i_private;spin_lock(&ano_inode->i_lock);
if (!pcs) {
xxx
}spin_unlock(&ano_inode->i_lock);Quoted 87 lines
+
mutex_lock(&pcs->list_mutex);
+list_add_tail(&vi->pcs_list, &pcs->list); + if (!pcs->cur) {
+pcs->cur=list_first_entry(&pcs->list, + struct erofs_inode, pcs_list); + }
+mutex_unlock(&pcs->list_mutex);
+ if (ano_inode->i_state&I_NEW)
+unlock_new_inode(ano_inode);
+ return 0;fs/erofs/pagecache_share.c new file +52
-0+} + +int erofs_pcs_remove (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode), *p, *tmp; + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + iput(ano_inode); + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; + mutex_lock(&pcs->list_mutex); + if (vi == pcs->cur) { + /* synchronize with reads using pcs->cur */ + down_write(&pcs->rw_sem); + list_del(&pcs->cur->pcs_list); + if (list_empty(&pcs->list)) { + pcs->cur = NULL; + up_write(&pcs->rw_sem); + goto free_ano_inode; + } + pcs->cur = list_first_entry(&pcs->list, struct erofs_inode, + pcs_list); + up_write(&pcs->rw_sem); + } else { + list_for_each_entry_safe(p, tmp, &pcs->list, pcs_list) { + if (p == vi) { + list_del(&p->pcs_list); + break; + } + } + } + iput(ano_inode); + mutex_unlock(&pcs->list_mutex); + return 0; + +free_ano_inode: + mutex_unlock(&pcs->list_mutex); + spin_lock(&ano_inode->i_lock); + if (atomic_read(&ano_inode->i_count) == 1) { + ano_inode->i_private = NULL; + kfree(pcs); + } + spin_unlock(&ano_inode->i_lock); + iput(ano_inode); + return 0; +} + +static int erofs_pcs_iomap_begin(struct inode *ano_inode, loff_toffset,
+loff_tlength, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap)fs/erofs/pagecache_share.c new file +8
-0+{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs || !pcs->cur) + return -EINVAL; + + return erofs_iomap_begin(&pcs->cur->vfs_inode, offset, length,flags,
+ iomap, srcmap);fs/erofs/pagecache_share.c new file +12
-0+} + +static int erofs_pcs_iomap_end (struct inode *ano_inode,loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; +Quoted 8 lines
fs/erofs/pagecache_share.c line 198
Quoted 7 lines
+ return
erofs_iomap_end(&pcs->cur->vfs_inode, pos, length, written, + flags, iomap);fs/erofs/pagecache_share.c new file +5
-0+} + +const struct iomap_ops erofs_pcs_iomap_ops = { + .iomap_begin = erofs_pcs_iomap_begin, + .iomap_end = erofs_pcs_iomap_end,Does this work on on-demand mode over fscache? As I know, this io path is only for erofs, not for erofs over fscache.
Quoted 9 lines
That is why I think this implementation is far from upstreaming:
- over fscache
- compression
are all needed to be implemented.I don't think it needs to add any extra helpers just for this, you need to handle page cache sharing in the original paths to find the share inodes instead.
I remember that Jingbo have done the similar thing like this. At: https://patchew.org/linux/[email protected]/
But I don't know what happened afterward.
Thanks,
Hongbo
Quoted 2 lines
Thanks,
Gao Xiang
Quoted 33 lines · Hongbo Li
Quoted 30 lines · Gao Xiang
Hi all,
Quoted 11 lines · Hongbo Li via Linux-erofs
Quoted 8 lines · 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.
Quoted 5 lines
Quoted 3 lines
This introduces the page cache share feature in erofs. During the mkfs phase, file content is hashed and the hash value is stored in the
user.fingerprintextended attribute. Inodes of files with the sameDoes this mean the hash is calculated at the file granularity?
Yes, it should share in the file granularity due to various MM limitations, but for strictly immutable fs like EROFS, that is pretty easy to go like this. Also it doesn't matter since such fingerprint is just an extended attribute so even Linux future versions support finer granularity, it should backward compatiable.
But to Hongzhen, I think you need to rename it as "trusted.erofs.fingerprint" instead since user xattrs are unacceptable for this.
Quoted 7 lines
Quoted 6 lines
Also I don't have enough time to review this version (working on other stuffs now) this month, there are many details needs to be reworked.
So Hongbo, if you have interest and time to review and improve this work, I greatly appreciate it.
ok, I will follow this.
Thanks!
Quoted 362 lines
Quoted 362 lines
Quoted 157 lines
Quoted 146 lines
user.fingerprintare mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
fs/erofs/Kconfig | 10 ++
fs/erofs/Makefile | 1 +
fs/erofs/internal.h | 8 +fs/erofs/pagecache_share.c| 313 +++++++++++++++++++++++++++++++++++++fs/erofs/pagecache_share.h| 20 +++5 files changed, 352 insertions(+)create mode 100644
fs/erofs/pagecache_share.c
create mode 100644fs/erofs/pagecache_share.hfs/erofs/Kconfig +0
-0@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRIat higher priority.
If unsure, say N.fs/erofs/Kconfig +10
-0+ +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.fs/erofs/Makefile +0
-0@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.oerofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/Makefile +1
-0+erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.ofs/erofs/internal.h +8
-0@@ -288,6 +288,12 @@ struct erofs_inode { }; #endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + struct list_head pcs_list; + char *fprt; + int fprt_len; + unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops; extern const struct address_space_operations erofs_raw_access_aops; extern const struct address_space_operations z_erofs_aops; extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops; extern const struct inode_operations erofs_generic_iops; extern const struct inode_operations erofs_symlink_iops;@@ -384,6 +391,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_pcs_file_fops; extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +5
-0@@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xxhash.h> fs/erofs/pagecache_share.c line 15
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+#include "pagecache_share.h" +#include "internal.h" +#include "xattr.h" + +#define PCS_FPRT_IDX 1 +#define PCS_FPRT_NAME "fingerprint" +#define PCS_FPRT_MAXLEN 1024 + +struct erofs_pcs { + struct erofs_inode *cur;Why we should cur to pointer the inode? I think we only need to record the fprt of the content. When we need inode, we can check the
list_empty, and to get the any inode to use.Quoted 16 lines
Quoted 5 lines
fs/erofs/pagecache_share.c new file +5
-0+ struct rw_semaphore rw_sem; + struct mutex list_mutex; + struct list_head list; +}; +fs/erofs/pagecache_share.c line 30
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+static DEFINE_MUTEX (pseudo_mnt_lock); +static unsigned long pseudo_mnt_count; +static struct vfsmount *erofs_pcs_mnt; + +int erofs_pcs_init_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + if (!erofs_pcs_mnt) { + erofs_pcs_mnt = kern_mount(&erofs_anon_fs_type); + if (IS_ERR(erofs_pcs_mnt))Not unlock here.
Also I don't think it needs another
kern_mountjust for this.You need to refactor the existing code in advance.
Quoted 169 lines
Quoted 37 lines
fs/erofs/pagecache_share.c new file +37
-0+ return PTR_ERR(erofs_pcs_mnt); + pseudo_mnt_count = 1; + } else + pseudo_mnt_count += 1; + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +int erofs_pcs_free_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + pseudo_mnt_count -= 1; + if (pseudo_mnt_count < 0) + return -EINVAL; + if (pseudo_mnt_count == 0) { + kern_unmount(erofs_pcs_mnt); + erofs_pcs_mnt = NULL; + } + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +void erofs_pcs_fill_inode (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + char fprt[PCS_FPRT_MAXLEN]; + + vi->fprt_len = erofs_getxattr(inode, PCS_FPRT_IDX, PCS_FPRT_NAME, fprt, + PCS_FPRT_MAXLEN); + if (vi->fprt_len > 0 && vi->fprt_len <= PCS_FPRT_MAXLEN) { + vi->fprt = kmalloc(vi->fprt_len, GFP_KERNEL); + if (IS_ERR(vi->fprt)) { + vi->fprt_len = -1; + return; + } + memcpy(vi->fprt, fprt, vi->fprt_len); + vi->fprt_hash = xxh32(vi->fprt, vi->fprt_len, 0);fs/erofs/pagecache_share.c line 77
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ } +} + +static int erofs_pcs_eq (struct inode *ano_inode,void *data) +{ + struct erofs_pcs *pcs = ano_inode->i_private; + struct erofs_inode *vi = (struct erofs_inode *)data; + + return pcs && pcs->cur && pcs->cur->fprt_len == vi->fprt_len && + memcmp(pcs->cur->fprt, vi->fprt, vi->fprt_len) == 0 ? 1 : 0;May be we just need compare
vi->fprt_hash, no need memcmp?Quoted 11 lines
fs/erofs/pagecache_share.c new file +11
-0+} + +static int erofs_pcs_set_aops (struct inode *ano_inode,void *data) +{ + ano_inode->i_mapping->a_ops = &erofs_pcs_aops; + ano_inode->i_size = ((struct erofs_inode *)data)->vfs_inode.i_size; + return 0; +} + +int erofs_pcs_add (struct inode *inode) +{fs/erofs/pagecache_share.c line 98
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ struct erofs_inode *vi = EROFS_I(inode); + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + spin_lock(&ano_inode->i_lock); + if (!ano_inode->i_private) { + pcs = kzalloc(sizeof(struct erofs_pcs), GFP_KERNEL); + if (IS_ERR(pcs))should call
spin_unlockwhen failedfs/erofs/pagecache_share.c line 106
Quoted 8 lines
fs/erofs/pagecache_share.c new file +8
-0+ return -ENOMEM; + INIT_LIST_HEAD(&pcs->list); + mutex_init(&pcs->list_mutex); + init_rwsem(&pcs->rw_sem); + ano_inode->i_private = pcs; + } + spin_unlock(&ano_inode->i_lock); + pcs = ano_inode->i_private;pcs =
ano_inode->i_private;spin_lock(&ano_inode->i_lock);
if (!pcs) {
xxx
}spin_unlock(&ano_inode->i_lock);Quoted 85 lines
fs/erofs/pagecache_share.c new file +85
-0+ mutex_lock(&pcs->list_mutex); + list_add_tail(&vi->pcs_list, &pcs->list); + if (!pcs->cur) { + pcs->cur = list_first_entry(&pcs->list, + struct erofs_inode, pcs_list); + } + mutex_unlock(&pcs->list_mutex); + if (ano_inode->i_state & I_NEW) + unlock_new_inode(ano_inode); + return 0; +} + +int erofs_pcs_remove (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode), *p, *tmp; + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + iput(ano_inode); + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; + mutex_lock(&pcs->list_mutex); + if (vi == pcs->cur) { + /* synchronize with reads using pcs->cur */ + down_write(&pcs->rw_sem); + list_del(&pcs->cur->pcs_list); + if (list_empty(&pcs->list)) { + pcs->cur = NULL; + up_write(&pcs->rw_sem); + goto free_ano_inode; + } + pcs->cur = list_first_entry(&pcs->list, struct erofs_inode, + pcs_list); + up_write(&pcs->rw_sem); + } else { + list_for_each_entry_safe(p, tmp, &pcs->list, pcs_list) { + if (p == vi) { + list_del(&p->pcs_list); + break; + } + } + } + iput(ano_inode); + mutex_unlock(&pcs->list_mutex); + return 0; + +free_ano_inode: + mutex_unlock(&pcs->list_mutex); + spin_lock(&ano_inode->i_lock); + if (atomic_read(&ano_inode->i_count) == 1) { + ano_inode->i_private = NULL; + kfree(pcs); + } + spin_unlock(&ano_inode->i_lock); + iput(ano_inode); + return 0; +} + +static int erofs_pcs_iomap_begin (struct inode *ano_inode,loff_t offset, + loff_t length, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs || !pcs->cur) + return -EINVAL; + + return erofs_iomap_begin(&pcs->cur->vfs_inode, offset, length, flags, + iomap, srcmap); +} + +static int erofs_pcs_iomap_end (struct inode *ano_inode,loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; +Quoted 8 lines
fs/erofs/pagecache_share.c line 198
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+ return erofs_iomap_end(&pcs->cur->vfs_inode, pos, length, written, + flags, iomap); +} + +const struct iomap_ops erofs_pcs_iomap_ops = { + .iomap_begin = erofs_pcs_iomap_begin, + .iomap_end = erofs_pcs_iomap_end,Does this work on on-demand mode over fscache? As I know, this io path is only for erofs, not for erofs over fscache.
That is why I think this implementation is far from upstreaming:
- over fscache
- compression
are all needed to be implemented.
Quoted 7 lines
Quoted 4 lines
I don't think it needs to add any extra helpers just for this, you need to handle page cache sharing in the original paths to find the share inodes instead.
I remember that Jingbo have done the similar thing like this. At: https://patchew.org/linux/[email protected]/
But I don't know what happened afterward.
Yes, those two resolve the same requirement but in different ways:
Jingbo's approach use bdev/fscache meta inodes for page cache sharing, but in that way:
- vma->pgoff needs to be modified (but which is unsafe due to some corner cases), and it needs the shared data physical consective.
- it doesn't apply to encoded data like
compressed data.
The current way (maintaining inodes across different filesystems) will resolve the issues above. It's much like a very simplified overlay filesystem anyway.
Thanks,
Gao Xiang
Quoted 163 lines · Hongbo Li
Quoted 11 lines · 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. During the mkfs phase, file content is hashed and the hash value is stored in the
user.fingerprintextended attribute. Inodes of files with the sameDoes this mean the hash is calculated at the file granularity?
Quoted 148 lines
user.fingerprintare mapped to an anonymous inode, whose page cache stores the actual contents. In this way, a single copy of the anonymous inode's page cache can serve read requests from several files mapped to it.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 +------------------+-------------+---------------+ | 1.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 +------------------+-------------+---------------+ | 1.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% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
fs/erofs/Kconfig | 10 ++
fs/erofs/Makefile | 1 +
fs/erofs/internal.h | 8 +fs/erofs/pagecache_share.c| 313 +++++++++++++++++++++++++++++++++++++fs/erofs/pagecache_share.h| 20 +++5 files changed, 352 insertions(+)create mode 100644
fs/erofs/pagecache_share.c
create mode 100644fs/erofs/pagecache_share.hfs/erofs/Kconfig +0
-0@@ -158,3 +158,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRIat higher priority.
If unsure, say N.fs/erofs/Kconfig +10
-0+ +config EROFS_FS_PAGE_CACHE_SHARE + bool "EROFS page cache share support" + depends on EROFS_FS + default n + help + This permits EROFS to share page cache for files with same + fingerprints. + + If unsure, say N.fs/erofs/Makefile +0
-0@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.oerofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/Makefile +1
-0+erofs-$(CONFIG_EROFS_FS_PAGE_CACHE_SHARE) += pagecache_share.ofs/erofs/internal.h +8
-0@@ -288,6 +288,12 @@ struct erofs_inode { }; #endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + struct list_head pcs_list; + char *fprt; + int fprt_len; + unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -376,6 +382,7 @@ extern const struct super_operations erofs_sops; extern const struct address_space_operations erofs_raw_access_aops; extern const struct address_space_operations z_erofs_aops; extern const struct address_space_operations erofs_fscache_access_aops; +extern const struct address_space_operations erofs_pcs_aops; extern const struct inode_operations erofs_generic_iops; extern const struct inode_operations erofs_symlink_iops;@@ -384,6 +391,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_pcs_file_fops; extern const struct iomap_ops z_erofs_iomap_report_ops;diff --git
a/fs/erofs/pagecache_share.cb/fs/erofs/pagecache_share.cnew file mode 100644
index 000000000000..e8c8fe16cda5fs/erofs/pagecache_share.c new file +8
-0@@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xxhash.h> +#include "pagecache_share.h" +#include "internal.h" +#include "xattr.h"
Quoted 8 lines
fs/erofs/pagecache_share.c line 15
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+ +#define PCS_FPRT_IDX 1 +#define PCS_FPRT_NAME "fingerprint" +#define PCS_FPRT_MAXLEN 1024 + +struct erofs_pcs { + struct erofs_inode *cur;Why we should cur to pointer the inode? I think we only need to record the fprt of the content. When we need inode, we can check the
list_empty, and to get the any inode to use.
Hi Hongbo,
Yes, but in this case, we need to add a read-write semaphore to the struct erofs_inode for synchronization. This is because, when the inode is being released, it might still be used for reading operations such as iomap elsewhere. The implementation you mentioned will increase concurrency. I will make this change later.
Quoted 8 lines
Quoted 8 lines
fs/erofs/pagecache_share.c new file +8
-0+ struct rw_semaphore rw_sem; + struct mutex list_mutex; + struct list_head list; +}; + +static DEFINE_MUTEX (pseudo_mnt_lock); +static unsigned long pseudo_mnt_count; +static struct vfsmount *erofs_pcs_mnt;
Quoted 8 lines
fs/erofs/pagecache_share.c line 30
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+ +int erofs_pcs_init_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + if (!erofs_pcs_mnt) { + erofs_pcs_mnt = kern_mount(&erofs_anon_fs_type); + if (IS_ERR(erofs_pcs_mnt))Not unlock here.
Thank you for the reminder; it was my oversight.
Quoted 41 lines
Quoted 41 lines
fs/erofs/pagecache_share.c new file +28
-0+ return PTR_ERR(erofs_pcs_mnt); + pseudo_mnt_count = 1; + } else + pseudo_mnt_count += 1; + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +int erofs_pcs_free_mnt (void ) +{ + mutex_lock(&pseudo_mnt_lock); + pseudo_mnt_count -= 1; + if (pseudo_mnt_count < 0) + return -EINVAL; + if (pseudo_mnt_count == 0) { + kern_unmount(erofs_pcs_mnt); + erofs_pcs_mnt = NULL; + } + mutex_unlock(&pseudo_mnt_lock); + return 0; +} + +void erofs_pcs_fill_inode (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + char fprt[PCS_FPRT_MAXLEN]; + + vi->fprt_len = erofs_getxattr(inode, PCS_FPRT_IDX,
PCS_FPRT_NAME, fprt,
+PCS_FPRT_MAXLEN);
+ if (vi->fprt_len> 0 &&vi->fprt_len<=PCS_FPRT_MAXLEN) { +vi->fprt=kmalloc(vi->fprt_len, GFP_KERNEL); + if (IS_ERR(vi->fprt)) {
+vi->fprt_len= -1;
+ return;
+ }
+memcpy(vi->fprt, fprt, vi->fprt_len);
+vi->fprt_hash=xxh32(vi->fprt, vi->fprt_len, 0); + }fs/erofs/pagecache_share.c new file +2
-0+} +
Quoted 8 lines
fs/erofs/pagecache_share.c line 77
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+static int erofs_pcs_eq (struct inode *ano_inode,void *data) +{ + struct erofs_pcs *pcs = ano_inode->i_private; + struct erofs_inode *vi = (struct erofs_inode *)data; + + return pcs && pcs->cur && pcs->cur->fprt_len == vi->fprt_len && + memcmp(pcs->cur->fprt, vi->fprt, vi->fprt_len) == 0 ? 1 : 0;May be we just need compare
vi->fprt_hash, no need memcmp?
This might be necessary because there is a hash collision when using iget5_locked, and we need to compare vi->fprt.
Quoted 14 lines
Quoted 14 lines
fs/erofs/pagecache_share.c new file +14
-0+} + +static int erofs_pcs_set_aops (struct inode *ano_inode,void *data) +{ + ano_inode->i_mapping->a_ops = &erofs_pcs_aops; + ano_inode->i_size = ((struct erofs_inode *)data)->vfs_inode.i_size; + return 0; +} + +int erofs_pcs_add (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + struct inode *ano_inode; + struct erofs_pcs *pcs;
Quoted 8 lines
Quoted 7 lines
+
+ano_inode= iget5_locked(erofs_pcs_mnt->mnt_sb,vi->fprt_hash, +erofs_pcs_eq,erofs_pcs_set_aops, vi); +spin_lock(&ano_inode->i_lock);
+ if (!ano_inode->i_private) {
+ pcs =kzalloc(sizeof(struct erofs_pcs), GFP_KERNEL); + if (IS_ERR(pcs))should call
spin_unlockwhen failed
Thanks.
Quoted 8 lines
Quoted 8 lines
+ return
-ENOMEM;
+INIT_LIST_HEAD(&pcs->list);
+mutex_init(&pcs->list_mutex);
+init_rwsem(&pcs->rw_sem);
+ano_inode->i_private= pcs;
+ }
+spin_unlock(&ano_inode->i_lock);
+ pcs =ano_inode->i_private;
Quoted 6 lines
pcs =
ano_inode->i_private;spin_lock(&ano_inode->i_lock);
if (!pcs) {
xxx
}spin_unlock(&ano_inode->i_lock);
This looks better.
Quoted 87 lines
Quoted 87 lines
+
mutex_lock(&pcs->list_mutex);
+list_add_tail(&vi->pcs_list, &pcs->list); + if (!pcs->cur) {
+pcs->cur=list_first_entry(&pcs->list, + struct erofs_inode, pcs_list); + }
+mutex_unlock(&pcs->list_mutex);
+ if (ano_inode->i_state&I_NEW)
+unlock_new_inode(ano_inode);
+ return 0;fs/erofs/pagecache_share.c new file +52
-0+} + +int erofs_pcs_remove (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode), *p, *tmp; + struct inode *ano_inode; + struct erofs_pcs *pcs; + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + iput(ano_inode); + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; + mutex_lock(&pcs->list_mutex); + if (vi == pcs->cur) { + /* synchronize with reads using pcs->cur */ + down_write(&pcs->rw_sem); + list_del(&pcs->cur->pcs_list); + if (list_empty(&pcs->list)) { + pcs->cur = NULL; + up_write(&pcs->rw_sem); + goto free_ano_inode; + } + pcs->cur = list_first_entry(&pcs->list, struct erofs_inode, + pcs_list); + up_write(&pcs->rw_sem); + } else { + list_for_each_entry_safe(p, tmp, &pcs->list, pcs_list) { + if (p == vi) { + list_del(&p->pcs_list); + break; + } + } + } + iput(ano_inode); + mutex_unlock(&pcs->list_mutex); + return 0; + +free_ano_inode: + mutex_unlock(&pcs->list_mutex); + spin_lock(&ano_inode->i_lock); + if (atomic_read(&ano_inode->i_count) == 1) { + ano_inode->i_private = NULL; + kfree(pcs); + } + spin_unlock(&ano_inode->i_lock); + iput(ano_inode); + return 0; +} + +static int erofs_pcs_iomap_begin(struct inode *ano_inode, loff_toffset,
+loff_tlength, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap)fs/erofs/pagecache_share.c new file +8
-0+{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs || !pcs->cur) + return -EINVAL; + + return erofs_iomap_begin(&pcs->cur->vfs_inode, offset, length,flags,
+ iomap, srcmap);fs/erofs/pagecache_share.c new file +12
-0+} + +static int erofs_pcs_iomap_end (struct inode *ano_inode,loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct erofs_pcs *pcs; + + pcs = ano_inode->i_private; + if (!pcs) + return -EINVAL; +
Quoted 8 lines
fs/erofs/pagecache_share.c line 198
Quoted 7 lines
+ return
erofs_iomap_end(&pcs->cur->vfs_inode, pos, length, written, + flags, iomap);fs/erofs/pagecache_share.c new file +5
-0+} + +const struct iomap_ops erofs_pcs_iomap_ops = { + .iomap_begin = erofs_pcs_iomap_begin, + .iomap_end = erofs_pcs_iomap_end,Does this work on on-demand mode over fscache? As I know, this io path is only for erofs, not for erofs over fscache.
Currently, this is not supported, but I will add this to my work later.
Quoted 139 lines
Quoted 139 lines
fs/erofs/pagecache_share.c new file +39
-0+}; + +static int erofs_pcs_read_folio (struct file *file,struct folio *folio) +{ + int err; + struct erofs_pcs *pcs; + + pcs = file->f_inode->i_private; + if (!pcs) + return -EINVAL; + down_read(&pcs->rw_sem); + err = iomap_read_folio(folio, &erofs_pcs_iomap_ops); + up_read(&pcs->rw_sem); + return err; +} + +static void erofs_pcs_readahead (struct readahead_control *rac) +{ + struct erofs_pcs *pcs; + + pcs = rac->file->f_inode->i_private; + if (!pcs) + return; + down_read(&pcs->rw_sem); + iomap_readahead(rac, &erofs_pcs_iomap_ops); + up_read(&pcs->rw_sem); +} + +const struct address_space_operations erofs_pcs_aops = { + .read_folio = erofs_pcs_read_folio, + .readahead = erofs_pcs_readahead, +}; + +static struct file *erofs_pcs_alloc_file (struct file *file, + struct inode *ano_inode) +{ + struct file *ano_file; + + ano_file = alloc_file_pseudo(ano_inode, erofs_pcs_mnt,"[
erofs_pcs_f]",
+O_RDONLY,&erofs_file_fops); +file_ra_state_init(&ano_file->f_ra, file->f_mapping); + returnano_file;fs/erofs/pagecache_share.c new file +18
-0+} + +static int erofs_pcs_file_open (struct inode *inode,struct file *file) +{ + struct file *ano_file; + struct inode *ano_inode; + struct erofs_inode *vi = EROFS_I(inode); + + ano_inode = iget5_locked(erofs_pcs_mnt->mnt_sb, vi->fprt_hash, + erofs_pcs_eq, erofs_pcs_set_aops, vi); + if (!ano_inode) + return -EINVAL; + ano_file = erofs_pcs_alloc_file(file, ano_inode); + file->private_data = (void *)ano_file; + return 0; +} + +static int erofs_pcs_file_release(struct inode *inode, struct file*file)
fs/erofs/pagecache_share.c new file +38
-0+{ + if (!file->private_data) + return -EINVAL; + fput((struct file *)file->private_data); + file->private_data = NULL; + return 0; +} + +static ssize_t erofs_pcs_file_read_iter (struct kiocb *iocb, + struct iov_iter *to) +{ + struct file *file, *ano_file; + struct kiocb ano_iocb; + ssize_t res; + + memcpy(&ano_iocb, iocb, sizeof(struct kiocb)); + file = iocb->ki_filp; + ano_file = file->private_data; + if (!ano_file) + return -EINVAL; + ano_iocb.ki_filp = ano_file; + res = filemap_read(&ano_iocb, to, 0); + memcpy(iocb, &ano_iocb, sizeof(struct kiocb)); + iocb->ki_filp = file; + file_accessed(file); + return res; +} + +static vm_fault_t erofs_pcs_fault (struct vm_fault *vmf) +{ + return filemap_fault(vmf); +} + +static const struct vm_operations_struct erofs_pcs_file_vm_ops = { + .fault = erofs_pcs_fault, +}; + +static int erofs_pcs_mmap(struct file *file, struct vm_area_struct*vma)
fs/erofs/pagecache_share.c new file +17
-0+{ + struct file *ano_file = file->private_data; + + vma_set_file(vma, ano_file); + vma->vm_ops = &erofs_pcs_file_vm_ops; + return 0; +} + +const struct file_operations erofs_pcs_file_fops = { + .open = erofs_pcs_file_open, + .llseek = generic_file_llseek, + .read_iter = erofs_pcs_file_read_iter, + .mmap = erofs_pcs_mmap, + .release = erofs_pcs_file_release, + .get_unmapped_area = thp_get_unmapped_area, + .splice_read = filemap_splice_read, +};fs/erofs/pagecache_share.h new file +15
-0@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#ifndef __EROFS_PAGECACHE_SHARE_H +#define __EROFS_PAGECACHE_SHARE_H + +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/rwlock.h> +#include <linux/mutex.h> +#include "internal.h" + +int erofs_pcs_init_mnt (void ); +int erofs_pcs_free_mnt (void );
Quoted 8 lines
fs/erofs/pagecache_share.h line 20
Quoted 5 lines
fs/erofs/pagecache_share.h new file +5
-0+void erofs_pcs_fill_inode (struct inode *inode); +int erofs_pcs_add (struct inode *inode); +int erofs_pcs_remove (struct inode *inode); + +#endif Thanks,
Hongbo
Thanks,
Hongzhen Luo
RFC PATCH 4/4 erofs: apply the page cache share feature
In reply to Hongzhen LuoThis modifies relevant functions to apply the page cache share feature.
--- fs/erofs/inode.c | 21 +++++++++++++++++++++ fs/erofs/super.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
fs/erofs/inode.c +21 -0
@@ -5,6 +5,7 @@* Copyright (C) 2021, Alibaba Cloud */ #include "xattr.h" +#include "pagecache_share.h" #include <trace/events/erofs.h>
@@ -229,10 +230,22 @@ static int erofs_fill_inode(struct inode *inode)switch (inode->i_mode &S_IFMT ) {case S_IFREG : inode->i_op = &erofs_generic_iops; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +erofs_pcs_fill_inode (inode); +#endif if (erofs_inode_is_data_compressed (vi->datalayout)) inode->i_fop = &generic_ro_fops; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + else { +if (vi->fprt_len >0 ) + inode->i_fop = &erofs_pcs_file_fops; +else + inode->i_fop = &erofs_file_fops; + } +#else else inode->i_fop = &erofs_file_fops; +#endif break ;case S_IFDIR : inode->i_op = &erofs_dir_iops;
@@ -325,6 +338,14 @@ struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)return ERR_PTR (err); }unlock_new_inode (inode); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +if ((inode->i_mode &S_IFMT ) ==S_IFREG && +EROFS_I (inode)->fprt_len >0 ) { + err =erofs_pcs_add (inode); +if (err) +return ERR_PTR (err); + } +#endif }return inode; }
fs/erofs/super.c +32 -0
@@ -12,6 +12,7 @@#include <linux/exportfs.h> #include <linux/pseudo_fs.h> #include "xattr.h" +#include "pagecache_share.h" #define CREATE_TRACE_POINTS #include <trace/events/erofs.h>
@@ -96,6 +97,9 @@ static struct inode *erofs_alloc_inode(struct super_block *sb)/* zero out everything except vfs_inode */ memset (vi,0 , offsetof(struct erofs_inode , vfs_inode)); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +INIT_LIST_HEAD (&vi->pcs_list); +#endif return &vi->vfs_inode; }
@@ -109,6 +113,23 @@ static void erofs_free_inode(struct inode *inode)kmem_cache_free (erofs_inode_cachep, vi); } +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +static void erofs_destroy_inode (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode); + +if ((inode->i_mode &S_IFMT ) ==S_IFREG && +EROFS_I (inode)->fprt_len >0 ) { +if (erofs_pcs_remove (inode)) +erofs_err (inode->i_sb,"pcs: fail to remove inode." ); +if (vi->fprt) { +kfree (vi->fprt); + vi->fprt =NULL ; + } + } +} +#endif +static bool check_layout_compatibility (struct super_block *sb,struct erofs_super_block *dsb) {
@@ -685,6 +706,11 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)if (err)return err; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + err =erofs_pcs_init_mnt (); +if (err) +return err; +#endif erofs_info (sb,"mounted with root inode @ nid %llu." , sbi->root_nid);return 0 ; }
@@ -795,6 +821,9 @@ static void erofs_kill_sb(struct super_block *sb)else kill_block_super (sb); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + (void )erofs_pcs_free_mnt (); +#endif erofs_free_dev_context (sbi->devs);fs_put_dax (sbi->dax_dev,NULL );erofs_fscache_unregister_fs (sb);
@@ -951,6 +980,9 @@ const struct super_operations erofs_sops = { .put_super = erofs_put_super, .alloc_inode = erofs_alloc_inode, .free_inode = erofs_free_inode, +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + .destroy_inode = erofs_destroy_inode, +#endif .statfs = erofs_statfs, .show_options = erofs_show_options, };
Signature · 1 line
2.43.5