[PATCH v2 3/6] selftests/liveupdate: add test for memfd content preservation

Pratyush Yadav
2026-04-03T19:40:33Z

Add a selftest that makes sure the contents of a memfd are correctly
preserved across a live update. In stage 1, create a memfd and fill it
with random data, and preserve it. Save the random data to the file
system. This will be used by stage 2 to verify the contents are correct.

In stage 2, retrieve the memfd and compare its contents with the
contents saved on the file system.

Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
tools/testing/selftests/liveupdate/Makefile | 1 +
.../testing/selftests/liveupdate/luo_memfd.c | 80 +++++++++++++++++++
2 files changed, 81 insertions(+)

diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index 051daae55eec..8e2eb6500c07 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -13,6 +13,7 @@ TEST_FILES += do_kexec.sh
include ../lib.mk

CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -I$(top_srcdir)/tools/include
CFLAGS += -Wall -O2 -Wno-unused-function
CFLAGS += -MD

diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing/selftests/liveupdate/luo_memfd.c
index 47d2c3163b62..70eb96950f04 100644
--- a/tools/testing/selftests/liveupdate/luo_memfd.c
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -3,6 +3,9 @@
/*
* Copyright (c) 2026, Google LLC.
* Pratyush Yadav (Google) <pratyush@kernel.org>
+ *
+ * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
+ * Pratyush Yadav <ptyadav@amazon.de>
*/

/*
@@ -11,11 +14,14 @@

#include <errno.h>
#include <fcntl.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
+#include <sys/mman.h>
#include <unistd.h>

#include <linux/liveupdate.h>
+#include <linux/sizes.h>

#include "../kselftest.h"
#include "../kselftest_harness.h"
@@ -25,9 +31,83 @@
#define STATE_SESSION_NAME "luo-state"
#define STATE_MEMFD_TOKEN 1

+#define MEMFD_DATA_SESSION_NAME "memfd_data_session"
+#define MEMFD_DATA_TOKEN 1
+#define MEMFD_DATA_BUFFER_SIZE SZ_1M
+#define MEMFD_DATA_FS_COPY "memfd_data_fs_copy.bin"
+
static int luo_fd = -1;
static int stage;

+static void memfd_data_stage_1(struct __test_metadata *_metadata)
+{
+ int fd, session;
+ char *buffer;
+ struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+
+ buffer = malloc(MEMFD_DATA_BUFFER_SIZE);
+ ASSERT_NE(buffer, NULL);
+
+ session = luo_create_session(luo_fd, MEMFD_DATA_SESSION_NAME);
+ ASSERT_GE(session, 0);
+
+ fd = create_random_memfd("memfd_data", buffer, MEMFD_DATA_BUFFER_SIZE);
+ ASSERT_GE(fd, 0);
+
+ ASSERT_EQ(save_test_data(MEMFD_DATA_FS_COPY, buffer, MEMFD_DATA_BUFFER_SIZE), 0);
+
+ preserve_arg.fd = fd;
+ preserve_arg.token = MEMFD_DATA_TOKEN;
+ ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), 0);
+
+ daemonize_and_wait();
+}
+
+static void memfd_data_stage_2(struct __test_metadata *_metadata)
+{
+ int fd, session;
+ char *buffer;
+ struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+ buffer = malloc(MEMFD_DATA_BUFFER_SIZE);
+ ASSERT_NE(buffer, NULL);
+
+ session = luo_retrieve_session(luo_fd, MEMFD_DATA_SESSION_NAME);
+ ASSERT_GE(session, 0);
+
+ ASSERT_EQ(load_test_data(MEMFD_DATA_FS_COPY, buffer, MEMFD_DATA_BUFFER_SIZE), 0);
+
+ retrieve_arg.token = MEMFD_DATA_TOKEN;
+ ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), 0);
+ fd = retrieve_arg.fd;
+ ASSERT_GE(fd, 0);
+
+ ASSERT_EQ(verify_fd_content_read(fd, buffer, MEMFD_DATA_BUFFER_SIZE), 0);
+
+ ASSERT_EQ(luo_session_finish(session), 0);
+}
+
+/*
+ * Test that a memfd with its data is preserved across live update.
+ */
+TEST(memfd_data)
+{
+ if (cwd_is_tmpfs())
+ SKIP(return, "test saves data to rootfs, cannot run on tmpfs");
+
+ switch (stage) {
+ case 1:
+ memfd_data_stage_1(_metadata);
+ break;
+ case 2:
+ memfd_data_stage_2(_metadata);
+ break;
+ default:
+ TH_LOG("Unknown stage %d\n", stage);
+ ASSERT_FALSE(true);
+ }
+}
+
int main(int argc, char *argv[])
{
int expected_stage = 0;
--
2.53.0.1213.gd9a14994de-goog