[PATCH v4 19/19] selftests/liveupdate: Test block accounting of retrieved tmpfs files

From: David Matlack

Date: Tue Nov 10 2026 - 19:29:56 EST


Add test cases to luo_tmpfs for how the blocks of a retrieved file are
charged: The file fits a mount of exactly its size, and a large folio
that extends past the end of the file is charged in full. Running out of
space on the mount, or hitting the memcg limit, fails permanently,
leaves the target empty and nothing charged to the mount or the memcg,
and the session can still be finished.

Enable CONFIG_TRANSPARENT_HUGEPAGE for the large folio test cases, and
CONFIG_CGROUPS and CONFIG_MEMCG for the memcg test case. They are
skipped without them.

tmpfs quotas are not tested.

Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
tools/testing/selftests/liveupdate/config | 3 +
.../testing/selftests/liveupdate/luo_tmpfs.c | 278 +++++++++++++++++-
2 files changed, 279 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config
index 37230f4c488f..7f38291c979d 100644
--- a/tools/testing/selftests/liveupdate/config
+++ b/tools/testing/selftests/liveupdate/config
@@ -1,4 +1,5 @@
CONFIG_BLK_DEV_INITRD=y
+CONFIG_CGROUPS=y
CONFIG_DEVTMPFS=y
CONFIG_KEXEC_FILE=y
CONFIG_KEXEC_HANDOVER=y
@@ -7,7 +8,9 @@ CONFIG_KEXEC_HANDOVER_DEBUGFS=y
CONFIG_KEXEC_HANDOVER_DEBUG=y
CONFIG_LIVEUPDATE=y
CONFIG_LIVEUPDATE_TEST=y
+CONFIG_MEMCG=y
CONFIG_MEMFD_CREATE=y
CONFIG_TMPFS=y
CONFIG_TMPFS_XATTR=y
CONFIG_SHMEM=y
+CONFIG_TRANSPARENT_HUGEPAGE=y
diff --git a/tools/testing/selftests/liveupdate/luo_tmpfs.c b/tools/testing/selftests/liveupdate/luo_tmpfs.c
index 0bd9b95f0575..783c46715fc1 100644
--- a/tools/testing/selftests/liveupdate/luo_tmpfs.c
+++ b/tools/testing/selftests/liveupdate/luo_tmpfs.c
@@ -62,8 +62,9 @@

/*
* Everything happens on a tmpfs mounted at TEST_DIR. In stage 1, the source
- * files are created on the tmpfs mounted at TEST_DIR/src. In stage 2, each
- * case runs in the directory TEST_DIR/<name of the case>.
+ * files are created on the tmpfs mounted at TEST_DIR/src, or at
+ * TEST_DIR/src_huge for files that need large folios. In stage 2, each case
+ * runs in the directory TEST_DIR/<name of the case>.
*/
#define TEST_DIR "/luo_tmpfs"

@@ -145,6 +146,7 @@ struct test_case {
static unsigned long page_size;
static int dev_fd;
static int session_fd;
+static bool have_huge_src;

/* nolibc doesn't have these, so use syscall() where needed. */
static int do_fcntl(int fd, int cmd, long arg)
@@ -209,6 +211,16 @@ static long mnt_used(const char *path)
return buf.f_blocks - buf.f_bfree;
}

+static long mnt_free(const char *path)
+{
+ struct statfs buf;
+
+ if (do_statfs(path, &buf))
+ return -1;
+
+ return buf.f_bfree;
+}
+
/* Mount a tmpfs with room for @pages pages at @dir. */
static int mount_tmpfs(const char *dir, unsigned long pages, const char *opts)
{
@@ -244,6 +256,26 @@ static long read_long(const char *path)
return strtol(buf, NULL, 10);
}

