erofs: introduce page cache share feature
v2 cover erofs: introduce page cache share feature
v2:
- Update the Implementation section.
- Update the Effect section.
- Add the Restriction section.
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]
================
During the mkfs phase, 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 a list, and this mapping is accomplished through xarray. When a read request occurs, one of these inodes that in the list (the first inode in this implementation) is used to store the actual content. In this way, a single copy of the selected inode's page cache can serve read requests from several files mapped to it.
[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 | 941 | 9% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 139 | 11% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 23% | +-------------------+------------------+-------------+---------------+ | 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 9% 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
[Restriction]
================
Currently, this feature only supports reading uncompressed files. It do not support:
- over fscache
- compression
The upcoming updates will expand the range of support to these.
Hongzhen Luo (2):
erofs: introduce page cache share feature
erofs: apply the page cache share feature
fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/inode.c | 23 +++++ fs/erofs/internal.h | 8 ++ fs/erofs/pagecache_share.c | 189 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 15 +++ fs/erofs/super.c | 23 +++++ 7 files changed, 269 insertions(+) create mode 100644 fs/erofs/pagecache_share.c create mode 100644 fs/erofs/pagecache_share.h
Signature · 1 line
2.43.5
RFC PATCH v2 1/2 erofs: introduce page cache share feature
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 trusted.erofs.fingerprint extended attribute. Inodes of files with the same trusted.erofs.fingerprint are mapped to a list. One of these inodes that in the list (the first inode in this implementation) is used to store the actual content. In this way, a single copy of the selected 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 | 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 | 941 | 9% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 139 | 11% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 23% | +-------------------+------------------+-------------+---------------+ | tomcat | No | 186 | - | | 10.1.25 & 10.1.26 +------------------+-------------+---------------+ | | Yes | 98 | 48% | +-------------------+------------------+-------------+---------------+
---
v2: Refactored the implementation.
v1: https://lore.kernel.org/all/[email protected]/
--- fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/internal.h | 8 ++ fs/erofs/pagecache_share.c | 189 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 15 +++ 5 files changed, 223 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,13 @@ struct erofs_inode { };#endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct list_head pcs_list; +struct rw_semaphore pcs_rwsem; +char *fprt; +int fprt_len; +unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };
@@ -383,6 +390,7 @@ extern const struct inode_operations erofs_fast_symlink_iops;extern const struct inode_operations erofs_dir_iops;extern const struct file_operations erofs_file_fops; +extern const struct file_operations erofs_pcs_file_fops;extern const struct file_operations erofs_dir_fops;extern const struct iomap_ops z_erofs_iomap_report_ops;
fs/erofs/pagecache_share.c new file +189 -0
189 lines not on this page · raw message
fs/erofs/pagecache_share.h new file +15 -0
@@ -0,0 +1,15 @@ +/* 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/mutex.h> +#include "internal.h" + +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 161 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
trusted.erofs.fingerprintextended attribute. Inodes of files with the sametrusted.erofs.fingerprintare mapped to a list. One of these inodes that in the list (the first inode in this implementation) is used to store the actual content. In this way, a single copy of the selected 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 | 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 | 941 | 9% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 139 | 11% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 23% | +-------------------+------------------+-------------+---------------+ | tomcat | No | 186 | - | | 10.1.25 & 10.1.26 +------------------+-------------+---------------+ | | Yes | 98 | 48% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
v2: Refactored the implementation.
v1: https://lore.kernel.org/all/[email protected]/--- fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/internal.h | 8 ++ fs/erofs/pagecache_share.c | 189 +++++++++++++++++++++++++++++++++++++ fs/erofs/pagecache_share.h | 15 +++ 5 files changed, 223 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,13 @@ struct erofs_inode { };#endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE +struct list_head pcs_list; +struct rw_semaphore pcs_rwsem; +char *fprt; +int fprt_len; +unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -383,6 +390,7 @@ extern const struct inode_operations erofs_fast_symlink_iops;extern const struct inode_operations erofs_dir_iops;extern const struct file_operations erofs_file_fops; +extern const struct file_operations erofs_pcs_file_fops;extern const struct file_operations erofs_dir_fops;extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +8
-0@@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xarray.h> +#include <linux/mutex.h> +#include <linux/xxhash.h> +#include "internal.h"
fs/erofs/pagecache_share.c line 18
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+#include "xattr.h" +#include "pagecache_share.h" + +struct erofs_pcs_lhead { +struct mutex lmutex; +struct list_head list; +}; + +#define PCS_FPRT_IDX 4 +#define PCS_FPRT_NAME "erofs.fingerprint"
This macro should be "trusted.erofs.fingerprint"
Quoted 8 lines
fs/erofs/pagecache_share.c new file +8
-0+#define PCS_FPRT_MAXLEN 1024 + +DEFINE_XARRAY (pcs_xarray); + +void erofs_pcs_fill_inode (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode); +char fprt[PCS_FPRT_MAXLEN ];
fs/erofs/pagecache_share.c line 36
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+ + 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);
This could use strscpy or other string copy function to avoid random garbage beyond a string's null terminator.
Quoted 11 lines
fs/erofs/pagecache_share.c new file +11
-0+ vi->fprt_hash =xxh32 (vi->fprt, vi->fprt_len,0 ); + } +} + +int erofs_pcs_add (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode); +struct erofs_pcs_lhead *lst; + +xa_lock (&pcs_xarray); + lst =xa_load (&pcs_xarray, vi->fprt_hash);
fs/erofs/pagecache_share.c line 57
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+if (!lst) { + lst =kmalloc (sizeof (struct erofs_pcs_lhead ),GFP_KERNEL ); +if (!lst) { +xa_unlock (&pcs_xarray); +return -ENOMEM ; + } +mutex_init (&lst->lmutex); +INIT_LIST_HEAD (&lst->list); +/* we have already held the xa_lock here */ +__xa_store (&pcs_xarray, vi->fprt_hash, lst,GFP_KERNEL );
Here, fprt_hash is calculated with xxh32, if hash conflict happend, the fprt_hash will be same, and the value in xarray also will be replaced. How can we avoid this case?
fs/erofs/pagecache_share.c line 70
Quoted 13 lines
fs/erofs/pagecache_share.c new file +13
-0+ } +xa_unlock (&pcs_xarray); + +mutex_lock (&lst->lmutex); +list_add_tail (&vi->pcs_list, &lst->list); +mutex_unlock (&lst->lmutex); +return 0 ; +} + +int erofs_pcs_remove (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode); +struct erofs_pcs_lhead *lst =xa_load (&pcs_xarray, vi->fprt_hash);
This operation should hold the xarray lock.
fs/erofs/pagecache_share.c line 73
Quoted 3 lines
fs/erofs/pagecache_share.c new file +3
-0+ +if (!lst ||list_empty (&lst->list)) +return -EINVAL ;
This part should not happen, right? Unless there is a problem with the code logic, and the remove operation seems unnecessary to return a value.
Quoted 9 lines
fs/erofs/pagecache_share.c new file +9
-0+ +mutex_lock (&lst->lmutex); +down_write (&vi->pcs_rwsem); +list_del (&vi->pcs_list); +up_write (&vi->pcs_rwsem); +mutex_unlock (&lst->lmutex); + +xa_lock (&pcs_xarray); +if (list_empty (&lst->list)) {
fs/erofs/pagecache_share.c line 92
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-0+__xa_erase (&pcs_xarray, vi->fprt_hash); +kfree (lst); + } +xa_unlock (&pcs_xarray); +return 0 ; +} + +static struct inode *erofs_pcs_get4read (struct inode *inode) +{ +struct erofs_inode *vi =EROFS_I (inode), *pcs_inode =NULL , *p, *tmp;
This should be locked xarray here? Otherwise, how can we prevent another thread deleting from the pcs_xarray?
fs/erofs/pagecache_share.c line 101
Quoted 9 lines
fs/erofs/pagecache_share.c new file +9
-0+struct erofs_pcs_lhead *lst =xa_load (&pcs_xarray, vi->fprt_hash); + +if (!lst ||list_empty (&lst->list)) +return ERR_PTR (-EINVAL ); + +mutex_lock (&lst->lmutex); +list_for_each_entry_safe (p, tmp, &lst->list, pcs_list) { +if (vi->fprt_len == p->fprt_len && + !memcmp (vi->fprt, p->fprt, p->fprt_len)) {
use string compare helper?
Quoted 55 lines
fs/erofs/pagecache_share.c new file +55
-0+ pcs_inode = p; +break ; + } + } +if (pcs_inode) +down_read (&pcs_inode->pcs_rwsem); +mutex_unlock (&lst->lmutex); + +return pcs_inode ? &pcs_inode->vfs_inode :ERR_PTR (-EINVAL ); +} + +static int erofs_pcs_file_open (struct inode *inode,struct file *file) +{ +struct inode *pcs_inode; +struct file *ano_file; + + pcs_inode =erofs_pcs_get4read (inode); +if (IS_ERR (pcs_inode)) +return PTR_ERR (pcs_inode); + + ano_file =alloc_file_pseudo (pcs_inode, file->f_path.mnt,"[erofs_pcs_f]" , +O_RDONLY , &erofs_file_fops); +file_ra_state_init (&ano_file->f_ra, file->f_mapping); +ihold (pcs_inode); + file->private_data = (void *)ano_file; +return 0 ; +} + +static int erofs_pcs_file_release (struct inode *inode,struct file *file) +{ +struct inode *pcs_inode; + +if (!file->private_data) +return -EINVAL ; + pcs_inode = ((struct file *)file->private_data)->f_inode; +up_read (&EROFS_I (pcs_inode)->pcs_rwsem); +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 );
fs/erofs/pagecache_share.c line 166
Quoted 10 lines
fs/erofs/pagecache_share.c new file +10
-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); +}
This helper is unnecessary. We could just assign the default fault helper.
fs/erofs/pagecache_share.c line 169
Quoted 3 lines
fs/erofs/pagecache_share.c new file +3
-0+ +static const struct vm_operations_struct erofs_pcs_file_vm_ops = { + .fault = erofs_pcs_fault,
If .map_pages not assigned, the fault around should not arouse.
Quoted 41 lines
fs/erofs/pagecache_share.c new file +20
-0+}; + +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 +15
-0@@ -0,0 +1,15 @@ +/* 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/mutex.h> +#include "internal.h" + +void erofs_pcs_fill_inode (struct inode *inode); +int erofs_pcs_add (struct inode *inode); +int erofs_pcs_remove (struct inode *inode); + +#endif
Quoted 165 lines · Hongbo Li
Quoted 162 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
trusted.erofs.fingerprintextended attribute. Inodes of files with the sametrusted.erofs.fingerprintare mapped to a list. One of these inodes that in the list (the first inode in this implementation) is used to store the actual content. In this way, a single copy of the selected 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 | 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 | 941 | 9% | +-------------------+------------------+-------------+---------------+ | | No | 155 | - | | mysql +------------------+-------------+---------------+ | 8.0.11 & 8.0.12 | Yes | 139 | 11% | +-------------------+------------------+-------------+---------------+ | | No | 25 | - | | nginx +------------------+-------------+---------------+ | 7.2.4 & 7.2.5 | Yes | 20 | 23% | +-------------------+------------------+-------------+---------------+ | tomcat | No | 186 | - | | 10.1.25 & 10.1.26 +------------------+-------------+---------------+ | | Yes | 98 | 48% | +-------------------+------------------+-------------+---------------+Signed-off-by Hongzhen Luo <[email protected]>---
v2: Refactored the implementation.
v1: https://lore.kernel.org/all/[email protected]/
---
fs/erofs/Kconfig | 10 ++
fs/erofs/Makefile | 1 +
fs/erofs/internal.h | 8 ++
fs/erofs/pagecache_share.c| 189 +++++++++++++++++++++++++++++++++++++fs/erofs/pagecache_share.h| 15 +++5 files changed, 223 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,13 @@ struct erofs_inode { }; #endif /* CONFIG_EROFS_FS_ZIP */ }; +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + struct list_head pcs_list; + struct rw_semaphore pcs_rwsem; + char *fprt; + int fprt_len; + unsigned long fprt_hash; +#endif /* the corresponding vfs inode */ struct inode vfs_inode; };@@ -383,6 +390,7 @@ extern const struct inode_operations erofs_fast_symlink_iops; extern const struct inode_operations erofs_dir_iops; extern const struct file_operations erofs_file_fops; +extern const struct file_operations erofs_pcs_file_fops; extern const struct file_operations erofs_dir_fops; extern const struct iomap_ops z_erofs_iomap_report_ops;fs/erofs/pagecache_share.c new file +11
-0@@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Alibaba Cloud +*/ +#include <linux/xarray.h> +#include <linux/mutex.h> +#include <linux/xxhash.h> +#include "internal.h" +#include "xattr.h" +#include "pagecache_share.h" +
Quoted 8 lines
fs/erofs/pagecache_share.c line 18
Quoted 7 lines
fs/erofs/pagecache_share.c new file +7
-0+struct erofs_pcs_lhead { + struct mutex lmutex; + struct list_head list; +}; + +#define PCS_FPRT_IDX 4 +#define PCS_FPRT_NAME "erofs.fingerprint"This macro should be "trusted.erofs.fingerprint"
Since we obtain the extended attribute value through erofs_getxattr() instead of vfs_getxattr(),
we only need to specify the index (PCS_FPRT_IDX) of the prefix and the name “erofs.fingerprint”.
Quoted 12 lines
Quoted 12 lines
fs/erofs/pagecache_share.c new file +10
-0+#define PCS_FPRT_MAXLEN 1024 + +DEFINE_XARRAY (pcs_xarray); + +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);
Quoted 8 lines
Quoted 7 lines
+ 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);This could use strscpy or other string copy function to avoid random garbage beyond a string's null terminator.
During the mkfs phase, file content is hashed (for example, by SHA256 ) and the hash value is stored in the
trusted.erofs.fingerprint extended attribute. The value of trusted.erofs.fingerprint may contain one or more bytes
with the value \0. When using strscpy to copy it, the content may be truncated, thus preventing the complete setting
of the trusted.erofs.fingerprint attribute value. This is not as expected.
Quoted 14 lines
Quoted 14 lines
+
vi->fprt_hash=xxh32(vi->fprt, vi->fprt_len, 0); + }fs/erofs/pagecache_share.c new file +12
-0+} + +int erofs_pcs_add (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + struct erofs_pcs_lhead *lst; + + xa_lock(&pcs_xarray); + lst = xa_load(&pcs_xarray, vi->fprt_hash); + if (!lst) { + lst = kmalloc(sizeof(struct erofs_pcs_lhead), GFP_KERNEL); + if (!lst) {
Quoted 8 lines
Quoted 7 lines
+
xa_unlock(&pcs_xarray);
+ return-ENOMEM;
+ }
+mutex_init(&lst->lmutex);
+INIT_LIST_HEAD(&lst->list);
+ /* we have already held thexa_lockhere */
+__xa_store(&pcs_xarray, vi->fprt_hash, lst, GFP_KERNEL);Here,
fprt_hashis calculated with xxh32, if hash conflict happend, thefprt_hashwill be same, and the value in xarray also will be replaced. How can we avoid this case?
Each element in the xarray is a list, with hash conflicting inodes being added to this list. When we need to fetch an inode for reading,
we select an inode with the same ->fprt from this list for reading. See erofs_pcs_get4read(). This is similar to iget5_locked().
Quoted 6 lines
Quoted 6 lines
+ }
+xa_unlock(&pcs_xarray);
+
+mutex_lock(&lst->lmutex);
+list_add_tail(&vi->pcs_list, &lst->list);
+mutex_unlock(&lst->lmutex);
Quoted 8 lines
fs/erofs/pagecache_share.c line 70
Quoted 7 lines
+ return 0;
fs/erofs/pagecache_share.c new file +6
-0+} + +int erofs_pcs_remove (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + struct erofs_pcs_lhead *lst = xa_load(&pcs_xarray, vi->fprt_hash);This operation should hold the xarray lock.
This might not be necessary. Because only the code in erofs_pcs_add():
if (!lst) {
lst = kmalloc(sizeof(struct erofs_pcs_lhead), GFP_KERNEL); if (!lst) {
xa_unlock(&pcs_xarray);
return -ENOMEM;
}
mutex_init(&lst->lmutex); INIT_LIST_HEAD(&lst->list);
/* we have already held the xa_lock here */ __xa_store(&pcs_xarray, vi->fprt_hash, lst, GFP_KERNEL); }
confilicts with the code in erofs_pcs_remove():
if (list_empty(&lst->list)) {
__xa_erase(&pcs_xarray, vi->fprt_hash);
kfree(lst);
}
And at this point, the list in xarray is not empty unless there is a problem with the code logic
(that is, erofs_pcs_add() and erofs_pcs_remove() not apear in pairs). So we can get the target
list just by xa_load(&pcs_xarray, vi->fprt_hash).
Quoted 4 lines
Quoted 3 lines
+
+ if (!lst ||list_empty(&lst->list))
+ return-EINVAL;This part should not happen, right? Unless there is a problem with the code logic, and the remove operation seems unnecessary to return a value.
Thank you for the reminder. Yes, this part should not happen, because the list is not empty at this moment. I will clean it up in the next version.
Quoted 13 lines
Quoted 13 lines
fs/erofs/pagecache_share.c new file +13
-0+ + mutex_lock(&lst->lmutex); + down_write(&vi->pcs_rwsem); + list_del(&vi->pcs_list); + up_write(&vi->pcs_rwsem); + mutex_unlock(&lst->lmutex); + + xa_lock(&pcs_xarray); + if (list_empty(&lst->list)) { + __xa_erase(&pcs_xarray, vi->fprt_hash); + kfree(lst); + } + xa_unlock(&pcs_xarray);
Quoted 8 lines
Quoted 7 lines
+ return 0;
fs/erofs/pagecache_share.c new file +5
-0+} + +static struct inode *erofs_pcs_get4read (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode), *pcs_inode = NULL, *p,*tmp;
This should be locked xarray here? Otherwise, how can we prevent another thread deleting from the
pcs_xarray?
The element (a list) in pcs_xarray will be deleted when the list is empty. And the code in erofs_pcs_remove() will delete it:
if (list_empty(&lst->list)) {
__xa_erase(&pcs_xarray, vi->fprt_hash);
kfree(lst);
}
At this point, the list will not be empty. So we can use xa_load(&pcs_xarray, vi->fprt_hash). Note that another thread will not
delete the pcs_xarray element because the list is not empty if the erofs_pcs_add() and erofs_pcs_remove() appear in pairs.
Quoted 10 lines
fs/erofs/pagecache_share.c line 101
Quoted 9 lines
+ struct
erofs_pcs_lhead*lst =xa_load(&pcs_xarray, vi->fprt_hash);fs/erofs/pagecache_share.c new file +8
-0+ + if (!lst || list_empty(&lst->list)) + return ERR_PTR(-EINVAL); + + mutex_lock(&lst->lmutex); + list_for_each_entry_safe(p, tmp, &lst->list, pcs_list) { + if (vi->fprt_len == p->fprt_len && + !memcmp(vi->fprt, p->fprt, p->fprt_len)) {use string compare helper?
For the same reasons mentioned above.
Quoted 60 lines
Quoted 60 lines
+
pcs_inode= p;
+ break;
+ }
+ }
+ if (pcs_inode)
+down_read(&pcs_inode->pcs_rwsem);
+mutex_unlock(&lst->lmutex);fs/erofs/pagecache_share.c new file +14
-0+ + return pcs_inode ? &pcs_inode->vfs_inode : ERR_PTR(-EINVAL); +} + +static int erofs_pcs_file_open (struct inode *inode,struct file *file) +{ + struct inode *pcs_inode; + struct file *ano_file; + + pcs_inode = erofs_pcs_get4read(inode); + if (IS_ERR(pcs_inode)) + return PTR_ERR(pcs_inode); + + ano_file = alloc_file_pseudo(pcs_inode, file->f_path.mnt,"[
erofs_pcs_f]",
+O_RDONLY,&erofs_file_fops); +file_ra_state_init(&ano_file->f_ra, file->f_mapping); +ihold(pcs_inode);
+file->private_data= (void *)ano_file;
+ return 0;fs/erofs/pagecache_share.c new file +3
-0+} + +static int erofs_pcs_file_release(struct inode *inode, struct file*file)
fs/erofs/pagecache_share.c new file +29
-0+{ + struct inode *pcs_inode; + + if (!file->private_data) + return -EINVAL; + pcs_inode = ((struct file *)file->private_data)->f_inode; + up_read(&EROFS_I(pcs_inode)->pcs_rwsem); + 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);
Quoted 8 lines
fs/erofs/pagecache_share.c line 166
Quoted 7 lines
+ return res;
fs/erofs/pagecache_share.c new file +6
-0+} + +static vm_fault_t erofs_pcs_fault (struct vm_fault *vmf) +{ + return filemap_fault(vmf); +}This helper is unnecessary. We could just assign the default fault helper.
Sure, thanks.
Quoted 4 lines
fs/erofs/pagecache_share.c line 169
Quoted 3 lines
fs/erofs/pagecache_share.c new file +3
-0+ +static const struct vm_operations_struct erofs_pcs_file_vm_ops = { + .fault = erofs_pcs_fault,If .map_pages not assigned, the fault around should not arouse.
I'm not quite sure about this part; I need to read the relevant code. However, the phenomenon I observed is that erofs_pcs_fault() was executed.
Thank you very much for the effort you put into carefully reviewing my code.
---
Hongzhen Luo
Quoted 42 lines
Quoted 42 lines
fs/erofs/pagecache_share.c new file +3
-0+}; + +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,15 @@ +/* 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/mutex.h> +#include "internal.h" + +void erofs_pcs_fill_inode (struct inode *inode); +int erofs_pcs_add (struct inode *inode); +int erofs_pcs_remove (struct inode *inode); + +#endif
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Hongzhen,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on xiang-erofs/dev-test]
[also build test WARNING on xiang-erofs/dev xiang-erofs/fixes linus/master v6.11-rc1 next-20240802] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Hongzhen-Luo/erofs-introduce-page-cache-share-feature/20240802-014238 base: https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git dev-test patch link: https://lore.kernel.org/r/20240731080704.678259-2-hongzhen%40linux.alibaba.com patch subject: [PATCH RFC v2 1/2] erofs: introduce page cache share feature config: arm-allmodconfig (https://download.01.org/0day-ci/archive/20240804/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240804/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
fs/erofs/pagecache_share.c: In function 'erofs_pcs_fill_inode':
>> fs/erofs/pagecache_share.c:39:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
39 | }
| ^vim +39 fs/erofs/pagecache_share.c
22
23 void erofs_pcs_fill_inode(struct inode *inode)
24 {
25 struct erofs_inode *vi = EROFS_I(inode);
26 char fprt[PCS_FPRT_MAXLEN];
27
28 vi->fprt_len = erofs_getxattr(inode, PCS_FPRT_IDX, PCS_FPRT_NAME, fprt,
29 PCS_FPRT_MAXLEN);
30 if (vi->fprt_len > 0 && vi->fprt_len <= PCS_FPRT_MAXLEN) {
31 vi->fprt = kmalloc(vi->fprt_len, GFP_KERNEL);
32 if (IS_ERR(vi->fprt)) {
33 vi->fprt_len = -1;
34 return;
35 }
36 memcpy(vi->fprt, fprt, vi->fprt_len);
37 vi->fprt_hash = xxh32(vi->fprt, vi->fprt_len, 0);
38 }
> 39 }
40 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v2 2/2 erofs: apply the page cache share feature
In reply to Hongzhen LuoThis modifies relevant functions to apply the page cache share feature.
---
v2: Make adjustments based on the latest implementation.
v1: https://lore.kernel.org/all/[email protected]/
--- fs/erofs/inode.c | 23 +++++++++++++++++++++++ fs/erofs/super.c | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+)
fs/erofs/inode.c +23 -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,16 @@ 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) { +iget_failed (inode); +return ERR_PTR (err); + } + } +#endif }return inode; }
fs/erofs/super.c +23 -0
@@ -11,6 +11,7 @@#include <linux/fs_parser.h> #include <linux/exportfs.h> #include "xattr.h" +#include "pagecache_share.h" #define CREATE_TRACE_POINTS #include <trace/events/erofs.h>
@@ -95,6 +96,10 @@ 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); +init_rwsem (&vi->pcs_rwsem); +#endif return &vi->vfs_inode; }
@@ -108,6 +113,21 @@ 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." ); +kfree (vi->fprt); + vi->fprt =NULL ; + } +} +#endif +static bool check_layout_compatibility (struct super_block *sb,struct erofs_super_block *dsb) {
@@ -937,6 +957,9 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)const struct super_operations erofs_sops = { .put_super = erofs_put_super, .alloc_inode = erofs_alloc_inode, +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + .destroy_inode = erofs_destroy_inode, +#endif .free_inode = erofs_free_inode, .statfs = erofs_statfs, .show_options = erofs_show_options,
Signature · 1 line
2.43.5
Re: [PATCH RFC v2 2/2] erofs: apply the page cache share feature
Quoted 25 lines · Hongzhen Luo
This modifies relevant functions to apply the page cache share feature.
Signed-off-by Hongzhen Luo <[email protected]>---
v2: Make adjustments based on the latest implementation.
v1: https://lore.kernel.org/all/[email protected]/--- fs/erofs/inode.c | 23 +++++++++++++++++++++++ fs/erofs/super.c | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+)fs/erofs/inode.c +1
-0@@ -5,6 +5,7 @@* Copyright (C) 2021, Alibaba Cloud */ #include "xattr.h" +#include "pagecache_share.h" #include <trace/events/erofs.h>
fs/erofs/inode.c static int erofs_fill_inode(struct inode *inode) · line 239
Quoted 10 lines
fs/erofs/inode.c +5
-0@@ -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 the compress data is not support, the erofs_pcs_fill_inode should fill the fingerprint in this branch only.
Quoted 13 lines
fs/erofs/inode.c +7
-0+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;
fs/erofs/inode.c struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid) · line 344
Quoted 7 lines
fs/erofs/inode.c +3
-0@@ -325,6 +338,16 @@ 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 &&may be S_ISREG macro is better. +EROFS_I (inode)->fprt_len >0 ) {
Perhaps this logic need to be enclosed within unlock_new_inode.
Quoted 34 lines
fs/erofs/inode.c +7
-0+ err =erofs_pcs_add (inode); +if (err) { +iget_failed (inode); +return ERR_PTR (err); + } + } +#endif }return inode;}fs/erofs/super.c +5
-0@@ -11,6 +11,7 @@#include <linux/fs_parser.h> #include <linux/exportfs.h> #include "xattr.h" +#include "pagecache_share.h" #define CREATE_TRACE_POINTS #include <trace/events/erofs.h> @@ -95,6 +96,10 @@ 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); +init_rwsem (&vi->pcs_rwsem); +#endif return &vi->vfs_inode; }
fs/erofs/super.c static void erofs_free_inode(struct inode *inode) · line 121
Quoted 9 lines
fs/erofs/super.c +6
-0@@ -108,6 +113,21 @@ 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 &&
using S_ISREG macro is better.
Quoted 22 lines
fs/erofs/super.c +12
-0+ EROFS_I(inode)->fprt_len > 0) { +if (erofs_pcs_remove (inode)) +erofs_err (inode->i_sb,"pcs: fail to remove inode." ); +kfree (vi->fprt); + vi->fprt =NULL ; + } +} +#endif +static bool check_layout_compatibility (struct super_block *sb,struct erofs_super_block *dsb) {@@ -937,6 +957,9 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root)const struct super_operations erofs_sops = { .put_super = erofs_put_super, .alloc_inode = erofs_alloc_inode, +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + .destroy_inode = erofs_destroy_inode, +#endif .free_inode = erofs_free_inode, .statfs = erofs_statfs, .show_options = erofs_show_options,
Re: [PATCH RFC v2 2/2] erofs: apply the page cache share feature
Quoted 29 lines · Hongbo Li
Quoted 26 lines · Hongzhen Luo
This modifies relevant functions to apply the page cache share feature.
Signed-off-by Hongzhen Luo <[email protected]>---
v2: Make adjustments based on the latest implementation.
v1: https://lore.kernel.org/all/[email protected]/
---
fs/erofs/inode.c| 23 +++++++++++++++++++++++
fs/erofs/super.c| 23 +++++++++++++++++++++++2 files changed, 46 insertions(+)fs/erofs/inode.c +1
-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;
Quoted 8 lines
Quoted 7 lines
fs/erofs/inode.c +3
-0+#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; +#ifdefCONFIG_EROFS_FS_PAGE_CACHE_SHARE
+ else {If the compress data is not support, the
erofs_pcs_fill_inodeshould fill the fingerprint in this branch only.
Sure, thanks.
Quoted 21 lines
Quoted 20 lines
+ 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;
caseS_IFDIR:
inode->i_op=&erofs_dir_iops;fs/erofs/inode.c +2
-0@@ -325,6 +338,16 @@ 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 &&may be S_ISREGmacro is better.
Quoted 2 lines
Quoted 1 line
+ EROFS_I(inode)->fprt_len > 0) {
Perhaps this logic need to be enclosed within
unlock_new_inode.
Sure, thanks.
Quoted 33 lines
Quoted 33 lines
+ err =
erofs_pcs_add(inode);
+ if (err) {
+iget_failed(inode);
+ returnERR_PTR(err);
+ }
+ }
+#endif
}
return inode;
}fs/erofs/super.c +5
-0@@ -11,6 +11,7 @@ #include <linux/fs_parser.h> #include <linux/exportfs.h> #include "xattr.h" +#include "pagecache_share.h" #define CREATE_TRACE_POINTS #include <trace/events/erofs.h>@@ -95,6 +96,10 @@ 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); + init_rwsem(&vi->pcs_rwsem); +#endif return &vi->vfs_inode; } @@ -108,6 +113,21 @@ static void erofs_free_inode(struct inode *inode)
kmem_cache_free(erofs_inode_cachep, vi);
Quoted 8 lines
fs/erofs/super.c static void erofs_free_inode(struct inode *inode) · line 121
Quoted 7 lines
}
+#ifdefCONFIG_EROFS_FS_PAGE_CACHE_SHAREfs/erofs/super.c +5
-0+static void erofs_destroy_inode (struct inode *inode) +{ + struct erofs_inode *vi = EROFS_I(inode); + + if ((inode->i_mode & S_IFMT) == S_IFREG &&using
S_ISREGmacro is better.
Thanks.
---
Hongzhen Luo
Quoted 23 lines
Quoted 23 lines
+ EROFS_I(inode)->fprt_len > 0) {
+ if (erofs_pcs_remove(inode))
+erofs_err(inode->i_sb, "pcs: fail to remove inode."); +kfree(vi->fprt);
+vi->fprt= NULL;
+ }fs/erofs/super.c +3
-0+} +#endif +static bool
check_layout_compatibility(struct super_block *sb, struct erofs_super_block *dsb){fs/erofs/super.c +3
-0@@ -937,6 +957,9 @@ static int erofs_show_options(struct seq_file *seq, struct dentry *root) const struct super_operations erofs_sops = { .put_super = erofs_put_super, .alloc_inode = erofs_alloc_inode, +#ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE + .destroy_inode = erofs_destroy_inode, +#endif .free_inode = erofs_free_inode, .statfs = erofs_statfs,.show_options =
erofs_show_options,