]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/liveupdate: add userspace API selftests
authorPasha Tatashin <pasha.tatashin@soleen.com>
Tue, 25 Nov 2025 16:58:46 +0000 (11:58 -0500)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 27 Nov 2025 22:24:41 +0000 (14:24 -0800)
Introduce a selftest suite for LUO.  These tests validate the core
userspace-facing API provided by the /dev/liveupdate device and its
associated ioctls.

The suite covers fundamental device behavior, session management, and the
file preservation mechanism using memfd as a test case.  This provides
regression testing for the LUO uAPI.

The following functionality is verified:

Device Access:
    Basic open and close operations on /dev/liveupdate.
    Enforcement of exclusive device access (verifying EBUSY on a
    second open).

Session Management:
    Successful creation of sessions with unique names.
    Failure to create sessions with duplicate names.

File Preservation:
    Preserving a single memfd and verifying its content remains
    intact post-preservation.
    Preserving multiple memfds within a single session, each with
    unique data.
    A complex scenario involving multiple sessions, each containing
    a mix of empty and data-filled memfds.

Note: This test suite is limited to verifying the pre-kexec functionality
of LUO (e.g., session creation, file preservation).  The post-kexec
restoration of resources is not covered, as the kselftest framework does
not currently support orchestrating a reboot and continuing execution in
the new kernel.

Link: https://lkml.kernel.org/r/20251125165850.3389713-17-pasha.tatashin@soleen.com
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Pratyush Yadav <pratyush@kernel.org>
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 <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: Zhu Yanjun <yanjun.zhu@linux.dev>
Cc: Zijun Hu <quic_zijuhu@quicinc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
MAINTAINERS
tools/testing/selftests/Makefile
tools/testing/selftests/liveupdate/.gitignore [new file with mode: 0644]
tools/testing/selftests/liveupdate/Makefile [new file with mode: 0644]
tools/testing/selftests/liveupdate/config [new file with mode: 0644]
tools/testing/selftests/liveupdate/liveupdate.c [new file with mode: 0644]

index cabbf30d50e19bf8ff238ee768cab596cf0908dc..83bac6c48c98c040eb1cda5f8d489caba64c64ef 100644 (file)
@@ -14480,6 +14480,7 @@ F:      include/linux/liveupdate/
 F:     include/uapi/linux/liveupdate.h
 F:     kernel/liveupdate/
 F:     mm/memfd_luo.c
+F:     tools/testing/selftests/liveupdate/
 
 LLC (802.2)
 L:     netdev@vger.kernel.org
index c46ebdb9b8ef7defcb604bd246261c88be0bbfe0..56e44a98d6a597179a5d7e0e67282c322c22ddda 100644 (file)
@@ -54,6 +54,7 @@ TARGETS += kvm
 TARGETS += landlock
 TARGETS += lib
 TARGETS += livepatch
