Skip to contentSkip to conversation

erofs: support splice() in inode_share mode

PATCH In review Zhan Xusheng 3 participants 3 messages Aug 21 – Aug 24 EROFS file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 erofs: support splice() in inode_share mode

From: Zhan Xusheng <[email protected]>

From: Zhan Xusheng <[email protected]>

erofs_ishare_fops routes everything that touches the page cache to the backing file in ->private_data: read_iter clones the iocb onto it, mmap does vma_set_file(), fadvise calls vfs_fadvise() on it. splice_read was left as filemap_splice_read(), which works on the user file's own mapping:
it does init_sync_kiocb(&iocb, in), and filemap_get_pages() then takes iocb->ki_filp->f_mapping.

So splice() and sendfile() fill the per-inode page cache rather than the shared one. The content is the same either way, since erofs_fill_inode() sets a_ops on that mapping too, which is why this went unnoticed. Two identical 8 MiB files under inode_share, reading one of them with splice(2) alone, in pages:

  before   own 2048   shared 0
  after    own 0      shared 2048

"own" is cachestat(fd), which reports the file's own mapping; "shared" is mmap()+mincore(), which erofs_ishare_mmap() redirects to the backing file.

Read through the backing file, as read_iter already does.

---
v1->v2:
- Retitled and dropped the Fixes tag: the sharing is best effort, so this is an enhancement rather than a fix.
- Added the measured before/after.
- Left file_accessed() alone, since it is a no-op under SB_NOATIME.
- An erofs-utils test is sent separately against experimental-tests.

v1: https://lore.kernel.org/all/[email protected]

 fs/erofs/ishare.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/erofs/ishare.c +8 -1
index fa7d4112dec5..01dc53e9e3ad 100644
@@ -148,6 +148,13 @@ static int erofs_ishare_mmap(struct file *file, struct vm_area_struct *vma)
 	return generic_file_readonly_mmap(file, vma);
 }
 
+static ssize_t erofs_ishare_splice_read(struct file *in, loff_t *ppos,
+					struct pipe_inode_info *pipe,
+					size_t len, unsigned int flags)
+{
+	return filemap_splice_read(in->private_data, ppos, pipe, len, flags);
+}
+
 static int erofs_ishare_fadvise(struct file *file, loff_t offset,
 				loff_t len, int advice)
 {
@@ -161,7 +168,7 @@ const struct file_operations erofs_ishare_fops = {
 	.mmap		= erofs_ishare_mmap,
 	.release	= erofs_ishare_file_release,
 	.get_unmapped_area = thp_get_unmapped_area,
-	.splice_read	= filemap_splice_read,
+	.splice_read	= erofs_ishare_splice_read,
 	.fadvise	= erofs_ishare_fadvise,
 };
 
Signature · 1 line
2.43.0
Reviewed-by Jingbo Xu \ on v2 1/1
#1 lore raw
Quoted 20 lines · Zhan Xusheng

From: Zhan Xusheng <[email protected]>

From: Zhan Xusheng <[email protected]>

erofs_ishare_fops routes everything that touches the page cache to the backing file in ->private_data: read_iter clones the iocb onto it, mmap does vma_set_file(), fadvise calls vfs_fadvise() on it. splice_read was left as filemap_splice_read(), which works on the user file's own mapping:
it does init_sync_kiocb(&iocb, in), and filemap_get_pages() then takes iocb->ki_filp->f_mapping.

So splice() and sendfile() fill the per-inode page cache rather than the shared one. The content is the same either way, since erofs_fill_inode() sets a_ops on that mapping too, which is why this went unnoticed. Two identical 8 MiB files under inode_share, reading one of them with splice(2) alone, in pages:

  before   own 2048   shared 0
  after    own 0      shared 2048
Quoted 7 lines

"own" is cachestat(fd), which reports the file's own mapping; "shared" is mmap()+mincore(), which erofs_ishare_mmap() redirects to the backing file.

Read through the backing file, as read_iter already does.

LGTM.

Quoted 40 lines

---
v1->v2:
- Retitled and dropped the Fixes tag: the sharing is best effort, so this is an enhancement rather than a fix.
- Added the measured before/after.
- Left file_accessed() alone, since it is a no-op under SB_NOATIME.
- An erofs-utils test is sent separately against experimental-tests.

v1: https://lore.kernel.org/all/[email protected]

 fs/erofs/ishare.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/erofs/ishare.c +8 -1
index fa7d4112dec5..01dc53e9e3ad 100644
@@ -148,6 +148,13 @@ static int erofs_ishare_mmap(struct file *file, struct vm_area_struct *vma)
 	return generic_file_readonly_mmap(file, vma);
 }
 
+static ssize_t erofs_ishare_splice_read(struct file *in, loff_t *ppos,
+					struct pipe_inode_info *pipe,
+					size_t len, unsigned int flags)
+{
+	return filemap_splice_read(in->private_data, ppos, pipe, len, flags);
+}
+
 static int erofs_ishare_fadvise(struct file *file, loff_t offset,
 				loff_t len, int advice)
 {
@@ -161,7 +168,7 @@ const struct file_operations erofs_ishare_fops = {
 	.mmap		= erofs_ishare_mmap,
 	.release	= erofs_ishare_file_release,
 	.get_unmapped_area = thp_get_unmapped_area,
-	.splice_read	= filemap_splice_read,
+	.splice_read	= erofs_ishare_splice_read,
 	.fadvise	= erofs_ishare_fadvise,
 };
 
Signature · 2 lines
Thanks,
Jingbo
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Zhan Xusheng
Quoted 20 lines · Zhan Xusheng

From: Zhan Xusheng <[email protected]>

From: Zhan Xusheng <[email protected]>

erofs_ishare_fops routes everything that touches the page cache to the backing file in ->private_data: read_iter clones the iocb onto it, mmap does vma_set_file(), fadvise calls vfs_fadvise() on it. splice_read was left as filemap_splice_read(), which works on the user file's own mapping:
it does init_sync_kiocb(&iocb, in), and filemap_get_pages() then takes iocb->ki_filp->f_mapping.

So splice() and sendfile() fill the per-inode page cache rather than the shared one. The content is the same either way, since erofs_fill_inode() sets a_ops on that mapping too, which is why this went unnoticed. Two identical 8 MiB files under inode_share, reading one of them with splice(2) alone, in pages:

   before   own 2048   shared 0
   after    own 0      shared 2048
Quoted 7 lines

"own" is cachestat(fd), which reports the file's own mapping; "shared" is mmap()+mincore(), which erofs_ishare_mmap() redirects to the backing file.

Read through the backing file, as read_iter already does.

Thanks,