[RFC PATCH 6/6] erofs: enable page cache sharing in fscache mode
From: Jingbo Xu
Date: Fri Jan 06 2023 - 07:54:24 EST
Erofs supports chunk deduplication to reduce disk usage. Furthermore we
can make inodes share page cache of these deduplicated chunks to reduce
the memory usage. This shall be much usable in container scenarios as
deduplication is requisite for container image.
This can be achieved by managing page cache of deduplicated chunks in
blob's address space. In this way, all inodes sharing the deduplicated
chunk will refer to and share the page cache in the blob's address
space.
So far there are some restrictions for enabling this feature.
The page cache sharing feature also supports .mmap(). The reverse
mapping requires that one vma can not be shared among inodes and can
be linked to only one inode. As the vma will be finally linked to the
blob's address space when page cache sharing enabled, the restriction of
the reverse mapping actually requires that the mapped file area can not
be mapped to multiple blobs. Thus page cache sharing can only be
enabled for those files mapped to one blob.
The chunk based data layout guarantees that a chunk will not cross the
device (blob) boundary. Thus in chunk based data layout, those files
smaller than the chunk size shall be guaranteed to be mapped to one
blob. As chunk size is tunable at a per-file basis, this restriction
can be relaxed at image building phase. As long as we ensure that the
file can not be deduplicated, the file's chunk size can be set to a
reasonable value larger than the file size, so that the page cache
sharing feature can be enabled on this file later.
The second restriction is that EROFS_BLKSIZ mus be multiples of
PAGE_SIZE to avoid data leakage. Otherwise unrelated data may be
exposed at the end of the last page, since file's data is arranged in
unit of EROFS_BLKSIZ in the image.
Signed-off-by: Jingbo Xu <jefflexu@xxxxxxxxxxxxxxxxx>
---
fs/erofs/inode.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/fs/erofs/inode.c b/fs/erofs/inode.c
index d3b8736fa124..8fe9b29422b5 100644
--- a/fs/erofs/inode.c
+++ b/fs/erofs/inode.c
@@ -241,6 +241,29 @@ static int erofs_fill_symlink(struct inode *inode, void *kaddr,
return 0;
}
+static bool erofs_can_share_page_cache(struct inode *inode)
+{
+ struct erofs_inode *vi = EROFS_I(inode);
+
+ /* enable page cache sharing only in share domain mode */
+ if (!erofs_is_fscache_mode(inode->i_sb) ||
+ !EROFS_SB(inode->i_sb)->domain_id)
+ return false;
+
+ if (vi->datalayout != EROFS_INODE_CHUNK_BASED)
+ return false;
+
+ /* avoid crossing multi devicces/blobs */
+ if (inode->i_size > 1UL << vi->chunkbits)
+ return false;
+
+ /* avoid data leakage in mmap routine */
+ if (EROFS_BLKSIZ % PAGE_SIZE)
+ return false;
+
+ return true;
+}
+
static int erofs_fill_inode(struct inode *inode)
{
struct erofs_inode *vi = EROFS_I(inode);
@@ -262,6 +285,10 @@ static int erofs_fill_inode(struct inode *inode)
inode->i_op = &erofs_generic_iops;
if (erofs_inode_is_data_compressed(vi->datalayout))
inode->i_fop = &generic_ro_fops;
+#ifdef CONFIG_EROFS_FS_ONDEMAND
+ else if (erofs_can_share_page_cache(inode))
+ inode->i_fop = &erofs_fscache_share_file_fops;
+#endif
else
inode->i_fop = &erofs_file_fops;
break;
--
2.19.1.6.gb485710b