+TARGETS += liveupdate
 TARGETS += lkdtm
 TARGETS += lsm
 TARGETS += membarrier
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
new file mode 100644 (file)
index 0000000..6618270
--- /dev/null
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+*
+!/**/
+!*.c
+!*.h
+!*.sh
+!.gitignore
+!config
+!Makefile
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
new file mode 100644 (file)
index 0000000..620cb4c
--- /dev/null
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+TEST_GEN_PROGS += liveupdate
+
+include ../lib.mk
+
+CFLAGS += $(KHDR_INCLUDES)
+CFLAGS += -Wall -O2 -Wno-unused-function
+CFLAGS += -MD
+
+LIB_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIB_C))
+TEST_O := $(patsubst %, %.o, $(TEST_GEN_PROGS))
+TEST_O += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
+
+TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIB_O))
+TEST_DEP_FILES += $(patsubst %.o, %.d, $(TEST_O))
+-include $(TEST_DEP_FILES)
+
+$(LIB_O): $(OUTPUT)/%.o: %.c
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIB_O)
+       $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIB_O) $(LDLIBS) -o $@
+
+EXTRA_CLEAN += $(LIB_O)
+EXTRA_CLEAN += $(TEST_O)
+EXTRA_CLEAN += $(TEST_DEP_FILES)
diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config
new file mode 100644 (file)
index 0000000..91d03f9
--- /dev/null
@@ -0,0 +1,11 @@
+CONFIG_BLK_DEV_INITRD=y
+CONFIG_KEXEC_FILE=y
+CONFIG_KEXEC_HANDOVER=y
+CONFIG_KEXEC_HANDOVER_ENABLE_DEFAULT=y
+CONFIG_KEXEC_HANDOVER_DEBUGFS=y
+CONFIG_KEXEC_HANDOVER_DEBUG=y
+CONFIG_LIVEUPDATE=y
+CONFIG_LIVEUPDATE_TEST=y
+CONFIG_MEMFD_CREATE=y
+CONFIG_TMPFS=y
+CONFIG_SHMEM=y
diff --git a/tools/testing/selftests/liveupdate/liveupdate.c b/tools/testing/selftests/liveupdate/liveupdate.c
new file mode 100644 (file)
index 0000000..c2878e3
--- /dev/null
@@ -0,0 +1,348 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Pasha Tatashin <pasha.tatashin@soleen.com>
+ */
+
+/*
+ * Selftests for the Live Update Orchestrator.
+ * This test suite verifies the functionality and behavior of the
+ * /dev/liveupdate character device and its session management capabilities.
+ *
+ * Tests include:
+ * - Device access: basic open/close, and enforcement of exclusive access.
+ * - Session management: creation of unique sessions, and duplicate name detection.
+ * - Resource preservation: successfully preserving individual and multiple memfds,
+ *   verifying contents remain accessible.
+ * - Complex multi-session scenarios involving mixed empty and populated files.
+ */
+
+#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"
+
+#define LIVEUPDATE_DEV "/dev/liveupdate"
+
+FIXTURE(liveupdate_device) {
+       int fd1;
+       int fd2;
+};
+
+FIXTURE_SETUP(liveupdate_device)
+{
+       self->fd1 = -1;
+       self->fd2 = -1;
+}
+
+FIXTURE_TEARDOWN(liveupdate_device)
+{
+       if (self->fd1 >= 0)
+               close(self->fd1);
+       if (self->fd2 >= 0)
+               close(self->fd2);
+}
+
+/*
+ * Test Case: Basic Open and Close
+ *
+ * Verifies that the /dev/liveupdate device can be opened and subsequently
+ * closed without errors. Skips if the device does not exist.
+ */
+TEST_F(liveupdate_device, basic_open_close)
+{
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist.", LIVEUPDATE_DEV);
+
+       ASSERT_GE(self->fd1, 0);
+       ASSERT_EQ(close(self->fd1), 0);
+       self->fd1 = -1;
+}
+
+/*
+ * Test Case: Exclusive Open Enforcement
+ *
+ * Verifies that the /dev/liveupdate device can only be opened by one process
+ * at a time. It checks that a second attempt to open the device fails with
+ * the EBUSY error code.
+ */
+TEST_F(liveupdate_device, exclusive_open)
+{
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist.", LIVEUPDATE_DEV);
+
+       ASSERT_GE(self->fd1, 0);
+       self->fd2 = open(LIVEUPDATE_DEV, O_RDWR);
+       EXPECT_LT(self->fd2, 0);
+       EXPECT_EQ(errno, EBUSY);
+}
+
+/* Helper function to create a LUO session via ioctl. */
+static int create_session(int lu_fd, const char *name)
+{
+       struct liveupdate_ioctl_create_session args = {};
+
+       args.size = sizeof(args);
+       strncpy((char *)args.name, name, sizeof(args.name) - 1);
+
+       if (ioctl(lu_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &args))
+               return -errno;
+
+       return args.fd;
+}
+
+/*
+ * Test Case: Create Duplicate Session
+ *
+ * Verifies that attempting to create two sessions with the same name fails
+ * on the second attempt with EEXIST.
+ */
+TEST_F(liveupdate_device, create_duplicate_session)
+{
+       int session_fd1, session_fd2;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd1 = create_session(self->fd1, "duplicate-session-test");
+       ASSERT_GE(session_fd1, 0);
+
+       session_fd2 = create_session(self->fd1, "duplicate-session-test");
+       EXPECT_LT(session_fd2, 0);
+       EXPECT_EQ(-session_fd2, EEXIST);
+
+       ASSERT_EQ(close(session_fd1), 0);
+}
+
+/*
+ * Test Case: Create Distinct Sessions
+ *
+ * Verifies that creating two sessions with different names succeeds.
+ */
+TEST_F(liveupdate_device, create_distinct_sessions)
+{
+       int session_fd1, session_fd2;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd1 = create_session(self->fd1, "distinct-session-1");
+       ASSERT_GE(session_fd1, 0);
+
+       session_fd2 = create_session(self->fd1, "distinct-session-2");
+       ASSERT_GE(session_fd2, 0);
+
+       ASSERT_EQ(close(session_fd1), 0);
+       ASSERT_EQ(close(session_fd2), 0);
+}
+
+static int preserve_fd(int session_fd, int fd_to_preserve, __u64 token)
+{
+       struct liveupdate_session_preserve_fd args = {};
+
+       args.size = sizeof(args);
+       args.fd = fd_to_preserve;
+       args.token = token;
+
+       if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &args))
+               return -errno;
+
+       return 0;
+}
+
+/*
+ * Test Case: Preserve MemFD
+ *
+ * Verifies that a valid memfd can be successfully preserved in a session and
+ * that its contents remain intact after the preservation call.
+ */
+TEST_F(liveupdate_device, preserve_memfd)
+{
+       const char *test_str = "hello liveupdate";
+       char read_buf[64] = {};
+       int session_fd, mem_fd;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd = create_session(self->fd1, "preserve-memfd-test");
+       ASSERT_GE(session_fd, 0);
+
+       mem_fd = memfd_create("test-memfd", 0);
+       ASSERT_GE(mem_fd, 0);
+
+       ASSERT_EQ(write(mem_fd, test_str, strlen(test_str)), strlen(test_str));
+       ASSERT_EQ(preserve_fd(session_fd, mem_fd, 0x1234), 0);
+       ASSERT_EQ(close(session_fd), 0);
+
+       ASSERT_EQ(lseek(mem_fd, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd, read_buf, sizeof(read_buf)), strlen(test_str));
+       ASSERT_STREQ(read_buf, test_str);
+       ASSERT_EQ(close(mem_fd), 0);
+}
+
+/*
+ * Test Case: Preserve Multiple MemFDs
+ *
+ * Verifies that multiple memfds can be preserved in a single session,
+ * each with a unique token, and that their contents remain distinct and
+ * correct after preservation.
+ */
+TEST_F(liveupdate_device, preserve_multiple_memfds)
+{
+       const char *test_str1 = "data for memfd one";
+       const char *test_str2 = "data for memfd two";
+       char read_buf[64] = {};
+       int session_fd, mem_fd1, mem_fd2;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd = create_session(self->fd1, "preserve-multi-memfd-test");
+       ASSERT_GE(session_fd, 0);
+
+       mem_fd1 = memfd_create("test-memfd-1", 0);
+       ASSERT_GE(mem_fd1, 0);
+       mem_fd2 = memfd_create("test-memfd-2", 0);
+       ASSERT_GE(mem_fd2, 0);
+
+       ASSERT_EQ(write(mem_fd1, test_str1, strlen(test_str1)), strlen(test_str1));
+       ASSERT_EQ(write(mem_fd2, test_str2, strlen(test_str2)), strlen(test_str2));
+
+       ASSERT_EQ(preserve_fd(session_fd, mem_fd1, 0xAAAA), 0);
+       ASSERT_EQ(preserve_fd(session_fd, mem_fd2, 0xBBBB), 0);
+
+       memset(read_buf, 0, sizeof(read_buf));
+       ASSERT_EQ(lseek(mem_fd1, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd1, read_buf, sizeof(read_buf)), strlen(test_str1));
+       ASSERT_STREQ(read_buf, test_str1);
+
+       memset(read_buf, 0, sizeof(read_buf));
+       ASSERT_EQ(lseek(mem_fd2, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd2, read_buf, sizeof(read_buf)), strlen(test_str2));
+       ASSERT_STREQ(read_buf, test_str2);
+
+       ASSERT_EQ(close(mem_fd1), 0);
+       ASSERT_EQ(close(mem_fd2), 0);
+       ASSERT_EQ(close(session_fd), 0);
+}
+
+/*
+ * Test Case: Preserve Complex Scenario
+ *
+ * Verifies a more complex scenario with multiple sessions and a mix of empty
+ * and non-empty memfds distributed across them.
+ */
+TEST_F(liveupdate_device, preserve_complex_scenario)
+{
+       const char *data1 = "data for session 1";
+       const char *data2 = "data for session 2";
+       char read_buf[64] = {};
+       int session_fd1, session_fd2;
+       int mem_fd_data1, mem_fd_empty1, mem_fd_data2, mem_fd_empty2;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd1 = create_session(self->fd1, "complex-session-1");
+       ASSERT_GE(session_fd1, 0);
+       session_fd2 = create_session(self->fd1, "complex-session-2");
+       ASSERT_GE(session_fd2, 0);
+
+       mem_fd_data1 = memfd_create("data1", 0);
+       ASSERT_GE(mem_fd_data1, 0);
+       ASSERT_EQ(write(mem_fd_data1, data1, strlen(data1)), strlen(data1));
+
+       mem_fd_empty1 = memfd_create("empty1", 0);
+       ASSERT_GE(mem_fd_empty1, 0);
+
+       mem_fd_data2 = memfd_create("data2", 0);
+       ASSERT_GE(mem_fd_data2, 0);
+       ASSERT_EQ(write(mem_fd_data2, data2, strlen(data2)), strlen(data2));
+
+       mem_fd_empty2 = memfd_create("empty2", 0);
+       ASSERT_GE(mem_fd_empty2, 0);
+
+       ASSERT_EQ(preserve_fd(session_fd1, mem_fd_data1, 0x1111), 0);
+       ASSERT_EQ(preserve_fd(session_fd1, mem_fd_empty1, 0x2222), 0);
+       ASSERT_EQ(preserve_fd(session_fd2, mem_fd_data2, 0x3333), 0);
+       ASSERT_EQ(preserve_fd(session_fd2, mem_fd_empty2, 0x4444), 0);
+
+       ASSERT_EQ(lseek(mem_fd_data1, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd_data1, read_buf, sizeof(read_buf)), strlen(data1));
+       ASSERT_STREQ(read_buf, data1);
+
+       memset(read_buf, 0, sizeof(read_buf));
+       ASSERT_EQ(lseek(mem_fd_data2, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd_data2, read_buf, sizeof(read_buf)), strlen(data2));
+       ASSERT_STREQ(read_buf, data2);
+
+       ASSERT_EQ(lseek(mem_fd_empty1, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd_empty1, read_buf, sizeof(read_buf)), 0);
+
+       ASSERT_EQ(lseek(mem_fd_empty2, 0, SEEK_SET), 0);
+       ASSERT_EQ(read(mem_fd_empty2, read_buf, sizeof(read_buf)), 0);
+
+       ASSERT_EQ(close(mem_fd_data1), 0);
+       ASSERT_EQ(close(mem_fd_empty1), 0);
+       ASSERT_EQ(close(mem_fd_data2), 0);
+       ASSERT_EQ(close(mem_fd_empty2), 0);
+       ASSERT_EQ(close(session_fd1), 0);
+       ASSERT_EQ(close(session_fd2), 0);
+}
+
+/*
+ * Test Case: Preserve Unsupported File Descriptor
+ *
+ * Verifies that attempting to preserve a file descriptor that does not have
+ * a registered Live Update handler fails gracefully.
+ * Uses /dev/null as a representative of a file type (character device)
+ * that is not supported by the orchestrator.
+ */
+TEST_F(liveupdate_device, preserve_unsupported_fd)
+{
+       int session_fd, unsupported_fd;
+       int ret;
+
+       self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
+       if (self->fd1 < 0 && errno == ENOENT)
+               SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
+       ASSERT_GE(self->fd1, 0);
+
+       session_fd = create_session(self->fd1, "unsupported-fd-test");
+       ASSERT_GE(session_fd, 0);
+
+       unsupported_fd = open("/dev/null", O_RDWR);
+       ASSERT_GE(unsupported_fd, 0);
+
+       ret = preserve_fd(session_fd, unsupported_fd, 0xDEAD);
+       EXPECT_EQ(ret, -ENOENT);
+
+       ASSERT_EQ(close(unsupported_fd), 0);
+       ASSERT_EQ(close(session_fd), 0);
+}
+
+TEST_HARNESS_MAIN