mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
To: Miklos Szeredi <miklos@szeredi.hu>, Amir Goldstein <amir73il@gmail.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Nhat Pham <nphamcs@gmail.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Shuah Khan <shuah@kernel.org>,
	linux-unionfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-kselftest@vger.kernel.org,
	Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Subject: [PATCH 1/3] fs, mm: add ->cachestat() file operation
Date: Tue, 23 Jun 2026 13:14:48 +0200	[thread overview]
Message-ID: <20260623111533.2285005-2-ptikhomirov@virtuozzo.com> (raw)
In-Reply-To: <20260623111533.2285005-1-ptikhomirov@virtuozzo.com>

The cachestat() syscall reads page cache statistics straight from the
file's f_mapping. Stackable filesystems such as overlayfs keep the data
pages in an underlying inode's mapping rather than in the overlay
inode's, so cachestat() reports all zeroes for them.

Add a ->cachestat() file operation and route the syscall through a new
vfs_cachestat() helper that calls it when present, falling back to
file's f_mapping otherwise. This lets stackable filesystems forward the
query to the file that actually owns the page cache. No behaviour change
for regular files.

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
Note: Memset change might be a bit tricky, I moved it to no
->cachestat() path to avoid multiple memset on nested overlayfs, that
means that ->cachestat() is expected to be able to handle unitialized
cs.
---
 include/linux/fs.h | 10 ++++++++++
 mm/filemap.c       | 43 +++++++++++++++++++++++++++++++++++--------
 2 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 6da44573ce450..966b6564707e4 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -53,6 +53,8 @@
 
 struct bdi_writeback;
 struct bio;
+struct cachestat_range;
+struct cachestat;
 struct io_comp_batch;
 struct fiemap_extent_info;
 struct kiocb;
@@ -1963,6 +1965,8 @@ struct file_operations {
 				   struct file *file_out, loff_t pos_out,
 				   loff_t len, unsigned int remap_flags);
 	int (*fadvise)(struct file *, loff_t, loff_t, int);
+	int (*cachestat)(struct file *file, struct cachestat_range *csr,
+			 struct cachestat *cs);
 	int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
 	int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
 				unsigned int poll_flags);
@@ -3633,6 +3637,12 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
 extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
 			   int advice);
 
+/* mm/filemap.c */
+#ifdef CONFIG_CACHESTAT_SYSCALL
+int vfs_cachestat(struct file *file, struct cachestat_range *csr,
+		  struct cachestat *cs);
+#endif
+
 static inline bool vfs_empty_path(int dfd, const char __user *path)
 {
 	char c;
diff --git a/mm/filemap.c b/mm/filemap.c
index 7e467c81d2138..90608c6b1ce55 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -4714,6 +4714,37 @@ static inline bool can_do_cachestat(struct file *f)
 	return file_permission(f, MAY_WRITE) == 0;
 }
 
+/**
+ * vfs_cachestat() - query page cache statistics of a file
+ * @file:	file to query
+ * @csr:	byte range to query
+ * @cs:		output statistics
+ *
+ * Compute the page cache statistics for the given byte range of @file.
+ *
+ * Stackable filesystems (e.g. overlayfs) keep the data pages in the
+ * mapping of an underlying file rather than in @file->f_mapping. Such
+ * filesystems provide a ->cachestat() file operation that forwards the
+ * query to the file that actually owns the page cache; otherwise the
+ * statistics are computed from @file->f_mapping directly.
+ */
+int vfs_cachestat(struct file *file, struct cachestat_range *csr,
+		  struct cachestat *cs)
+{
+	pgoff_t first_index, last_index;
+
+	if (file->f_op->cachestat)
+		return file->f_op->cachestat(file, csr, cs);
+
+	first_index = csr->off >> PAGE_SHIFT;
+	last_index =
+		csr->len == 0 ? ULONG_MAX : (csr->off + csr->len - 1) >> PAGE_SHIFT;
+	memset(cs, 0, sizeof(struct cachestat));
+	filemap_cachestat(file->f_mapping, first_index, last_index, cs);
+	return 0;
+}
+EXPORT_SYMBOL(vfs_cachestat);
+
 /*
  * The cachestat(2) system call.
  *
@@ -4753,10 +4784,9 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
 		struct cachestat __user *, cstat, unsigned int, flags)
 {
 	CLASS(fd, f)(fd);
-	struct address_space *mapping;
 	struct cachestat_range csr;
 	struct cachestat cs;
-	pgoff_t first_index, last_index;
+	int ret;
 
 	if (fd_empty(f))
 		return -EBADF;
@@ -4775,12 +4805,9 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
 	if (flags != 0)
 		return -EINVAL;
 
-	first_index = csr.off >> PAGE_SHIFT;
-	last_index =
-		csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
-	memset(&cs, 0, sizeof(struct cachestat));
-	mapping = fd_file(f)->f_mapping;
-	filemap_cachestat(mapping, first_index, last_index, &cs);
+	ret = vfs_cachestat(fd_file(f), &csr, &cs);
+	if (ret)
+		return ret;
 
 	if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
 		return -EFAULT;
-- 
2.54.0


  reply	other threads:[~2026-06-23 11:15 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 11:14 [PATCH 0/3] ovl: make cachestat() work Pavel Tikhomirov
2026-06-23 11:14 ` Pavel Tikhomirov [this message]
2026-06-23 13:48   ` [PATCH 1/3] fs, mm: add ->cachestat() file operation Johannes Weiner
2026-06-23 14:55     ` Pavel Tikhomirov
2026-06-23 15:34       ` Amir Goldstein
2026-06-24 11:59         ` Pavel Tikhomirov
2026-06-24 14:11           ` Amir Goldstein
2026-06-25 10:36         ` Christian Brauner
2026-06-26 15:18           ` Amir Goldstein
2026-06-30  7:55             ` Christian Brauner
2026-06-30  9:45               ` Pavel Tikhomirov
2026-06-30 10:56               ` Amir Goldstein
2026-06-23 16:01   ` Nhat Pham
2026-06-24 11:46     ` Pavel Tikhomirov
2026-06-23 11:14 ` [PATCH 2/3] ovl: support cachestat() syscall on overlayfs files Pavel Tikhomirov
2026-06-23 17:12   ` Nhat Pham
2026-06-24 11:45     ` Pavel Tikhomirov
2026-06-24 14:16       ` Amir Goldstein
2026-06-24 19:06         ` Nhat Pham
2026-06-25 12:53           ` Matthew Wilcox
2026-06-25 16:12             ` Nhat Pham
2026-06-23 11:14 ` [PATCH 3/3] selftests/cachestat: add an overlayfs test case Pavel Tikhomirov
2026-06-23 16:13   ` Nhat Pham
2026-06-24 12:22     ` Pavel Tikhomirov
2026-06-24 20:50 ` [PATCH 0/3] ovl: make cachestat() work Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623111533.2285005-2-ptikhomirov@virtuozzo.com \
    --to=ptikhomirov@virtuozzo.com \
    --cc=akpm@linux-foundation.org \
    --cc=amir73il@gmail.com \
    --cc=brauner@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-unionfs@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=nphamcs@gmail.com \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®