erofs: introduce page cache share feature
v3 cover erofs: introduce page cache share feature
v3:
Changes since v2:
- The implementation of page cache share has been redesigned to overcome read dependencies during device unmounting, where an inode cannot be freed when it is currently being used to perform a read elsewhere.
- Added support for compressed files.
- The relevant experiments were repeated, in accordance with the latest implementations.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
[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]
================
This introduces the page cache share feature in erofs. During the mkfs phase, the file content is hashed and the hash value is stored in the trusted.erofs.fingerprint extended attribute. Inodes of files with the same trusted.erofs.fingerprint are mapped to the same anonymous inode (indicated by the ano_inode field). When a read request occurs, the anonymous inode serves as a "container" whose page cache is shared. The actual operations involving the iomap are carried out by the original inode which is mapped to the anonymous inode.
[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 | 35 | - | | redis +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 28 | 20% | +-------------------+------------------+-------------+---------------+ | | No | 149 | - | | postgres +------------------+-------------+---------------+ | 16.1 & 16.2 | Yes | 95 | 37% | +-------------------+------------------+-------------+---------------+ | | No | 1028 | - | | tensorflow +------------------+-------------+---------------+ | 1.11.0 & 2.11.1 | Yes | 930 | 10% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 132 | 15% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 20% | +-------------------+------------------+-------------+---------------+ | tomcat | No | 186 | - | | 10.1.25 & 10.1.26 +------------------+-------------+---------------+ | | Yes | 98 | 48% | +-------------------+------------------+-------------+---------------+
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 10% to 48%.
[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 (3):
erofs: move struct erofs_anon_fs_type to super.c
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 | 34 +++++++- fs/erofs/fscache.c | 13 --- fs/erofs/inode.c | 12 +++ fs/erofs/internal.h | 6 ++ fs/erofs/pagecache_share.c | 171 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 20 +++++ fs/erofs/super.c | 50 +++++++++++ fs/erofs/zdata.c | 32 +++++++ 10 files changed, 335 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 v3 1/3 erofs: move `struct erofs_anon_fs_type` to super.c
Move the struct erofs_anon_fs_type to the super.c and expose it in preparation for the upcoming page cache share feature.
---
v3:
Changes since the v1:
- Utilize the erofs_anon_sops as the interface for the VFS to operate on anonymous inodes. The subsequent patch will implement .free_inode to facilitate more granular control over anonymous inodes.
v2: The patch set v2 does not move the struct erofs_anon_fs_type to super.c.
v1: https://lore.kernel.org/all/[email protected]/
--- fs/erofs/fscache.c | 13 ------------- fs/erofs/internal.h | 2 ++ fs/erofs/super.c | 21 +++++++++++++++++++++ 3 files changed, 23 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 +21 -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
@@ -848,6 +849,26 @@ static struct file_system_type erofs_fs_type = { };MODULE_ALIAS_FS ("erofs" ); +static const struct super_operations erofs_anon_sops = { + .statfs = simple_statfs, +}; + +static int erofs_anon_init_fs_context (struct fs_context *fc) +{ +struct pseudo_fs_context *ctx =init_pseudo (fc,EROFS_SUPER_MAGIC ); + +if (ctx) + ctx->ops = &erofs_anon_sops; +return ctx ?0 : -ENOMEM ; +} + +struct file_system_type erofs_anon_fs_type = { + .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 v3 2/3 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, the file content is hashed and the hash value is stored in the trusted.erofs.fingerprint extended attribute. Inodes of files with the same trusted.erofs.fingerprint are mapped to the same anonymous inode (indicated by the ano_inode field). When a read request occurs, the anonymous inode serves as a "container" whose page cache is shared. The actual operations involving the iomap are carried out by the original inode which is mapped to the anonymous inode.
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 | 35 | - | | redis +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 28 | 20% | +-------------------+------------------+-------------+---------------+ | | No | 149 | - | | postgres +------------------+-------------+---------------+ | 16.1 & 16.2 | Yes | 95 | 37% | +-------------------+------------------+-------------+---------------+ | | No | 1028 | - | | tensorflow +------------------+-------------+---------------+ | 1.11.0 & 2.11.1 | Yes | 930 | 10% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 132 | 15% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 20% | +-------------------+------------------+-------------+---------------+ | tomcat | No | 186 | - | | 10.1.25 & 10.1.26 +------------------+-------------+---------------+ | | Yes | 98 | 48% | +-------------------+------------------+-------------+---------------+
---
v3:
The previous implementation maintained a list for inodes with identical content, and utilized one of these inodes from the list to handle read request operations. However, this could lead to a situation where a device cannot be unmounted because its inode is being used for read-related operations.
This implementation has been redesigned to avoid the aforementioned unmounting issues. Additionally, this implementation adds support for compressed files.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- fs/erofs/Kconfig | 10 +++ fs/erofs/Makefile | 1 + fs/erofs/internal.h | 4 + fs/erofs/pagecache_share.c | 171 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 20 +++++ 5 files changed, 206 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 +4 -0
@@ -288,6 +288,9 @@ struct erofs_inode { };#endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct inode *ano_inode; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };
@@ -384,6 +387,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 +171 -0
171 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 ); +void erofs_pcs_free_mnt (void ); +void erofs_pcs_fill_inode (struct inode *inode); + +extern const struct vm_operations_struct generic_file_vm_ops; + +#endif
Signature · 1 line
2.43.5
RFC PATCH v3 3/3 erofs: apply the page cache share feature
In reply to Hongzhen LuoThis modifies relevant functions to apply the page cache share feature.
---
v3: Reimplement and add support for compressed files.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- fs/erofs/data.c | 34 +++++++++++++++++++++++++++++++++- fs/erofs/inode.c | 12 ++++++++++++ fs/erofs/super.c | 29 +++++++++++++++++++++++++++++ fs/erofs/zdata.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-)
fs/erofs/data.c +33 -1
@@ -347,12 +347,44 @@ int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,*/ static int erofs_read_folio (struct file *file,struct folio *folio) { +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct erofs_inode *vi =NULL ; +int ret; + +if (file && file->private_data) { + vi = file->private_data; +if (vi->ano_inode ==file_inode (file)) + folio->mapping->host = &vi->vfs_inode; +else + vi =NULL ; + } + ret =iomap_read_folio (folio, &erofs_iomap_ops); +if (vi) + folio->mapping->host =file_inode (file); +return ret; +#else return iomap_read_folio (folio, &erofs_iomap_ops); +#endif } -static void erofs_readahead (struct readahead_control *rac) { +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct erofs_inode *vi =NULL ; +struct file *file = rac->file; + +if (file && file->private_data) { + vi = file->private_data; +if (vi->ano_inode ==file_inode (file)) + rac->mapping->host = &vi->vfs_inode; +else + vi =NULL ; + } +iomap_readahead (rac, &erofs_iomap_ops); +if (vi) + rac->mapping->host =file_inode (file); +#else return iomap_readahead (rac, &erofs_iomap_ops); +#endif }static sector_t erofs_bmap (struct address_space *mapping,sector_t block)
fs/erofs/inode.c +12 -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,21 @@ 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); +if (vi->ano_inode) + inode->i_fop = &erofs_pcs_file_fops; +else if (erofs_inode_is_data_compressed (vi->datalayout)) + inode->i_fop = &generic_ro_fops; +else + inode->i_fop = &erofs_file_fops; +#else if (erofs_inode_is_data_compressed (vi->datalayout)) inode->i_fop = &generic_ro_fops;else inode->i_fop = &erofs_file_fops; +#endif +break ;case S_IFDIR : inode->i_op = &erofs_dir_iops;
fs/erofs/super.c +29 -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>
@@ -103,6 +104,12 @@ static void erofs_free_inode(struct inode *inode) {struct erofs_inode *vi =EROFS_I (inode); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +if (S_ISREG (inode->i_mode) && vi->ano_inode) { +iput (vi->ano_inode); + vi->ano_inode =NULL ; + } +#endif if (inode->i_op == &erofs_fast_symlink_iops)kfree (inode->i_link);kfree (vi->xattr_shared_xattrs);
@@ -701,6 +708,12 @@ 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 ; }
@@ -811,6 +824,9 @@ static void erofs_kill_sb(struct super_block *sb)else kill_block_super (sb); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +erofs_pcs_free_mnt (); +#endif erofs_free_dev_context (sbi->devs);fs_put_dax (sbi->dax_dev,NULL );erofs_fscache_unregister_fs (sb);
@@ -849,8 +865,21 @@ static struct file_system_type erofs_fs_type = { };MODULE_ALIAS_FS ("erofs" ); +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +static void erofs_free_anon_inode (struct inode *inode) +{ +if (inode->i_private) { +kfree (inode->i_private); + inode->i_private =NULL ; + } +} +#endif +static const struct super_operations erofs_anon_sops = { .statfs = simple_statfs, +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + .free_inode = erofs_free_anon_inode, +#endif };static int erofs_anon_init_fs_context (struct fs_context *fc)
fs/erofs/zdata.c +32 -0
@@ -1802,6 +1802,17 @@ static void z_erofs_pcluster_readmore(struct z_erofs_decompress_frontend *f,static int z_erofs_read_folio (struct file *file,struct folio *folio) { +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct erofs_inode *vi =NULL ; + +if (file && file->private_data) { + vi = file->private_data; +if (vi->ano_inode ==file_inode (file)) + folio->mapping->host = &vi->vfs_inode; +else + vi =NULL ; + } +#endif struct inode *const inode = folio->mapping->host;struct erofs_sb_info *const sbi =EROFS_I_SB (inode);struct z_erofs_decompress_frontend f =DECOMPRESS_FRONTEND_INIT (inode);
@@ -1824,11 +1835,27 @@ static int z_erofs_read_folio(struct file *file, struct folio *folio)erofs_put_metabuf (&f.map.buf);erofs_release_pages (&f.pagepool); + +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +if (vi) + folio->mapping->host =file_inode (file); +#endif return err; }static void z_erofs_readahead (struct readahead_control *rac) { +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct erofs_inode *vi =NULL ; + +if (rac->file && rac->file->private_data) { + vi = rac->file->private_data; +if (vi->ano_inode ==file_inode (rac->file)) + rac->mapping->host = &vi->vfs_inode; +else + vi =NULL ; + } +#endif struct inode *const inode = rac->mapping->host;struct erofs_sb_info *const sbi =EROFS_I_SB (inode);struct z_erofs_decompress_frontend f =DECOMPRESS_FRONTEND_INIT (inode);
@@ -1863,6 +1890,11 @@ static void z_erofs_readahead(struct readahead_control *rac)z_erofs_runqueue (&f,z_erofs_is_sync_decompress (sbi, nr_folios), true);erofs_put_metabuf (&f.map.buf);erofs_release_pages (&f.pagepool); + +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +if (vi) + rac->mapping->host =file_inode (rac->file); +#endif }const struct address_space_operations z_erofs_aops = {
Signature · 1 line
2.43.5