Skip to contentSkip to conversation

fuse: Forward vfs_fadvise to backing_file in passthrough mode

PATCH Superseded Shai Barack 4 participants 9 messages Sep 9 – Oct 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse: Forward vfs_fadvise to backing_file in passthrough mode

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
---
 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  7 +++++++
 fs/fuse/passthrough.c | 16 ++++++++++++++++
 3 files changed, 34 insertions(+)
fs/fuse/file.c +11 -0
index a4d736945..81709ab82 100644
@@ -3537,6 +3537,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
 	return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct fuse_file *ff = file->private_data;
+
+	if (fuse_file_passthrough(ff))
+		return fuse_passthrough_fadvise(ff, offset, len, advice);
+
+	return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
 	.llseek		= fuse_file_llseek,
 	.read_iter	= fuse_file_read_iter,
@@ -3557,6 +3567,7 @@ static const struct file_operations fuse_file_operations = {
 	.fallocate	= fuse_file_fallocate,
 	.copy_file_range = fuse_copy_file_range,
 	.fop_flags	= FOP_DONTCACHE,
+	.fadvise	= fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +7 -0
index 939553b34..50073e070 100644
@@ -1545,6 +1545,12 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+
+static inline int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset,
+					   loff_t len, int advice)
+{
+	return -EOPNOTSUPP;
+}
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1581,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 				      struct file *out, loff_t *ppos,
 				      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice);
 
 /* backing.c */
 
fs/fuse/passthrough.c +16 -0
index 059e57969..8062110dd 100644
@@ -382,3 +382,19 @@ void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb)
 	put_cred(ff->cred);
 	ff->cred = NULL;
 }
+
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice)
+{
+	struct file *backing_file = fuse_file_passthrough(ff);
+	const struct cred *old_cred;
+	int ret;
+
+	if (!backing_file)
+		return -EINVAL;
+
+	old_cred = override_creds(ff->cred);
+	ret = vfs_fadvise(backing_file, offset, len, advice);
+	revert_creds(old_cred);
+
+	return ret;
+}
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 33 lines · Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Quoted 1 line

---

Hi Shai,

This makes sense.

Please CC fuse-devel with future posting.

See some nits below...

Quoted 39 lines
 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  7 +++++++
 fs/fuse/passthrough.c | 16 ++++++++++++++++
 3 files changed, 34 insertions(+)
fs/fuse/file.c +11 -0
index a4d736945..81709ab82 100644
@@ -3537,6 +3537,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
        return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+       struct fuse_file *ff = file->private_data;
+
+       if (fuse_file_passthrough(ff))
+               return fuse_passthrough_fadvise(ff, offset, len, advice);
+
+       return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
        .llseek         = fuse_file_llseek,
        .read_iter      = fuse_file_read_iter,
@@ -3557,6 +3567,7 @@ static const struct file_operations fuse_file_operations = {
        .fallocate      = fuse_file_fallocate,
        .copy_file_range = fuse_copy_file_range,
        .fop_flags      = FOP_DONTCACHE,
+       .fadvise        = fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +0 -0
index 939553b34..50073e070 100644

fs/fuse/fuse_i.h static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb) · line 1553

Quoted 9 lines
fs/fuse/fuse_i.h +6 -0
@@ -1545,6 +1545,12 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+
+static inline int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset,
+                                          loff_t len, int advice)
+{
+       return -EOPNOTSUPP;
+}

Not needed, the same as fuse_passthrough_mmap stub is not needed because if (fuse_file_passthrough()) branch is optimized out.

Quoted 18 lines
fs/fuse/fuse_i.h +1 -0
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1581,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
                                      struct file *out, loff_t *ppos,
                                      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice);
 
 /* backing.c */
 
fs/fuse/passthrough.c +0 -0
index 059e57969..8062110dd 100644
@@ -382,3 +382,19 @@ void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb)
        put_cred(ff->cred);
        ff->cred = NULL;

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 393

Quoted 10 lines
fs/fuse/passthrough.c +9 -0
 }
+
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice)
+{
+       struct file *backing_file = fuse_file_passthrough(ff);
+       const struct cred *old_cred;
+       int ret;
+
+       if (!backing_file)
+               return -EINVAL;

Not needed. already checked.

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 400

Quoted 7 lines
fs/fuse/passthrough.c +7 -0
+
+       old_cred = override_creds(ff->cred);
+       ret = vfs_fadvise(backing_file, offset, len, advice);
+       revert_creds(old_cred);
+
+       return ret;
+}

