Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/liveupdate: add simple kexec-based selftest for LUO

Introduce a kexec-based selftest, luo_kexec_simple, to validate the
end-to-end lifecycle of a Live Update Orchestrator session across a
reboot.

While existing tests verify the uAPI in a pre-reboot context, this test
ensures that the core functionality—preserving state via Kexec Handover
and restoring it in a new kernel—works as expected.

The test operates in two stages, managing its state across the reboot by
preserving a dedicated "state session" containing a memfd. This mechanism
dogfoods the LUO feature itself for state tracking, making the test
self-contained.

The test validates the following sequence:

Stage 1 (Pre-kexec):
- Creates a test session (test-session).
- Creates and preserves a memfd with a known data pattern into the test
session.
- Creates the state-tracking session to signal progression to Stage 2.
- Executes a kexec reboot via a helper script.

Stage 2 (Post-kexec):
- Retrieves the state-tracking session to confirm it is in the
post-reboot stage.
- Retrieves the preserved test session.
- Restores the memfd from the test session and verifies its contents
match the original data pattern written in Stage 1.
- Finalizes both the test and state sessions to ensure a clean
teardown.

The test relies on a helper script (do_kexec.sh) to perform the reboot and
a shared utility library (luo_test_utils.c) for common LUO operations,
keeping the main test logic clean and focused.

Link: https://lkml.kernel.org/r/20251125165850.3389713-18-pasha.tatashin@soleen.com
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Zhu Yanjun <yanjun.zhu@linux.dev>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Tested-by: David Matlack <dmatlack@google.com>
Cc: Aleksander Lobakin <aleksander.lobakin@intel.com>
Cc: Alexander Graf <graf@amazon.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andriy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: anish kumar <yesanishhere@gmail.com>
Cc: Anna Schumaker <anna.schumaker@oracle.com>
Cc: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Borislav Betkov <bp@alien8.de>
Cc: Chanwoo Choi <cw00.choi@samsung.com>
Cc: Chen Ridong <chenridong@huawei.com>
Cc: Chris Li <chrisl@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Daniel Wagner <wagi@kernel.org>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Jeffery <djeffery@redhat.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Guixin Liu <kanie@linux.alibaba.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ira Weiny <ira.weiny@intel.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Joanthan Cameron <Jonathan.Cameron@huawei.com>
Cc: Joel Granados <joel.granados@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Lennart Poettering <lennart@poettering.net>
Cc: Leon Romanovsky <leon@kernel.org>
Cc: Leon Romanovsky <leonro@nvidia.com>
Cc: Lukas Wunner <lukas@wunner.de>
Cc: Marc Rutland <mark.rutland@arm.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Matthew Maurer <mmaurer@google.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Myugnjoo Ham <myungjoo.ham@samsung.com>
Cc: Parav Pandit <parav@nvidia.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Pratyush Yadav <ptyadav@amazon.de>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Saeed Mahameed <saeedm@nvidia.com>
Cc: Samiullah Khawaja <skhawaja@google.com>
Cc: Song Liu <song@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Stuart Hayes <stuart.w.hayes@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Thomas Weißschuh <linux@weissschuh.net>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: William Tu <witu@nvidia.com>
Cc: Yoann Congal <yoann.congal@smile.fr>
Cc: Zijun Hu <quic_zijuhu@quicinc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Pasha Tatashin and committed by
Andrew Morton
a003bdb9 80bab43f

