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 3/3] selftests/cachestat: add an overlayfs test case
Date: Tue, 23 Jun 2026 13:14:50 +0200	[thread overview]
Message-ID: <20260623111533.2285005-4-ptikhomirov@virtuozzo.com> (raw)
In-Reply-To: <20260623111533.2285005-1-ptikhomirov@virtuozzo.com>

Mount an overlayfs, create and write a file in the merged directory, and
run cachestat() on it, reusing the existing test_cachestat() helper.

Also bump NR_TESTS to the actual number of tests run: it was 9 while
ten tests were already executed, and this adds an eleventh.

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
---
 .../selftests/cachestat/test_cachestat.c      | 75 ++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
index 542cd09cb4434..1662d9817c50b 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -4,21 +4,25 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdlib.h>
 #include <linux/kernel.h>
 #include <linux/magic.h>
 #include <linux/mman.h>
 #include <sys/mman.h>
+#include <sys/mount.h>
 #include <sys/shm.h>
+#include <sys/stat.h>
 #include <sys/syscall.h>
 #include <sys/vfs.h>
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
 
 #include "kselftest.h"
 
-#define NR_TESTS	9
+#define NR_TESTS	11
 
 static const char * const dev_files[] = {
 	"/dev/zero", "/dev/null", "/dev/urandom",
@@ -294,6 +298,62 @@ bool run_cachestat_test(enum file_type type)
 	return ret;
 }
 
+/*
+ * Set up an overlayfs mount and run cachestat on a freshly created file in the
+ * merged directory. Overlayfs forwards data I/O to the underlying (upper)
+ * inode, so the page cache lives there and not in the overlay inode's mapping.
+ * This is a regression test for cachestat returning all zeroes on overlayfs.
+ */
+static int run_cachestat_overlayfs_test(void)
+{
+	char tmpl[] = "/tmp/cachestat_ovl.XXXXXX";
+	char lower[PATH_MAX], upper[PATH_MAX], work[PATH_MAX];
+	char merged[PATH_MAX], opts[4 * PATH_MAX], file[PATH_MAX];
+	char *base;
+	int ret;
+
+	base = mkdtemp(tmpl);
+	if (!base) {
+		ksft_print_msg("Unable to create overlayfs base dir: %s\n",
+			       strerror(errno));
+		return KSFT_FAIL;
+	}
+
+	snprintf(lower, sizeof(lower), "%s/lower", base);
+	snprintf(upper, sizeof(upper), "%s/upper", base);
+	snprintf(work, sizeof(work), "%s/work", base);
+	snprintf(merged, sizeof(merged), "%s/merged", base);
+
+	if (mkdir(lower, 0755) || mkdir(upper, 0755) ||
+	    mkdir(work, 0755) || mkdir(merged, 0755)) {
+		ksft_print_msg("Unable to create overlayfs dirs: %s\n",
+			       strerror(errno));
+		ret = KSFT_FAIL;
+		goto cleanup;
+	}
+
+	snprintf(opts, sizeof(opts), "lowerdir=%s,upperdir=%s,workdir=%s",
+		 lower, upper, work);
+
+	if (mount("overlay", merged, "overlay", 0, opts)) {
+		ksft_print_msg("Unable to mount overlayfs (need root?): %s\n",
+			       strerror(errno));
+		ret = KSFT_SKIP;
+		goto cleanup;
+	}
+
+	snprintf(file, sizeof(file), "%s/merged/cachestat", base);
+	ret = test_cachestat(file, true, true, false, 4, O_CREAT | O_RDWR, 0600);
+
+	umount(merged);
+cleanup:
+	/* Best-effort recursive cleanup of the temporary tree. */
+	snprintf(opts, sizeof(opts), "rm -rf %s", base);
+	if (system(opts))
+		ksft_print_msg("Unable to clean up %s\n", base);
+	return ret;
+}
+
 int main(void)
 {
 	int ret;
@@ -361,5 +421,18 @@ int main(void)
 		ksft_test_result_fail("cachestat fails with a mmap file\n");
 		ret = 1;
 	}
+
+	switch (run_cachestat_overlayfs_test()) {
+	case KSFT_FAIL:
+		ksft_test_result_fail("cachestat fails with an overlayfs file\n");
+		ret = 1;
+		break;
+	case KSFT_PASS:
+		ksft_test_result_pass("cachestat works with an overlayfs file\n");
+		break;
+	case KSFT_SKIP:
+		ksft_test_result_skip("overlayfs not available\n");
+		break;
+	}
 	return ret;
 }
-- 
2.54.0


  parent reply	other threads:[~2026-06-23 11:16 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 ` [PATCH 1/3] fs, mm: add ->cachestat() file operation Pavel Tikhomirov
2026-06-23 13:48   ` 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 ` Pavel Tikhomirov [this message]
2026-06-23 16:13   ` [PATCH 3/3] selftests/cachestat: add an overlayfs test case 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-4-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®