Please reorder this above fuse_passthrough_open() along with the other passthrough ops. open and release are special.

</nits>

This got me thinking - do we have any other purely page cache operations that should be executed on the passthrough file?

There is one pattern that stands out: fuse_fsync()/fuse_flush() in case server does not implement those ops.

Currently, they are mostly a noop for fuse passthrough files, but it could make sense to passthrough these two ops on the backing file same as overlayfs does.

This has implications on the use case that you described.
If an application wants to synchronically evict page cache of large file that was recently written, POSIX_FADV_DONTNEED alone is not enough.

Some would claim that passthtough of fallocate() to backing file in case server does not implement FUSE_FALLCOATE also makes sense, but that will be a visible change of behavior from current -EOPNOTSUPP, so I think this is better to do with an opt-in "passthrough ops mask".

Thanks,
Amir.

#2 lore raw

PATCH v2 1/1 fuse: Forward vfs_fadvise to backing_file in passthrough mode

In reply to Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

---
v2:
- Remove redundant !backing_file check in fuse_passthrough_fadvise() (Amir Goldstein)
- Remove unused fuse_passthrough_fadvise() stub under #ifndef CONFIG_FUSE_PASSTHROUGH (Amir Goldstein)
- Reorder fuse_passthrough_fadvise() above fuse_passthrough_open() alongside other passthrough ops (Amir Goldstein)
- Add [email protected] to CC list (Amir Goldstein, TJ Mercier)

 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  1 +
 fs/fuse/passthrough.c | 13 +++++++++++++
 3 files changed, 25 insertions(+)
fs/fuse/file.c +11 -0
index a4d736945..81709ab82 100644
@@ -3537,6 +3537,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
 	return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct fuse_file *ff = file->private_data;
+
+	if (fuse_file_passthrough(ff))
+		return fuse_passthrough_fadvise(ff, offset, len, advice);
+
+	return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
 	.llseek		= fuse_file_llseek,
 	.read_iter	= fuse_file_read_iter,
@@ -3557,6 +3567,7 @@ static const struct file_operations fuse_file_operations = {
 	.fallocate	= fuse_file_fallocate,
 	.copy_file_range = fuse_copy_file_range,
 	.fop_flags	= FOP_DONTCACHE,
+	.fadvise	= fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +1 -0
index 939553b34..6c60716e7 100644
@@ -1581,6 +1581,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 				      struct file *out, loff_t *ppos,
 				      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice);
 
 /* backing.c */
 
fs/fuse/passthrough.c +13 -0
index 059e57969..8a8fa6b8c 100644
@@ -160,6 +160,19 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
 	return backing_file_mmap(backing_file, vma, &ctx);
 }
 
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice)
+{
+	struct file *backing_file = fuse_file_passthrough(ff);
+	const struct cred *old_cred;
+	int ret;
+
+	old_cred = override_creds(ff->cred);
+	ret = vfs_fadvise(backing_file, offset, len, advice);
+	revert_creds(old_cred);
+
+	return ret;
+}
+
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
 	if (fb && refcount_inc_not_zero(&fb->count))
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#3 lore raw
Quoted 24 lines · Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Quoted 10 lines
Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

Thanks,
Amir.

Quoted 79 lines

---
v2:
- Remove redundant !backing_file check in fuse_passthrough_fadvise() (Amir Goldstein)
- Remove unused fuse_passthrough_fadvise() stub under #ifndef CONFIG_FUSE_PASSTHROUGH (Amir Goldstein)
- Reorder fuse_passthrough_fadvise() above fuse_passthrough_open() alongside other passthrough ops (Amir Goldstein)
- Add [email protected] to CC list (Amir Goldstein, TJ Mercier)

 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  1 +
 fs/fuse/passthrough.c | 13 +++++++++++++
 3 files changed, 25 insertions(+)
fs/fuse/file.c +11 -0
index a4d736945..81709ab82 100644
@@ -3537,6 +3537,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
        return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+       struct fuse_file *ff = file->private_data;
+
+       if (fuse_file_passthrough(ff))
+               return fuse_passthrough_fadvise(ff, offset, len, advice);
+
+       return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
        .llseek         = fuse_file_llseek,
        .read_iter      = fuse_file_read_iter,