+421
+6
tools/testing/selftests/liveupdate/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 3 + LIB_C += luo_test_utils.c 4 + 3 5 TEST_GEN_PROGS += liveupdate 6 + 7 + TEST_GEN_PROGS_EXTENDED += luo_kexec_simple 8 + 9 + TEST_FILES += do_kexec.sh 4 10 5 11 include ../lib.mk 6 12
+16
tools/testing/selftests/liveupdate/do_kexec.sh
··· 1 + #!/bin/sh 2 + # SPDX-License-Identifier: GPL-2.0 3 + set -e 4 + 5 + # Use $KERNEL and $INITRAMFS to pass custom Kernel and optional initramfs 6 + 7 + KERNEL="${KERNEL:-/boot/bzImage}" 8 + set -- -l -s --reuse-cmdline "$KERNEL" 9 + 10 + INITRAMFS="${INITRAMFS:-/boot/initramfs}" 11 + if [ -f "$INITRAMFS" ]; then 12 + set -- "$@" --initrd="$INITRAMFS" 13 + fi 14 + 15 + kexec "$@" 16 + kexec -e
+89
tools/testing/selftests/liveupdate/luo_kexec_simple.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + /* 4 + * Copyright (c) 2025, Google LLC. 5 + * Pasha Tatashin <pasha.tatashin@soleen.com> 6 + * 7 + * A simple selftest to validate the end-to-end lifecycle of a LUO session 8 + * across a single kexec reboot. 9 + */ 10 + 11 + #include "luo_test_utils.h" 12 + 13 + #define TEST_SESSION_NAME "test-session" 14 + #define TEST_MEMFD_TOKEN 0x1A 15 + #define TEST_MEMFD_DATA "hello kexec world" 16 + 17 + /* Constants for the state-tracking mechanism, specific to this test file. */ 18 + #define STATE_SESSION_NAME "kexec_simple_state" 19 + #define STATE_MEMFD_TOKEN 999 20 + 21 + /* Stage 1: Executed before the kexec reboot. */ 22 + static void run_stage_1(int luo_fd) 23 + { 24 + int session_fd; 25 + 26 + ksft_print_msg("[STAGE 1] Starting pre-kexec setup...\n"); 27 + 28 + ksft_print_msg("[STAGE 1] Creating state file for next stage (2)...\n"); 29 + create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2); 30 + 31 + ksft_print_msg("[STAGE 1] Creating session '%s' and preserving memfd...\n", 32 + TEST_SESSION_NAME); 33 + session_fd = luo_create_session(luo_fd, TEST_SESSION_NAME); 34 + if (session_fd < 0) 35 + fail_exit("luo_create_session for '%s'", TEST_SESSION_NAME); 36 + 37 + if (create_and_preserve_memfd(session_fd, TEST_MEMFD_TOKEN, 38 + TEST_MEMFD_DATA) < 0) { 39 + fail_exit("create_and_preserve_memfd for token %#x", 40 + TEST_MEMFD_TOKEN); 41 + } 42 + 43 + close(luo_fd); 44 + daemonize_and_wait(); 45 + } 46 + 47 + /* Stage 2: Executed after the kexec reboot. */ 48 + static void run_stage_2(int luo_fd, int state_session_fd) 49 + { 50 + int session_fd, mfd, stage; 51 + 52 + ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n"); 53 + 54 + restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage); 55 + if (stage != 2) 56 + fail_exit("Expected stage 2, but state file contains %d", stage); 57 + 58 + ksft_print_msg("[STAGE 2] Retrieving session '%s'...\n", TEST_SESSION_NAME); 59 + session_fd = luo_retrieve_session(luo_fd, TEST_SESSION_NAME); 60 + if (session_fd < 0) 61 + fail_exit("luo_retrieve_session for '%s'", TEST_SESSION_NAME); 62 + 63 + ksft_print_msg("[STAGE 2] Restoring and verifying memfd (token %#x)...\n", 64 + TEST_MEMFD_TOKEN); 65 + mfd = restore_and_verify_memfd(session_fd, TEST_MEMFD_TOKEN, 66 + TEST_MEMFD_DATA); 67 + if (mfd < 0) 68 + fail_exit("restore_and_verify_memfd for token %#x", TEST_MEMFD_TOKEN); 69 + close(mfd); 70 + 71 + ksft_print_msg("[STAGE 2] Test data verified successfully.\n"); 72 + ksft_print_msg("[STAGE 2] Finalizing test session...\n"); 73 + if (luo_session_finish(session_fd) < 0) 74 + fail_exit("luo_session_finish for test session"); 75 + close(session_fd); 76 + 77 + ksft_print_msg("[STAGE 2] Finalizing state session...\n"); 78 + if (luo_session_finish(state_session_fd) < 0) 79 + fail_exit("luo_session_finish for state session"); 80 + close(state_session_fd); 81 + 82 + ksft_print_msg("\n--- SIMPLE KEXEC TEST PASSED ---\n"); 83 + } 84 + 85 + int main(int argc, char *argv[]) 86 + { 87 + return luo_test(argc, argv, STATE_SESSION_NAME, 88 + run_stage_1, run_stage_2); 89 + }
+266
tools/testing/selftests/liveupdate/luo_test_utils.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + /* 4 + * Copyright (c) 2025, Google LLC. 5 + * Pasha Tatashin <pasha.tatashin@soleen.com> 6 + */ 7 + 8 + #define _GNU_SOURCE 9 + 10 + #include <stdio.h> 11 + #include <stdlib.h> 12 + #include <string.h> 13 + #include <getopt.h> 14 + #include <fcntl.h> 15 + #include <unistd.h> 16 + #include <sys/ioctl.h> 17 + #include <sys/syscall.h> 18 + #include <sys/mman.h> 19 + #include <sys/types.h> 20 + #include <sys/stat.h> 21 + #include <errno.h> 22 + #include <stdarg.h> 23 + 24 + #include "luo_test_utils.h" 25 + 26 + int luo_open_device(void) 27 + { 28 + return open(LUO_DEVICE, O_RDWR); 29 + } 30 + 31 + int luo_create_session(int luo_fd, const char *name) 32 + { 33 + struct liveupdate_ioctl_create_session arg = { .size = sizeof(arg) }; 34 + 35 + snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s", 36 + LIVEUPDATE_SESSION_NAME_LENGTH - 1, name); 37 + 38 + if (ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg) < 0) 39 + return -errno; 40 + 41 + return arg.fd; 42 + } 43 + 44 + int luo_retrieve_session(int luo_fd, const char *name) 45 + { 46 + struct liveupdate_ioctl_retrieve_session arg = { .size = sizeof(arg) }; 47 + 48 + snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s", 49 + LIVEUPDATE_SESSION_NAME_LENGTH - 1, name); 50 + 51 + if (ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg) < 0) 52 + return -errno; 53 + 54 + return arg.fd; 55 + } 56 + 57 + int create_and_preserve_memfd(int session_fd, int token, const char *data) 58 + { 59 + struct liveupdate_session_preserve_fd arg = { .size = sizeof(arg) }; 60 + long page_size = sysconf(_SC_PAGE_SIZE); 61 + void *map = MAP_FAILED; 62 + int mfd = -1, ret = -1; 63 + 64 + mfd = memfd_create("test_mfd", 0); 65 + if (mfd < 0) 66 + return -errno; 67 + 68 + if (ftruncate(mfd, page_size) != 0) 69 + goto out; 70 + 71 + map = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, mfd, 0); 72 + if (map == MAP_FAILED) 73 + goto out; 74 + 75 + snprintf(map, page_size, "%s", data); 76 + munmap(map, page_size); 77 + 78 + arg.fd = mfd; 79 + arg.token = token; 80 + if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0) 81 + goto out; 82 + 83 + ret = 0; 84 + out: 85 + if (ret != 0 && errno != 0) 86 + ret = -errno; 87 + if (mfd >= 0) 88 + close(mfd); 89 + return ret; 90 + } 91 + 92 + int restore_and_verify_memfd(int session_fd, int token, 93 + const char *expected_data) 94 + { 95 + struct liveupdate_session_retrieve_fd arg = { .size = sizeof(arg) }; 96 + long page_size = sysconf(_SC_PAGE_SIZE); 97 + void *map = MAP_FAILED; 98 + int mfd = -1, ret = -1; 99 + 100 + arg.token = token; 101 + if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0) 102 + return -errno; 103 + mfd = arg.fd; 104 + 105 + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, mfd, 0); 106 + if (map == MAP_FAILED) 107 + goto out; 108 + 109 + if (expected_data && strcmp(expected_data, map) != 0) { 110 + ksft_print_msg("Data mismatch! Expected '%s', Got '%s'\n", 111 + expected_data, (char *)map); 112 + ret = -EINVAL; 113 + goto out_munmap; 114 + } 115 + 116 + ret = mfd; 117 + out_munmap: 118 + munmap(map, page_size); 119 + out: 120 + if (ret < 0 && errno != 0) 121 + ret = -errno; 122 + if (ret < 0 && mfd >= 0) 123 + close(mfd); 124 + return ret; 125 + } 126 + 127 + int luo_session_finish(int session_fd) 128 + { 129 + struct liveupdate_session_finish arg = { .size = sizeof(arg) }; 130 + 131 + if (ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, &arg) < 0) 132 + return -errno; 133 + 134 + return 0; 135 + } 136 + 137 + void create_state_file(int luo_fd, const char *session_name, int token, 138 + int next_stage) 139 + { 140 + char buf[32]; 141 + int state_session_fd; 142 + 143 + state_session_fd = luo_create_session(luo_fd, session_name); 144 + if (state_session_fd < 0) 145 + fail_exit("luo_create_session for state tracking"); 146 + 147 + snprintf(buf, sizeof(buf), "%d", next_stage); 148 + if (create_and_preserve_memfd(state_session_fd, token, buf) < 0) 149 + fail_exit("create_and_preserve_memfd for state tracking"); 150 + 151 + /* 152 + * DO NOT close session FD, otherwise it is going to be unpreserved 153 + */ 154 + } 155 + 156 + void restore_and_read_stage(int state_session_fd, int token, int *stage) 157 + { 158 + char buf[32] = {0}; 159 + int mfd; 160 + 161 + mfd = restore_and_verify_memfd(state_session_fd, token, NULL); 162 + if (mfd < 0) 163 + fail_exit("failed to restore state memfd"); 164 + 165 + if (read(mfd, buf, sizeof(buf) - 1) < 0) 166 + fail_exit("failed to read state mfd"); 167 + 168 + *stage = atoi(buf); 169 + 170 + close(mfd); 171 + } 172 + 173 + void daemonize_and_wait(void) 174 + { 175 + pid_t pid; 176 + 177 + ksft_print_msg("[STAGE 1] Forking persistent child to hold sessions...\n"); 178 + 179 + pid = fork(); 180 + if (pid < 0) 181 + fail_exit("fork failed"); 182 + 183 + if (pid > 0) { 184 + ksft_print_msg("[STAGE 1] Child PID: %d. Resources are pinned.\n", pid); 185 + ksft_print_msg("[STAGE 1] You may now perform kexec reboot.\n"); 186 + exit(EXIT_SUCCESS); 187 + } 188 + 189 + /* Detach from terminal so closing the window doesn't kill us */ 190 + if (setsid() < 0) 191 + fail_exit("setsid failed"); 192 + 193 + close(STDIN_FILENO); 194 + close(STDOUT_FILENO); 195 + close(STDERR_FILENO); 196 + 197 + /* Change dir to root to avoid locking filesystems */ 198 + if (chdir("/") < 0) 199 + exit(EXIT_FAILURE); 200 + 201 + while (1) 202 + sleep(60); 203 + } 204 + 205 + static int parse_stage_args(int argc, char *argv[]) 206 + { 207 + static struct option long_options[] = { 208 + {"stage", required_argument, 0, 's'}, 209 + {0, 0, 0, 0} 210 + }; 211 + int option_index = 0; 212 + int stage = 1; 213 + int opt; 214 + 215 + optind = 1; 216 + while ((opt = getopt_long(argc, argv, "s:", long_options, &option_index)) != -1) { 217 + switch (opt) { 218 + case 's': 219 + stage = atoi(optarg); 220 + if (stage != 1 && stage != 2) 221 + fail_exit("Invalid stage argument"); 222 + break; 223 + default: 224 + fail_exit("Unknown argument"); 225 + } 226 + } 227 + return stage; 228 + } 229 + 230 + int luo_test(int argc, char *argv[], 231 + const char *state_session_name, 232 + luo_test_stage1_fn stage1, 233 + luo_test_stage2_fn stage2) 234 + { 235 + int target_stage = parse_stage_args(argc, argv); 236 + int luo_fd = luo_open_device(); 237 + int state_session_fd; 238 + int detected_stage; 239 + 240 + if (luo_fd < 0) { 241 + ksft_exit_skip("Failed to open %s. Is the luo module loaded?\n", 242 + LUO_DEVICE); 243 + } 244 + 245 + state_session_fd = luo_retrieve_session(luo_fd, state_session_name); 246 + if (state_session_fd == -ENOENT) 247 + detected_stage = 1; 248 + else if (state_session_fd >= 0) 249 + detected_stage = 2; 250 + else 251 + fail_exit("Failed to check for state session"); 252 + 253 + if (target_stage != detected_stage) { 254 + ksft_exit_fail_msg("Stage mismatch Requested --stage %d, but system is in stage %d.\n" 255 + "(State session %s: %s)\n", 256 + target_stage, detected_stage, state_session_name, 257 + (detected_stage == 2) ? "EXISTS" : "MISSING"); 258 + } 259 + 260 + if (target_stage == 1) 261 + stage1(luo_fd); 262 + else 263 + stage2(luo_fd, state_session_fd); 264 + 265 + return 0; 266 + }
+44
tools/testing/selftests/liveupdate/luo_test_utils.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + /* 4 + * Copyright (c) 2025, Google LLC. 5 + * Pasha Tatashin <pasha.tatashin@soleen.com> 6 + * 7 + * Utility functions for LUO kselftests. 8 + */ 9 + 10 + #ifndef LUO_TEST_UTILS_H 11 + #define LUO_TEST_UTILS_H 12 + 13 + #include <errno.h> 14 + #include <string.h> 15 + #include <linux/liveupdate.h> 16 + #include "../kselftest.h" 17 + 18 + #define LUO_DEVICE "/dev/liveupdate" 19 + 20 + #define fail_exit(fmt, ...) \ 21 + ksft_exit_fail_msg("[%s:%d] " fmt " (errno: %s)\n", \ 22 + __func__, __LINE__, ##__VA_ARGS__, strerror(errno)) 23 + 24 + int luo_open_device(void); 25 + int luo_create_session(int luo_fd, const char *name); 26 + int luo_retrieve_session(int luo_fd, const char *name); 27 + int luo_session_finish(int session_fd); 28 + 29 + int create_and_preserve_memfd(int session_fd, int token, const char *data); 30 + int restore_and_verify_memfd(int session_fd, int token, const char *expected_data); 31 + 32 + void create_state_file(int luo_fd, const char *session_name, int token, 33 + int next_stage); 34 + void restore_and_read_stage(int state_session_fd, int token, int *stage); 35 + 36 + void daemonize_and_wait(void); 37 + 38 + typedef void (*luo_test_stage1_fn)(int luo_fd); 39 + typedef void (*luo_test_stage2_fn)(int luo_fd, int state_session_fd); 40 + 41 + int luo_test(int argc, char *argv[], const char *state_session_name, 42 + luo_test_stage1_fn stage1, luo_test_stage2_fn stage2); 43 + 44 + #endif /* LUO_TEST_UTILS_H */