Handling 0-sized pseudo-files (cgroupfs/sysfs/procfs) in virtiofs and FUSE
RFC 1/1 Handling 0-sized pseudo-files (cgroupfs/sysfs/procfs) in virtiofs and FUSE
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:
fs/fuse/file.c +2 -0
@@ -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
Miklos Szeredi
maintainerQuoted 3 lines · Changyuan Lyu
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)?
Why not just set "--cache never" for the pseudo fs export?
Quoted 2 lines
2. For the kernel: Does the proposed
copy_splice_read()fallback forFOPEN_DIRECT_IOfiles infuse_splice_read()look acceptable?
Yes, in fact this patch has been sitting in my local tree for a week.
Pushed out now, updated with a link to this report as well.
Thanks,
Miklos
Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Changyuan Lyu
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)?
Why not just set "--cache never" for the pseudo fs export?
Enabling caching does give a pretty noticeable performance improvement for those files that can be cached, and we'd like to have just a single export instead of setting up multiple and assembling it piecemeal. (To this end we do have an out-of-tree extension to the virtiofsd-rs passthrough module to allow changing caching duration on a per file basis as the protocol allows..., been meaning to start a thread if folks are interested in this since it has proven somewhat useful for us now).
But I felt having first class support for this felt more "right" - enabling caching shouldn't break reading of certain files, and the server is in a position to know if a file would support this or not.
khazhy
Quoted 9 lines
Quoted 3 lines
2. For the kernel: Does the proposed
copy_splice_read()fallback forFOPEN_DIRECT_IOfiles infuse_splice_read()look acceptable?Yes, in fact this patch has been sitting in my local tree for a week.
Pushed out now, updated with a link to this report as well.Thanks,
Miklos
Miklos Szeredi
maintainerQuoted 20 lines · Khazhy Kumykov
Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Changyuan Lyu
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)?
Why not just set "--cache never" for the pseudo fs export?
Enabling caching does give a pretty noticeable performance improvement for those files that can be cached, and we'd like to have just a single export instead of setting up multiple and assembling it piecemeal. (To this end we do have an out-of-tree extension to the virtiofsd-rs passthrough module to allow changing caching duration on a per file basis as the protocol allows..., been meaning to start a thread if folks are interested in this since it has proven somewhat useful for us now).
Quoted 3 lines
But I felt having first class support for this felt more "right" - enabling caching shouldn't break reading of certain files, and the server is in a position to know if a file would support this or not.
I'm fine with this. But virtiofsd maintainers will have to make a decision about merging or not merging this feature.
One idea that occurs to me is that the kernel is in a much better position to give a hint:
- file is normal: POSIX semantics apply
- file is pseudo; POSIX semantics don't apply
The "normal" category could also be split into:
- file is local: changes always generate fanotify events
- file is remote: changes do not always generate fanotify events
Thanks,
Miklos
Quoted 22 lines · Miklos Szeredi
Quoted 20 lines · Khazhy Kumykov
Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Changyuan Lyu
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)?
Why not just set "--cache never" for the pseudo fs export?
Enabling caching does give a pretty noticeable performance improvement for those files that can be cached, and we'd like to have just a single export instead of setting up multiple and assembling it piecemeal. (To this end we do have an out-of-tree extension to the virtiofsd-rs passthrough module to allow changing caching duration on a per file basis as the protocol allows..., been meaning to start a thread if folks are interested in this since it has proven somewhat useful for us now).
Quoted 6 lines
Quoted 3 lines
But I felt having first class support for this felt more "right" - enabling caching shouldn't break reading of certain files, and the server is in a position to know if a file would support this or not.
I'm fine with this. But virtiofsd maintainers will have to make a decision about merging or not merging this feature.
Ack. Am hoping this list is the right place to get attention :) I tried signing up for gitlab but I think it very much does not like the corporate network here -- it was asking me for a credit card to create an account (!!!).
Quoted 3 lines
One idea that occurs to me is that the kernel is in a much better position to give a hint:
I'm not sure I understand the proposal here: the guest kernel doesn't have context beyond what we pass through the inode lookup, right? I don't think we have enough information /in the guest/ to know if a file was a special pseudo file or not /on the host/. I might be missing something...?
Quoted 11 lines
- file is normal: POSIX semantics apply
- file is pseudo; POSIX semantics don't applyThe "normal" category could also be split into:
- file is local: changes always generate fanotify events
- file is remote: changes do not always generate fanotify eventsThanks,
Miklos
Miklos Szeredi
maintainerQuoted 5 lines · Khazhy Kumykov
I'm not sure I understand the proposal here: the guest kernel doesn't have context beyond what we pass through the inode lookup, right? I don't think we have enough information /in the guest/ to know if a file was a special pseudo file or not /on the host/. I might be missing something...?
I mean the host kernel is in good position to give a hint to the server about the semantics of the file being served.
Thanks,
Miklos
Quoted 11 lines
Quoted 11 lines
- file is normal: POSIX semantics apply
- file is pseudo; POSIX semantics don't applyThe "normal" category could also be split into:
- file is local: changes always generate fanotify events
- file is remote: changes do not always generate fanotify eventsThanks,
Miklos