@@ -3557,6 +3567,7 @@ static const struct file_operations fuse_file_operations = {
        .fallocate      = fuse_file_fallocate,
        .copy_file_range = fuse_copy_file_range,
        .fop_flags      = FOP_DONTCACHE,
+       .fadvise        = fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +1 -0
index 939553b34..6c60716e7 100644
@@ -1581,6 +1581,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
                                      struct file *out, loff_t *ppos,
                                      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice);
 
 /* backing.c */
 
fs/fuse/passthrough.c +13 -1
index 059e57969..8a8fa6b8c 100644
@@ -160,6 +160,19 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
        return backing_file_mmap(backing_file, vma, &ctx);
 }
 
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice)
+{
+       struct file *backing_file = fuse_file_passthrough(ff);
+       const struct cred *old_cred;
+       int ret;
+
+       old_cred = override_creds(ff->cred);
+       ret = vfs_fadvise(backing_file, offset, len, advice);
+       revert_creds(old_cred);
+
+       return ret;
+}
+
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
        if (fb && refcount_inc_not_zero(&fb->count))
--

2.43.0

Reviewed-by on v2 1/1
#4 lore raw
In reply to Shai Barack
Quoted 24 lines · Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Quoted 1 line
#5 lore raw

Hi Miklos,

Gentle ping on this v2 patch when you have a chance. It has Reviewed-by tags from Amir and T.J.

Cheers!

Quoted 29 lines · T.J. Mercier
Quoted 24 lines · Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Quoted 1 line
#6 lore raw

PATCH v3 1/1 fuse: Forward vfs_fadvise to backing_file in passthrough mode

In reply to Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc T.J. Mercier <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

---
v3:
- Rebase onto fuse.git for-next (commit bbe2d73a8231 "fuse: don't assume ff->passthrough is set for FOPEN_PASSTHROUGH"): use fuse_is_passthrough() in fuse_file_fadvise(), take struct file * in fuse_passthrough_fadvise(), and fall back to generic_fadvise() when backing_file is NULL (T.J. Mercier)
- Collect Reviewed-by tags from Amir Goldstein and T.J. Mercier

v2:
- Remove redundant !backing_file check in fuse_passthrough_fadvise() (Amir Goldstein)
- Remove unused fuse_passthrough_fadvise() stub under #ifndef CONFIG_FUSE_PASSTHROUGH (Amir Goldstein)
- Reorder fuse_passthrough_fadvise() above fuse_passthrough_open() alongside other passthrough ops (Amir Goldstein)
- Add [email protected] to CC list (Amir Goldstein, T.J. Mercier)

 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  1 +
 fs/fuse/passthrough.c | 17 +++++++++++++++++
 3 files changed, 29 insertions(+)
fs/fuse/file.c +11 -0
index 6d707f2..6e6136e 100644
@@ -3100,6 +3100,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
 	return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct fuse_file *ff = file->private_data;
+
+	if (fuse_is_passthrough(ff))
+		return fuse_passthrough_fadvise(file, offset, len, advice);
+
+	return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
 	.llseek		= fuse_file_llseek,
 	.read_iter	= fuse_file_read_iter,
@@ -3120,6 +3130,7 @@ static const struct file_operations fuse_file_operations = {
 	.fallocate	= fuse_file_fallocate,
 	.copy_file_range = fuse_copy_file_range,
 	.setlease	= generic_setlease,
+	.fadvise	= fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +1 -0
index 8546855..49b3101 100644
@@ -1316,6 +1316,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 				      struct file *out, loff_t *ppos,
 				      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct file *file, loff_t offset, loff_t len, int advice);
 
 #ifdef CONFIG_SYSCTL
 extern int fuse_sysctl_register(void);
fs/fuse/passthrough.c +17 -0
index b43d3e0..2d29ff6 100644
@@ -148,6 +148,23 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
 	return backing_file_mmap(backing_file, vma, &ctx);
 }
 
