From: "Pratyush Yadav (Google)" <[email protected]>

Currently memfd preservation using LUO is only tested indirectly via the
luo_multi_session or luo_kexec_simple tests. Their main purpose is to
test other live update functionality.

Add a framework for writing memfd tests. The framework hooks into the
kselftest harness, but adds some things on top to make it suitable for
live update.

The LUO FD (/dev/liveupdate) can only be opened by one process at a
time. Each test runs in its own process. This means the LUO FD must be
owned by the main process and shared to children. main() opens the LUO
FD and shares it to child runners using a global variable.

Live update tests run in two stages. One before kexec and one after.
Detect the stage using a special state session. If the session is
present, it means the test is in post-kexec state.

Additionally, take in an optional --stage argument that lets callers
specify expected stage. This is useful as a safety net to catch LUO core
failures. If LUO core fails to preserve the state session properly, this
option can help detect this and fail early. Since the option is not
recognized by the kselftest harness, remove it from argv before calling
test_harness_run().

Signed-off-by: Pratyush Yadav (Google) <[email protected]>
---
 tools/testing/selftests/liveupdate/Makefile   |  1 +
 .../testing/selftests/liveupdate/luo_memfd.c  | 77 +++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.c

diff --git a/tools/testing/selftests/liveupdate/Makefile 
b/tools/testing/selftests/liveupdate/Makefile
index 080754787ede..051daae55eec 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -6,6 +6,7 @@ TEST_GEN_PROGS += liveupdate
 
 TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
 TEST_GEN_PROGS_EXTENDED += luo_multi_session
+TEST_GEN_PROGS_EXTENDED += luo_memfd
 
 TEST_FILES += do_kexec.sh
 
diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c 
b/tools/testing/selftests/liveupdate/luo_memfd.c
new file mode 100644
index 000000000000..b779eee18387
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2026, Google LLC.
+ * Pratyush Yadav (Google) <[email protected]>
+ */
+
+/*
+ * Selftests for memfd preservation via LUO.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <linux/liveupdate.h>
+
+#include "../kselftest.h"
+#include "../kselftest_harness.h"
+
+#include "luo_test_utils.h"
+
+#define STATE_SESSION_NAME "luo-state"
+#define STATE_MEMFD_TOKEN 1
+
+#define LIVEUPDATE_DEV "/dev/liveupdate"
+static int luo_fd = -1, stage;
+
+int main(int argc, char *argv[])
+{
+       int session, expected_stage = 0;
+
+       /*
+        * The test takes an optional --stage argument. This lets callers
+        * provide the expected stage, and if that doesn't match the test errors
+        * out.
+        *
+        * Look for the stage. Since test_harness_run() doesn't recognize it,
+        * once found, remove it from argv.
+        */
+       for (int i = 1; i < argc; i++) {
+               if (strcmp(argv[i], "--stage") == 0) {
+                       if (i + 1 < argc) {
+                               expected_stage = atoi(argv[i + 1]);
+                               memmove(&argv[i], &argv[i + 2], (argc - i - 1) 
* sizeof(char *));
+                               argc -= 2;
+                               i--;
+                       } else {
+                               ksft_exit_fail_msg("Option --stage requires an 
argument\n");
+                       }
+               }
+       }
+
+       luo_fd = luo_open_device();
+       if (luo_fd < 0)
+               ksft_exit_skip("Failed to open %s (%s). Is the luo module 
loaded?\n",
+                              strerror(errno), LUO_DEVICE);
+
+       session = luo_retrieve_session(luo_fd, STATE_SESSION_NAME);
+       if (session == -ENOENT)
+               stage = 1;
+       else if (session >= 0)
+               stage = 2;
+       else
+               fail_exit("Failed to check for state session");
+
+       if (expected_stage && expected_stage != stage)
+               ksft_exit_fail_msg("Stage mismatch: expected %d, got %d\n",
+                                  expected_stage, stage);
+
+       if (stage == 1)
+               create_state_file(luo_fd, STATE_SESSION_NAME, 
STATE_MEMFD_TOKEN, 2);
+
+       test_harness_run(argc, argv);
+}
-- 
2.53.0.473.g4a7958ca14-goog


Reply via email to