Hi all,
We have been investigating an issue when sharing host pseudo-filesystems
(such as /sys/fs/cgroup, /proc, and /sys) into a guest VM via virtiofs.
For our environment, we would like to provide statistics about the
enclosing host-level container (such as cgroup metrics) to feed into
application decisions in the VM guest, for which we would like to pass
through pseudo-filesystems like /sys/fs/cgroup.
Virtiofs does not support this well today. When guest applications read
dynamic pseudo-files (for example, /sys/fs/cgroup/cgroup.controllers or
cgroup.stat), they frequently receive 0 bytes (EOF), even though the host
file contains live dynamic content.
We would like to discuss our findings and proposed solutions for both
virtiofsd and the guest kernel FUSE client.
1. Problem Analysis
===================
In Linux, kernel pseudo-files (kernfs, cgroupfs, procfs) report
st_mode = S_IFREG and st_size = 0 via stat()/statx() because their
content is generated dynamically on read().
In virtiofsd with the default CachePolicy::Auto mode, open_flags is 0
(FOPEN_DIRECT_IO is not set). Consequently, the guest kernel routes reads
through the page cache:
fuse_file_read_iter()
-> fuse_cache_read_iter()
-> generic_file_read_iter() (mm/filemap.c)
In generic_file_read_iter(), the kernel checks:
if (iocb->ki_pos >= i_size_read(inode))
return 0;
Because inode->i_size is 0, generic_file_read_iter() immediately returns
0 bytes (EOF) from guest RAM without ever issuing a FUSE_READ request to
virtiofsd.
2. Host Daemon (virtiofsd) Solution
===================================
To prevent generic_file_read_iter() from truncating reads at i_size == 0,
the guest must open the file with FOPEN_DIRECT_IO so that reads go through
fuse_direct_read_iter() and dispatch FUSE_READ to virtiofsd.
However, setting FOPEN_DIRECT_IO unconditionally on all 0-sized files is
problematic because regular empty files (e.g. touch empty.txt) grown
while open via ftruncate() may encounter mmap(MAP_SHARED) limitations on
older kernels without FUSE_DIRECT_IO_ALLOW_MMAP.
In virtiofsd [1], we implemented an automatic detection in
do_open() and create():
When a regular file has st_size == 0, virtiofsd inspects the underlying
host filesystem type using fstatfs64(). If the file resides on a known
pseudo-filesystem (e.g. CGROUP2_SUPER_MAGIC, CGROUP_SUPER_MAGIC,
PROC_SUPER_MAGIC, SYSFS_MAGIC, TRACEFS_MAGIC, DEBUGFS_MAGIC,
SECURITYFS_MAGIC, BPF_FS_MAGIC), virtiofsd sets OpenOptions::DIRECT_IO
(FOPEN_DIRECT_IO) in the open reply:
if flags & (libc::O_DIRECTORY as u32) == 0
&& !opts.contains(OpenOptions::DIRECT_IO)
&& is_zero_sized_pseudo_file(&file)?
{
opts |= OpenOptions::DIRECT_IO;
}
With this change, standard read() syscalls (e.g. via dd, head, or runtimes)
in the guest receive FOPEN_DIRECT_IO and successfully read dynamic host
pseudo-file content.
3. Guest Kernel Issue: fuse_splice_read()
=========================================
Even with FOPEN_DIRECT_IO returned by virtiofsd, utilities that use
splice()/sendfile() (such as BusyBox cat, GNU cat in certain cases, or cp)
still fail and return 0 bytes.
Looking at fs/fuse/file.c:fuse_splice_read():
static ssize_t fuse_splice_read(struct file *in, loff_t *ppos,
struct pipe_inode_info *pipe, size_t len,
unsigned int flags)
{
struct fuse_file *ff = in->private_data;
/* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
return fuse_passthrough_splice_read(in, ppos, pipe, len, flags);
else
return filemap_splice_read(in, ppos, pipe, len, flags);
}
Notice that when FOPEN_DIRECT_IO is set, fuse_splice_read() falls back to
filemap_splice_read() in mm/filemap.c. filemap_splice_read() also checks
*ppos >= i_size_read() and immediately breaks with 0 bytes spliced.
Other filesystems (such as 9p, ceph, cifs, kernfs) handle direct I/O
splice reads by delegating to copy_splice_read(), which invokes
in->f_op->read_iter() (fuse_direct_read_iter).
We propose the following fix for the guest kernel fs/fuse/file.c:
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1884,6 +1884,8 @@ static ssize_t fuse_splice_read(struct file *in, loff_t
*ppos,
/* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
return fuse_passthrough_splice_read(in, ppos, pipe, len, flags);
+ else if (ff->open_flags & FOPEN_DIRECT_IO)
+ return copy_splice_read(in, ppos, pipe, len, flags);
else
return filemap_splice_read(in, ppos, pipe, len, flags);
}
Questions for Upstream
======================
1. For virtiofsd: Does the automatic detection of pseudo-filesystems via
fstatfs magic match upstream expectations, or would upstream prefer an
explicit configuration option (such as per-path cache policies)?
2. For the kernel: Does the proposed copy_splice_read() fallback for
FOPEN_DIRECT_IO files in fuse_splice_read() look acceptable?
Any feedback and suggestions would be greatly appreciated.
[1]
https://gitlab.com/Lencerf/virtiofsd/-/commit/ee9445c7d2963a60e554f8483bd47b7c1634d569
Thanks,
Changyuan Lyu