+int fuse_passthrough_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct fuse_file *ff = file->private_data;
+	struct file *backing_file = fuse_file_passthrough(ff);
+	const struct cred *old_cred;
+	int ret;
+
+	if (!backing_file)
+		return generic_fadvise(file, offset, len, advice);
+
+	old_cred = override_creds(ff->cred);
+	ret = vfs_fadvise(backing_file, offset, len, advice);
+	revert_creds(old_cred);
+
+	return ret;
+}
+
 /*
  * Setup passthrough to a backing file.
  *
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
maintainer Applied
#7 lore raw
Quoted 7 lines · Shai Barack

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Applied, thanks.

Miklos

#8 lore raw

PATCH v2 1/1 fuse: Forward vfs_fadvise to backing_file in passthrough mode

In reply to Shai Barack

When an application invokes posix_fadvise(..., POSIX_FADV_DONTNEED) or other fadvise advice on a FUSE passthrough file descriptor, the VFS currently only executes generic_fadvise() on the upper FUSE inode mapping.

Because FUSE passthrough does not forward fadvise operations to the underlying backing file, pages residing in the lower filesystem's pagecache remain resident in memory.

During media-heavy workloads (such as image thumbnail generation or streaming), single-access file data accumulates in the inactive pagecache of the lower filesystem. Under memory pressure, the kernel retains these stale media pages and instead evicts active executable pages from running processes, leading to severe direct reclaim latency spikes.

Architecturally, Linux VFS supports filesystem-specific fadvise delegation via the .fadvise hook in struct file_operations. Implement fuse_file_fadvise() in fs/fuse/file.c and register it in fuse_file_operations. When passthrough is active on a fuse_file, fuse_file_fadvise() forwards the vfs_fadvise() call to the lower backing_file under the opened credentials, allowing POSIX_FADV_DONTNEED to cleanly invalidate the physical backing filesystem pagecache.

Cc Miklos Szeredi <[email protected]>
Cc Amir Goldstein <[email protected]>
Cc Bernd Schubert <[email protected]>
Cc Sandeep Dhavale <[email protected]>
Cc Akilesh Kailash <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

---
v2:
- Remove redundant !backing_file check in fuse_passthrough_fadvise() (Amir Goldstein)
- Remove unused fuse_passthrough_fadvise() stub under #ifndef CONFIG_FUSE_PASSTHROUGH (Amir Goldstein)
- Reorder fuse_passthrough_fadvise() above fuse_passthrough_open() alongside other passthrough ops (Amir Goldstein)
- Add [email protected] to CC list (Amir Goldstein, TJ Mercier)

 fs/fuse/file.c        | 11 +++++++++++
 fs/fuse/fuse_i.h      |  1 +
 fs/fuse/passthrough.c | 13 +++++++++++++
 3 files changed, 25 insertions(+)
fs/fuse/file.c +11 -0
index a4d736945..81709ab82 100644
@@ -3537,6 +3537,16 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
 	return ret;
 }
 
+static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
+{
+	struct fuse_file *ff = file->private_data;
+
+	if (fuse_file_passthrough(ff))
+		return fuse_passthrough_fadvise(ff, offset, len, advice);
+
+	return generic_fadvise(file, offset, len, advice);
+}
+
 static const struct file_operations fuse_file_operations = {
 	.llseek		= fuse_file_llseek,
 	.read_iter	= fuse_file_read_iter,
@@ -3557,6 +3567,7 @@ static const struct file_operations fuse_file_operations = {
 	.fallocate	= fuse_file_fallocate,
 	.copy_file_range = fuse_copy_file_range,
 	.fop_flags	= FOP_DONTCACHE,
+	.fadvise	= fuse_file_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/fuse_i.h +1 -0
index 939553b34..6c60716e7 100644
@@ -1581,6 +1581,7 @@ ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 				      struct file *out, loff_t *ppos,
 				      size_t len, unsigned int flags);
 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice);
 
 /* backing.c */
 
fs/fuse/passthrough.c +13 -0
index 059e57969..8a8fa6b8c 100644
@@ -160,6 +160,19 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
 	return backing_file_mmap(backing_file, vma, &ctx);
 }
 
+int fuse_passthrough_fadvise(struct fuse_file *ff, loff_t offset, loff_t len, int advice)
+{
+	struct file *backing_file = fuse_file_passthrough(ff);
+	const struct cred *old_cred;
+	int ret;
+
+	old_cred = override_creds(ff->cred);
+	ret = vfs_fadvise(backing_file, offset, len, advice);
+	revert_creds(old_cred);
+
+	return ret;
+}
+
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
 	if (fb && refcount_inc_not_zero(&fb->count))
Signature · 1 line
2.43.0