+/* Write @str to the file at @path. Return 0 or -errno. */
+static int write_file(const char *path, const char *str)
+{
+ ssize_t len = strlen(str), ret;
+ int fd;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ return -errno;
+
+ ret = write(fd, str, len);
+ if (ret != len)
+ ret = ret < 0 ? -errno : -EIO;
+ else
+ ret = 0;
+
+ close(fd);
+ return ret;
+}
+
/* Retrieve with the argument @arg. Return the retrieved fd or -errno. */
static int retrieve_arg(__u64 token, struct liveupdate_shmem_retrieve_arg *arg)
{
@@ -451,6 +483,33 @@ static int preserve_memfd(struct test_case *tc, unsigned int flags,
return ret;
}

+/* Preserve a file from the tmpfs mounted with huge=always. */
+static int preserve_huge_file(struct test_case *tc)
+{
+ char path[128];
+ int fd, ret;
+
+ if (!have_huge_src)
+ return KSFT_SKIP;
+
+ snprintf(path, sizeof(path), "src_huge/%s", tc->name);
+ fd = create_source(path, tc->size, tc->token);
+ CHECK(fd >= 0);
+
+ /* The test needs the last, partial page to be in a large folio. */
+ if (tc->size % page_size && file_pages(fd) <= nr_pages(tc->size)) {
+ ksft_print_msg("%s: No large folio for the source file\n",
+ tc->name);
+ close(fd);
+ return KSFT_SKIP;
+ }
+
+ ret = preserve_fd(fd, tc->pos, tc->token);
+ close(fd);
+
+ return ret;
+}
+
/* Skip a case whose source file couldn't be preserved in stage 1. */
static int skip_not_preserved(struct test_case *tc)
{
@@ -1305,6 +1364,181 @@ static int exec_after_finish(struct test_case *tc)
return KSFT_PASS;
}

+static int mount_fits_verify(struct test_case *tc)
+{
+ unsigned long pages = nr_pages(tc->size);
+ int fd;
+
+ /* The file can be retrieved into a mount it fits exactly. */
+ CHECK_EQ(mount_tmpfs("fit", pages, NULL), 0);
+ fd = retrieve_new(tc->token, "fit");
+ CHECK(fd >= 0);
+ TRY(check_restored(tc, fd));
+ CHECK_EQ(mnt_used("fit"), pages);
+ CHECK_EQ(mnt_free("fit"), 0);
+
+ CHECK_EQ(ftruncate(fd, 0), 0);
+ CHECK_EQ(mnt_used("fit"), 0);
+
+ close(fd);
+ return KSFT_PASS;
+}
+
+static int mount_too_small_verify(struct test_case *tc)
+{
+ unsigned long pages = nr_pages(tc->size);
+ int target;
+
+ /* The failure leaves the target empty, and the mount untouched... */
+ CHECK_EQ(mount_tmpfs("small", pages - 1, NULL), 0);
+ target = new_target("small");
+ CHECK(target >= 0);
+ CHECK_EQ(retrieve_into(tc->token, target), -ENOSPC);
+ CHECK_EQ(file_size(target), 0);
+ CHECK_EQ(file_pages(target), 0);
+ CHECK_EQ(mnt_used("small"), 0);
+ close(target);
+
+ /*
+ * ... but the preserved contents were restored before it was detected,
+ * so the failure is permanent. Finishing the session frees them.
+ */
+ CHECK_EQ(mount_tmpfs("big", pages, NULL), 0);
+ CHECK_EQ(retrieve_new(tc->token, "big"), -ENOSPC);
+ CHECK_EQ(luo_session_retrieve_fd(session_fd, tc->token), -ENOSPC);
+ CHECK_EQ(mnt_used("big"), 0);
+
+ return KSFT_PASS;
+}
+
+static int huge_folios_verify(struct test_case *tc)
+{
+ unsigned long pages = nr_pages(tc->size);
+ int fd;
+
+ CHECK_EQ(mount_tmpfs("dst", pages, "huge=never"), 0);
+ fd = retrieve_new(tc->token, "dst");
+ if (fd == -ENOENT)
+ return skip_not_preserved(tc);
+ CHECK(fd >= 0);
+ TRY(check_restored(tc, fd));
+ CHECK_EQ(mnt_used("dst"), pages);
+
+ /* LUO keeps a reference to the file until the session is finished. */
+ close(fd);
+ CHECK_EQ(mnt_used("dst"), pages);
+
+ return KSFT_PASS;
+}
+
+static int huge_folios_after_finish(struct test_case *tc)
+{
+ CHECK_EQ(mnt_used("dst"), 0);
+ return KSFT_PASS;
+}
+
+static int huge_folio_tail_verify(struct test_case *tc)
+{
+ long pages;
+ int fd;
+
+ CHECK_EQ(mount_tmpfs("dst", 4096, "huge=never"), 0);
+ fd = retrieve_new(tc->token, "dst");
+ if (fd == -ENOENT)
+ return skip_not_preserved(tc);
+ CHECK(fd >= 0);
+ CHECK_EQ(file_size(fd), tc->size);
+ TRY(check_contents(fd, tc->size, tc->token));
+
+ /* The whole folio is charged, including the part past EOF. */
+ pages = file_pages(fd);
+ CHECK(pages > 1);
+ CHECK_EQ(mnt_used("dst"), pages);
+
+ CHECK_EQ(ftruncate(fd, 0), 0);
+ CHECK_EQ(mnt_used("dst"), 0);
+
+ close(fd);
+ return KSFT_PASS;
+}
+
+static int huge_folio_tail_enospc_verify(struct test_case *tc)
+{
+ int ret;
+
+ /* There is room for the file, but not for its last folio. */
+ CHECK_EQ(mount_tmpfs("small", 1, "huge=never"), 0);
+ ret = retrieve_new(tc->token, "small");
+ if (ret == -ENOENT)
+ return skip_not_preserved(tc);
+ CHECK_EQ(ret, -ENOSPC);
+ CHECK_EQ(mnt_used("small"), 0);
+
+ return KSFT_PASS;
+}
+
+static int memcg_limit_child(struct test_case *tc)
+{
+ long limit = tc->size / 2, usage;
+ int ret;
+
+ /*
+ * Charging small folios may invoke the memcg OOM killer. Protect the
+ * child, so that the retrieve fails with -ENOMEM instead. Charging a
+ * large folio doesn't invoke it.
+ */
+ CHECK_EQ(write_file("/proc/self/oom_score_adj", "-1000"), 0);
+ CHECK_EQ(write_file("memcg/small/cgroup.procs", "0"), 0);
+
+ /* The folios are charged to the memcg of the caller, which is full. */
+ ret = retrieve_new(tc->token, "dst");
+ if (ret == -ENOENT)
+ return skip_not_preserved(tc);
+ CHECK_EQ(ret, -ENOMEM);
+
+ /* The failure leaves the mount and the memcg untouched. */
+ CHECK_EQ(mnt_used("dst"), 0);
+ usage = read_long("memcg/small/memory.current");
+ CHECK(usage >= 0 && usage < limit);
+
+ return KSFT_PASS;
+}
+
+static int memcg_limit_verify(struct test_case *tc)
+{
+ unsigned long pages = nr_pages(tc->size);
+ char limit[32];
+ int ret;
+
+ CHECK_EQ(mkdir("memcg", 0755), 0);
+ if (do_mount("cgroup2", "memcg", "cgroup2", 0, NULL)) {
+ ksft_print_msg("%s: cgroup2 is not available\n", tc->name);
+ return KSFT_SKIP;
+ }
+ if (write_file("memcg/cgroup.subtree_control", "+memory")) {
+ ksft_print_msg("%s: The memory controller is not available\n",
+ tc->name);
+ return KSFT_SKIP;
+ }
+
+ /* A memcg without room for the file. */
+ CHECK_EQ(mkdir("memcg/small", 0755), 0);
+ snprintf(limit, sizeof(limit), "%zu", tc->size * 3 / 4);
+ CHECK_EQ(write_file("memcg/small/memory.max", limit), 0);
+ CHECK_EQ(mount_tmpfs("dst", pages, "huge=never"), 0);
+
+ ret = run_child(memcg_limit_child, tc);
+ rmdir("memcg/small");
+ if (ret != KSFT_PASS)
+ return ret;
+
+ /* The preserved contents were restored, so the failure is permanent. */
+ CHECK_EQ(retrieve_new(tc->token, "dst"), -ENOMEM);
+ CHECK_EQ(mnt_used("dst"), 0);
+
+ return KSFT_PASS;
+}
+
static int memfd_preserve(struct test_case *tc)
{
return preserve_memfd(tc, 0, 0);
@@ -1629,6 +1863,42 @@ static struct test_case test_cases[] = {
.verify = exec_verify,
.after_finish = exec_after_finish,
},
+ /* Block accounting. */
+ {
+ .name = "mount_fits",
+ .size = 2 << 20,
+ .verify = mount_fits_verify,
+ },
+ {
+ .name = "mount_too_small",
+ .size = 2 << 20,
+ .verify = mount_too_small_verify,
+ },
+ {
+ .name = "huge_folios",
+ .size = 4 << 20,
+ .preserve = preserve_huge_file,
+ .verify = huge_folios_verify,
+ .after_finish = huge_folios_after_finish,
+ },
+ {
+ .name = "huge_folio_tail",
+ .size = 100,
+ .preserve = preserve_huge_file,
+ .verify = huge_folio_tail_verify,
+ },
+ {
+ .name = "huge_folio_tail_enospc",
+ .size = 100,
+ .preserve = preserve_huge_file,
+ .verify = huge_folio_tail_enospc_verify,
+ },
+ {
+ .name = "memcg_limit",
+ .size = 4 << 20,
+ .preserve = preserve_huge_file,
+ .verify = memcg_limit_verify,
+ },
/* memfds. */
{
.name = "memfd",
@@ -1733,6 +2003,10 @@ static void run_stage_1(int luo_fd)
if (chdir(TEST_DIR) || mount_tmpfs("src", 4096, "huge=never"))
fail_exit("Failed to mount the source tmpfs");

+ have_huge_src = !mount_tmpfs("src_huge", 4096, "huge=always");
+ if (!have_huge_src)
+ ksft_print_msg("[STAGE 1] huge=always is not supported\n");
+
session_fd = luo_create_session(luo_fd, SESSION_NAME);
if (session_fd < 0)
fail_exit("luo_create_session for '%s'", SESSION_NAME);
--
2.56.0.542.g3ef89b7020-goog