Skip to contentSkip to conversation

selftests/liveupdate: add memfd tests

PATCH Superseded Pratyush Yadav 3 participants 17 messages Mar 9 – Mar 18

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pratyush Yadav

author maintainer
#0 lore raw

cover selftests/liveupdate: add memfd tests

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

Hi,

This series adds some tests for memfd preservation across a live update. Currently memfd is only tested indirectly via luo_kexec_simple or luo_multi_session. Add a dedicated test suite for it.

Patches 1 and 2 are preparatory, adding base framework and helpers, and the other patches each add a test. Some of the code is taken from the libluo patches [0] I sent a while ago.

[0] https://lore.kernel.org/linux-mm/[email protected]/

Regards,
Pratyush Yadav

Pratyush Yadav (Google) (6):
selftests/liveupdate: add framework for memfd tests selftests/liveupdate: add helper functions for memfd tests selftests/liveupdate: add test for memfd content preservation selftests/liveupdate: add test for zero-size memfd preservation selftests/liveupdate: add test for operations on a preserved memfd selftests/liveupdate: add fallocate test for memfd

 tools/testing/selftests/liveupdate/Makefile   |   2 +
 .../testing/selftests/liveupdate/luo_memfd.c  | 306 ++++++++++++++++++
 .../selftests/liveupdate/luo_test_utils.c     | 175 +++++++++-
 .../selftests/liveupdate/luo_test_utils.h     |   9 +
 4 files changed, 491 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.c

base-commit: 6f04517c06a898228eda00cbd8573d9e84003108

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
author maintainer
#1 lore raw

PATCH 1/6 selftests/liveupdate: add framework for memfd tests

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().

---
 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
tools/testing/selftests/liveupdate/Makefile +1 -0
index 080754787ede..051daae55eec 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_memfd.c new file +77 -0

77 lines not on this page · raw message

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
#2 lore raw

Re: [PATCH 1/6] selftests/liveupdate: add framework for memfd tests

Quoted 69 lines · On Mon, 9 Mar 2026 11:54:34 +0000 Pratyush Yadav <[email protected]> wrote:

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().

---
 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
tools/testing/selftests/liveupdate/Makefile +1 -0
index 080754787ede..051daae55eec 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_memfd.c new file +18 -0
new file mode 100644
index 000000000000..b779eee18387
@@ -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>

tools/testing/selftests/liveupdate/luo_memfd.c line 28

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+
+#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"

Hello!

LIVEUPDATE_DEV doesn't seem to be used anywhere?

Quoted 21 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +21 -0
+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--;

tools/testing/selftests/liveupdate/luo_memfd.c line 59

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+			} 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);

Need to reverse strerror(errno) and LUO_DEVICE?

Quoted 22 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +18 -0
+
+	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);
+}
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
author maintainer
#3 lore raw

Re: [PATCH 1/6] selftests/liveupdate: add framework for memfd tests

Quoted 1 line · Usama Arif

On Mon, 9 Mar 2026 11:54:34 +0000 Pratyush Yadav <[email protected]> wrote:

[...]

Quoted 37 lines
Quoted 24 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +18 -0
new file mode 100644
index 000000000000..b779eee18387
@@ -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>

tools/testing/selftests/liveupdate/luo_memfd.c line 28

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+
+#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"

Hello!

Quoted 1 line

LIVEUPDATE_DEV doesn't seem to be used anywhere?

Right. This is a leftover from development. Will drop.

Quoted 26 lines
Quoted 26 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +25 -0
+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");
+			}
+		}
Quoted 8 lines

tools/testing/selftests/liveupdate/luo_memfd.c line 59

Quoted 6 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +6 -0
+	}
+
+	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);

Need to reverse strerror(errno) and LUO_DEVICE?

Will fix. Thanks.

Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +18 -0
+
+	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);
+}
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Signature · 2 lines
Regards,
Pratyush Yadav
maintainer Reviewed-by on v1 1/6
#4 lore raw

Re: [PATCH 1/6] selftests/liveupdate: add framework for memfd tests

In reply to Pratyush Yadav
Quoted 19 lines · Pratyush Yadav

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.

Quoted 8 lines

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().

with a few nits below.

Quoted 43 lines
---
 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
tools/testing/selftests/liveupdate/Makefile +1 -0
index 080754787ede..051daae55eec 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_memfd.c new file +19 -0
new file mode 100644
index 000000000000..b779eee18387
@@ -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>
+

tools/testing/selftests/liveupdate/luo_memfd.c line 29

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+#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;

Nit: declaring stage on a separate line is a bit clearer.

tools/testing/selftests/liveupdate/luo_memfd.c line 33

Quoted 4 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +4 -0
+
+int main(int argc, char *argv[])
+{
+	int session, expected_stage = 0;

Ditto.

Quoted 15 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +15 -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;

tools/testing/selftests/liveupdate/luo_memfd.c line 58

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+				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",

LUO can't be a module :) Maybe

"Is LUO enabled in the kernel?"

tools/testing/selftests/liveupdate/luo_memfd.c line 67

Quoted 9 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +9 -0
+			       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");

There is ksft_exit_fail_perror(), would be more consistent with other exit paths.

Quoted 13 lines
tools/testing/selftests/liveupdate/luo_memfd.c new file +10 -0
+
+	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);
+}
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#5 lore raw

PATCH 2/6 selftests/liveupdate: add helper functions for memfd tests

In reply to Pratyush Yadav

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

Add some helper functions that will be used by memfd tests. This moves some of the complexity out of the test itself, which results in better test readability and less code duplication.

---
 .../selftests/liveupdate/luo_test_utils.c     | 175 +++++++++++++++++-
 .../selftests/liveupdate/luo_test_utils.h     |   9 +
 2 files changed, 183 insertions(+), 1 deletion(-)
tools/testing/selftests/liveupdate/luo_test_utils.c +174 -1
index 3c8721c505df..45ace3697ee6 100644
@@ -1,8 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 /*
- * Copyright (c) 2025, Google LLC.
+ * Copyright (c) 2025-2026, Google LLC.
  * Pasha Tatashin <[email protected]>
+ * Pratyush Yadav (Google) <[email protected]>
+ *
+ * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
+ * Pratyush Yadav <[email protected]>
  */
 
 #define _GNU_SOURCE
@@ -23,6 +27,175 @@
 
 #include "luo_test_utils.h"
 
+/* Read exactly specified size from fd. Any less results in error. */
+int read_size(int fd, char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t bytes_read;
+
+	while (remain) {
+		bytes_read = read(fd, buffer, remain);
+		if (bytes_read == 0)
+			return -ENODATA;
+		if (bytes_read < 0)
+			return -errno;
+
+		remain -= bytes_read;
+	}
+
+	return 0;
+}
+
+/* Write exactly specified size from fd. Any less results in error. */
+int write_size(int fd, const char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t written;
+
+	while (remain) {
+		written = write(fd, buffer, remain);
+		if (written == 0)
+			return -EIO;
+		if (written < 0)
+			return -errno;
+
+		remain -= written;
+	}
+
+	return 0;
+}
+
+int generate_random_data(char *buffer, size_t size)
+{
+	int fd, ret;
+
+	fd = open("/dev/urandom", O_RDONLY);
+	if (fd < 0)
+		return -errno;
+
+	ret = read_size(fd, buffer, size);
+	close(fd);
+	return ret;
+}
+
+int save_test_data(const char *filename, const char *buffer, size_t size)
+{
+	int fd, ret;
+
+	fd = open(filename, O_RDWR | O_CREAT, 0666);
+	if (fd < 0)
+		return -errno;
+
+	ret = write_size(fd, buffer, size);
+	fsync(fd);
+	close(fd);
+	return ret;
+}
+
+int load_test_data(const char *filename, char *buffer, size_t size)
+{
+	int fd, ret;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		return -errno;
+
+	ret = read_size(fd, buffer, size);
+	close(fd);
+	return ret;
+}
+
+/* Create and initialize a memfd with random data. */
+int create_random_memfd(const char *memfd_name, char *buffer, size_t size)
+{
+	int fd;
+	int ret;
+
+	fd = memfd_create(memfd_name, 0);
+	if (fd < 0)
+		return -errno;
+
+	ret = generate_random_data(buffer, size);
+	if (ret < 0) {
+		close(fd);
+		return ret;
+	}
+
+	if (write_size(fd, buffer, size) < 0) {
+		close(fd);
+		return -errno;
+	}
+
+	/* Reset file position to beginning */
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		close(fd);
+		return -errno;
+	}
+
+	return fd;
+}
+
+/*
+ * Make sure fd contains expected data up to size. Returns 0 on success, 1 on
+ * data mismatch, -errno on error.
+ */
+int verify_fd_content(int fd, const char *expected_data, size_t size)
+{
+	char *buffer;
+	int ret;
+
+	buffer = malloc(size);
+	if (!buffer)
+		return -ENOMEM;
+
+	/* Reset file position to beginning */
+	if (lseek(fd, 0, SEEK_SET) < 0) {
+		ret = -errno;
+		goto out;
+	}
+
+	ret = read_size(fd, buffer, size);
+	if (ret < 0)
+		goto out;
+
+	if (memcmp(buffer, expected_data, size) != 0) {
+		ret = 1;
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	free(buffer);
+	return ret;
+}
+
+/*
+ * Verify fd content using mmap. Returns 0 on success, 1 on data mismatch,
+ * -errno on error.
+ */
+int verify_fd_content_mmap(int fd, const char *expected_data, size_t size)
+{
+	char *mapped_mem;
+	int ret;
+
+	mapped_mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+	if (mapped_mem == MAP_FAILED)
+		return -errno;
+
+	/* ret = memcmp(mapped_mem, expected_data, size) ? 1 : 0; */
+	ret = 0;
+	for (size_t i = 0; i < size; i++) {
+		if (mapped_mem[i] != expected_data[i]) {
+			ret = 1;
+			break;
+		}
+	}
+
+	munmap(mapped_mem, size);
+	return ret;
+}
+
 int luo_open_device(void)
 {
 	return open(LUO_DEVICE, O_RDWR);
tools/testing/selftests/liveupdate/luo_test_utils.h +9 -0
index 90099bf49577..d1b85703708b 100644
@@ -35,6 +35,15 @@ void restore_and_read_stage(int state_session_fd, int token, int *stage);
 
 void daemonize_and_wait(void);
 
+int read_size(int fd, char *buffer, size_t size);
+int write_size(int fd, const char *buffer, size_t size);
+int generate_random_data(char *buffer, size_t size);
+int save_test_data(const char *filename, const char *buffer, size_t size);
+int load_test_data(const char *filename, char *buffer, size_t size);
+int create_random_memfd(const char *memfd_name, char *buffer, size_t size);
+int verify_fd_content(int fd, const char *expected_data, size_t size);
+int verify_fd_content_mmap(int fd, const char *expected_data, size_t size);
+
 typedef void (*luo_test_stage1_fn)(int luo_fd);
 typedef void (*luo_test_stage2_fn)(int luo_fd, int state_session_fd);
 
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer
#6 lore raw

Re: [PATCH 2/6] selftests/liveupdate: add helper functions for memfd tests

Quoted 6 lines · Pratyush Yadav

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

Add some helper functions that will be used by memfd tests. This moves some of the complexity out of the test itself, which results in better test readability and less code duplication.

Quoted 6 lines
---
 .../selftests/liveupdate/luo_test_utils.c     | 175 +++++++++++++++++-
 .../selftests/liveupdate/luo_test_utils.h     |   9 +
 2 files changed, 183 insertions(+), 1 deletion(-)

Some review comments from an LLM that make sense to me as well :)

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +0 -0

[ ... ]

Quoted 5 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +5 -0
+/* Read exactly specified size from fd. Any less results in error. */
+int read_size(int fd, char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t bytes_read;

tools/testing/selftests/liveupdate/luo_test_utils.c line 44

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10 -0
+
+	while (remain) {
+		bytes_read = read(fd, buffer, remain);
+		if (bytes_read == 0)
+			return -ENODATA;
+		if (bytes_read < 0)
+			return -errno;
+
+		remain -= bytes_read;
+	}

Should the buffer pointer be advanced after each read()? As written, if read() returns a partial result, the next iteration reads into the same position, overwriting the data just read. Something like buffer += bytes_read after remain -= bytes_read seems to be missing.

This is exercised by generate_random_data() which reads from /dev/urandom, where partial reads are possible for large requests.

Quoted 5 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +5 -0
+/* Write exactly specified size from fd. Any less results in error. */
+int write_size(int fd, const char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t written;

tools/testing/selftests/liveupdate/luo_test_utils.c line 63

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10 -0
+
+	while (remain) {
+		written = write(fd, buffer, remain);
+		if (written == 0)
+			return -EIO;
+		if (written < 0)
+			return -errno;
+
+		remain -= written;
+	}

Same issue here: buffer is not advanced after each write(), so on a partial write the same initial bytes would be re-sent instead of continuing from where the previous write left off.

Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#7 lore raw

Re: [PATCH 2/6] selftests/liveupdate: add helper functions for memfd tests

Quoted 57 lines · Mike Rapoport
Quoted 6 lines · Pratyush Yadav

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

Add some helper functions that will be used by memfd tests. This moves some of the complexity out of the test itself, which results in better test readability and less code duplication.

Quoted 6 lines
---
 .../selftests/liveupdate/luo_test_utils.c     | 175 +++++++++++++++++-
 .../selftests/liveupdate/luo_test_utils.h     |   9 +
 2 files changed, 183 insertions(+), 1 deletion(-)

Some review comments from an LLM that make sense to me as well :)

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +0 -0

[ ... ]

Quoted 5 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +5 -0
+/* Read exactly specified size from fd. Any less results in error. */
+int read_size(int fd, char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t bytes_read;

tools/testing/selftests/liveupdate/luo_test_utils.c line 44

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10 -0
+
+	while (remain) {
+		bytes_read = read(fd, buffer, remain);
+		if (bytes_read == 0)
+			return -ENODATA;
+		if (bytes_read < 0)
+			return -errno;
+
+		remain -= bytes_read;
+	}

Should the buffer pointer be advanced after each read()? As written, if read() returns a partial result, the next iteration reads into the same position, overwriting the data just read. Something like buffer += bytes_read after remain -= bytes_read seems to be missing.

This is exercised by generate_random_data() which reads from /dev/urandom, where partial reads are possible for large requests.

Quoted 11 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +11 -0
+/* Write exactly specified size from fd. Any less results in error. */
+int write_size(int fd, const char *buffer, size_t size)
+{
+	size_t remain = size;
+	ssize_t written;
+
+	while (remain) {
+		written = write(fd, buffer, remain);
+		if (written == 0)
+			return -EIO;
+		if (written < 0)
Quoted 8 lines

tools/testing/selftests/liveupdate/luo_test_utils.c line 63

Quoted 4 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +4 -0
+			return -errno;
+
+		remain -= written;
+	}

Same issue here: buffer is not advanced after each write(), so on a partial write the same initial bytes would be re-sent instead of continuing from where the previous write left off.

Yeah, good catch on both. Will fix.

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer Reviewed-by on v1 2/6
#8 lore raw

Re: [PATCH 2/6] selftests/liveupdate: add helper functions for memfd tests

In reply to Pratyush Yadav
Quoted 15 lines · Pratyush Yadav

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

Add some helper functions that will be used by memfd tests. This moves some of the complexity out of the test itself, which results in better test readability and less code duplication.

---
 .../selftests/liveupdate/luo_test_utils.c     | 175 +++++++++++++++++-
 .../selftests/liveupdate/luo_test_utils.h     |   9 +
 2 files changed, 183 insertions(+), 1 deletion(-)
tools/testing/selftests/liveupdate/luo_test_utils.c +0 -0
index 3c8721c505df..45ace3697ee6 100644

...

tools/testing/selftests/liveupdate/luo_test_utils.c line 142

Quoted 5 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +5 -0
+/*
+ * Make sure fd contains expected data up to size. Returns 0 on success, 1 on
+ * data mismatch, -errno on error.
+ */
+int verify_fd_content(int fd, const char *expected_data, size_t size)

Maybe verify_fd_content_read() to match _mmap()?

WIth this and write_size() and read_size() fixed

Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#9 lore raw

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

In reply to Pratyush Yadav

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

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.

---
 tools/testing/selftests/liveupdate/Makefile   |  1 +
 .../testing/selftests/liveupdate/luo_memfd.c  | 61 +++++++++++++++++++
 2 files changed, 62 insertions(+)
tools/testing/selftests/liveupdate/Makefile +1 -0
index 051daae55eec..8e2eb6500c07 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_memfd.c +61 -0

87 lines not on this page · raw message

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer
#10 lore raw

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

Quoted 6 lines · Pratyush Yadav

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

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.

There's a small caveat here, as some CI systems put selftests in tmpfs, so creating a file there won't really help.
I think there should be a skip if the test runs of tmpfs.

Quoted 52 lines

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

---
 tools/testing/selftests/liveupdate/Makefile   |  1 +
 .../testing/selftests/liveupdate/luo_memfd.c  | 61 +++++++++++++++++++
 2 files changed, 62 insertions(+)
tools/testing/selftests/liveupdate/Makefile +1 -0
index 051daae55eec..8e2eb6500c07 100644
@@ -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
 
tools/testing/selftests/liveupdate/luo_memfd.c +6 -0
index b779eee18387..52b5f6b16e19 100644
@@ -3,6 +3,9 @@
 /*
  * Copyright (c) 2026, Google LLC.
  * Pratyush Yadav (Google) <[email protected]>
+ *
+ * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
+ * Pratyush Yadav <[email protected]>
  */
 
 /*
@@ -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"

tools/testing/selftests/liveupdate/luo_memfd.c line 37

Quoted 7 lines
tools/testing/selftests/liveupdate/luo_memfd.c +4 -0
@@ -25,9 +31,64 @@
 #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 RANDOM_DATA_FILE "luo_random_data.bin"

Reads to me like a random file containing data :) Something like FILESYSTEM_DATA_COPY would better express the meaning.

Quoted 9 lines
tools/testing/selftests/liveupdate/luo_memfd.c +6 -0
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
+/*
+ * Test that a memfd with its data is preserved across live update.
+ */
+TEST(memfd_data)
+{

tools/testing/selftests/liveupdate/luo_memfd.c line 56

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c +10 -0
+	int fd, session;
+	char *buffer;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+	struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+	buffer = malloc(MEMFD_DATA_BUFFER_SIZE);
+	ASSERT_NE(buffer, NULL);
+
+	switch (stage) {
+	case 1:

Can we please move each state to a helper function?

Quoted 43 lines
tools/testing/selftests/liveupdate/luo_memfd.c +35 -0
+		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(RANDOM_DATA_FILE, 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();
+		break;
+	case 2:
+		session = luo_retrieve_session(luo_fd, MEMFD_DATA_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
+		ASSERT_EQ(load_test_data(RANDOM_DATA_FILE, 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(fd, buffer, MEMFD_DATA_BUFFER_SIZE), 0);
+
+		ASSERT_EQ(luo_session_finish(session), 0);
+		break;
 
 
+	default:
+		TH_LOG("Unknown stage %d\n", stage);
+		ASSERT_FALSE(true);
+	}
+}
+
 int main(int argc, char *argv[])

{
int session, expected_stage = 0;

Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#11 lore raw

PATCH 4/6 selftests/liveupdate: add test for zero-size memfd preservation

In reply to Pratyush Yadav

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

A zero-size memfd is a special case of memfd preservation. It takes a different path from normal both during preservation and during restore. In the serialization structure, the number of folios if zero and the vmalloc array with folios is empty. The restore logic should check for this and make sure to not touch the invalid array.

Add a test to make sure this path works as expected. In stage 1, the test creates and preserves a memfd without any data. In stage 2, the test retrieves the memfd and makes sure it is still without data.

---
 .../testing/selftests/liveupdate/luo_memfd.c  | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +47 -0
index 52b5f6b16e19..56106cd09978 100644
@@ -36,6 +36,9 @@
 #define MEMFD_DATA_BUFFER_SIZE SZ_1M
 #define RANDOM_DATA_FILE "luo_random_data.bin"
 
+#define ZERO_SESSION_NAME "zero_session"
+#define ZERO_MEMFD_TOKEN 1
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
@@ -89,6 +92,50 @@ TEST(memfd_data)
 	}
 }
 
+/*
+ * Test that a zero-sized memfd is preserved across live update.
+ */
+TEST(zero_memfd)
+{
+	int zero_fd, session;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+	struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+	switch (stage) {
+	case 1:
+		session = luo_create_session(luo_fd, ZERO_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
+		zero_fd = memfd_create("zero_memfd", 0);
+		ASSERT_GE(zero_fd, 0);
+
+		preserve_arg.fd = zero_fd;
+		preserve_arg.token = ZERO_MEMFD_TOKEN;
+		ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), 0);
+
+		close(zero_fd);
+		daemonize_and_wait();
+		break;
+	case 2:
+		session = luo_retrieve_session(luo_fd, ZERO_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
+		retrieve_arg.token = ZERO_MEMFD_TOKEN;
+		ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_RETRIEVE_FD, &retrieve_arg), 0);
+		zero_fd = retrieve_arg.fd;
+		ASSERT_GE(zero_fd, 0);
+
+		ASSERT_EQ(lseek(zero_fd, 0, SEEK_END), 0);
+
+		ASSERT_EQ(luo_session_finish(session), 0);
+		close(zero_fd);
+		break;
+	default:
+		TH_LOG("Unknown stage %d\n", stage);
+		ASSERT_FALSE(true);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	int session, expected_stage = 0;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer Reviewed-by on v1 4/6
#12 lore raw

Re: [PATCH 4/6] selftests/liveupdate: add test for zero-size memfd preservation

Quoted 5 lines · Pratyush Yadav

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

A zero-size memfd is a special case of memfd preservation. It takes a different path from normal both during preservation and during restore. In the serialization structure, the number of folios if zero and the

^ typo: is

Quoted 31 lines

vmalloc array with folios is empty. The restore logic should check for this and make sure to not touch the invalid array.

Add a test to make sure this path works as expected. In stage 1, the test creates and preserves a memfd without any data. In stage 2, the test retrieves the memfd and makes sure it is still without data.

---
 .../testing/selftests/liveupdate/luo_memfd.c  | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +4 -0
index 52b5f6b16e19..56106cd09978 100644
@@ -36,6 +36,9 @@
 #define MEMFD_DATA_BUFFER_SIZE SZ_1M
 #define RANDOM_DATA_FILE "luo_random_data.bin"
 
+#define ZERO_SESSION_NAME "zero_session"
+#define ZERO_MEMFD_TOKEN 1
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
@@ -89,6 +92,50 @@ TEST(memfd_data)
 	}
 }
 
+/*

tools/testing/selftests/liveupdate/luo_memfd.c TEST(memfd_data) · line 105

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c +10 -0
+ * Test that a zero-sized memfd is preserved across live update.
+ */
+TEST(zero_memfd)
+{
+	int zero_fd, session;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+	struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+	switch (stage) {
+	case 1:

Helper functions? And in other tests that do save and restore too?

Otherwise

Quoted 3 lines
tools/testing/selftests/liveupdate/luo_memfd.c +3 -0
+		session = luo_create_session(luo_fd, ZERO_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#13 lore raw

PATCH 5/6 selftests/liveupdate: add test for operations on a preserved memfd

In reply to Pratyush Yadav

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

Once a memfd is preserved, certain operations are not allowed to succeed since they might make the state of the memfd inconsistent with the serialized state. Among these operations are truncating or growing the memfd. Writes and reads to already existing memfd should succeed.

Add a test that makes sure a preserved memfd does not allow growing or shrinking, but does allow reads and writes to existing memory to go thorough.

---
 .../testing/selftests/liveupdate/luo_memfd.c  | 57 +++++++++++++++++++
 1 file changed, 57 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +57 -0
index 56106cd09978..75f88101e7b5 100644
@@ -39,6 +39,10 @@
 #define ZERO_SESSION_NAME "zero_session"
 #define ZERO_MEMFD_TOKEN 1
 
+#define PRESERVED_SESSION_NAME "preserved_session"
+#define PRESERVED_MEMFD_TOKEN 1
+#define PRESERVED_BUFFER_SIZE SZ_1M
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
@@ -136,6 +140,59 @@ TEST(zero_memfd)
 	}
 }
 
+/*
+ * Test that preserved memfd can't grow or shrink, but reads and writes still
+ * work.
+ */
+TEST(preserved_ops)
+{
+	char write_buffer[128] = {'A'};
+	int fd, session;
+	char *buffer;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+
+	if (stage != 1)
+		SKIP(return, "test only expected to run on stage 1");
+
+	buffer = malloc(PRESERVED_BUFFER_SIZE);
+	ASSERT_NE(buffer, NULL);
+
+	session = luo_create_session(luo_fd, PRESERVED_SESSION_NAME);
+	ASSERT_GE(session, 0);
+
+	fd = create_random_memfd("preserved_memfd", buffer, PRESERVED_BUFFER_SIZE);
+	ASSERT_GE(fd, 0);
+
+	preserve_arg.fd = fd;
+	preserve_arg.token = PRESERVED_MEMFD_TOKEN;
+	ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), 0);
+
+	/*
+	 * Write to the preserved memfd (within existing size). This should
+	 * work.
+	 */
+	ASSERT_GE(lseek(fd, 0, SEEK_SET), 0);
+	/* Write buffer is smaller than total file size. */
+	ASSERT_EQ(write_size(fd, write_buffer, sizeof(write_buffer)), 0);
+	ASSERT_EQ(verify_fd_content(fd, write_buffer, sizeof(write_buffer)), 0);
+
+	/* Try to grow the file using write(). */
+
+	/* First, seek to one byte behind initial size. */
+	ASSERT_GE(lseek(fd, PRESERVED_BUFFER_SIZE - 1, SEEK_SET), 0);
+
+	/*
+	 * Then, write some data that should increase the file size. This should
+	 * fail.
+	 */
+	ASSERT_LT(write_size(fd, write_buffer, sizeof(write_buffer)), 0);
+	ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE);
+
+	/* Try to shrink the file using truncate. This should also fail. */
+	ASSERT_LT(ftruncate(fd, PRESERVED_BUFFER_SIZE / 2), 0);
+	ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE);
+}
+
 int main(int argc, char *argv[])
 {
 	int session, expected_stage = 0;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer Reviewed-by on v1 5/6
#14 lore raw

Re: [PATCH 5/6] selftests/liveupdate: add test for operations on a preserved memfd

Quoted 7 lines · Pratyush Yadav

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

Once a memfd is preserved, certain operations are not allowed to succeed since they might make the state of the memfd inconsistent with the serialized state. Among these operations are truncating or growing the memfd. Writes and reads to already existing memfd should succeed.

Quoted 6 lines

Add a test that makes sure a preserved memfd does not allow growing or shrinking, but does allow reads and writes to existing memory to go thorough.

Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#15 lore raw

PATCH 6/6 selftests/liveupdate: add fallocate test for memfd

In reply to Pratyush Yadav

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

When memory is added to a memfd via fallocate(), it does not get zeroed immediately. This is tracked by the absence of the uptodate folio flag. Initially, memfd preservation simply saved the folio flags at preserve time. This led to a bug, where all writes to un-initialized fallocated memory after preserve were lost after live update. This is fixed by patch [0] (not in mainline as of writing this).

Add a test that fallocates some memory in a memfd, preserves it, writes to it. Then in stage 2 it verifies the written content is still present.

[0] https://lore.kernel.org/linux-mm/[email protected]/

---
 .../testing/selftests/liveupdate/luo_memfd.c  | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +64 -0
index 75f88101e7b5..dde3c78db50e 100644
@@ -43,6 +43,11 @@
 #define PRESERVED_MEMFD_TOKEN 1
 #define PRESERVED_BUFFER_SIZE SZ_1M
 
+#define FALLOCATE_SESSION_NAME "fallocate_session"
+#define FALLOCATE_MEMFD_TOKEN 1
+#define FALLOCATE_BUFFER_SIZE SZ_1M
+#define RANDOM_DATA_FILE_FALLOCATE "luo_random_data_fallocate.bin"
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
@@ -193,6 +198,65 @@ TEST(preserved_ops)
 	ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE);
 }
 
+/*
+ * Test that an fallocated memfd is preserved across live update and can be
+ * written to after being preserved.
+ */
+TEST(fallocate_memfd)
+{
+	int fd, session;
+	char *buffer;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+	struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+	buffer = malloc(FALLOCATE_BUFFER_SIZE);
+	ASSERT_NE(buffer, NULL);
+
+	switch (stage) {
+	case 1:
+		session = luo_create_session(luo_fd, FALLOCATE_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
+		fd = memfd_create("fallocate_memfd", 0);
+		ASSERT_GE(fd, 0);
+
+		/* Fallocate memory but do not write to it yet */
+		ASSERT_EQ(fallocate(fd, 0, 0, FALLOCATE_BUFFER_SIZE), 0);
+
+		preserve_arg.fd = fd;
+		preserve_arg.token = FALLOCATE_MEMFD_TOKEN;
+		ASSERT_GE(ioctl(session, LIVEUPDATE_SESSION_PRESERVE_FD, &preserve_arg), 0);
+
+		/* Now write to it after preserving */
+		ASSERT_GE(generate_random_data(buffer, FALLOCATE_BUFFER_SIZE), 0);
+		ASSERT_EQ(save_test_data(RANDOM_DATA_FILE_FALLOCATE, buffer, FALLOCATE_BUFFER_SIZE), 0);
+
+		ASSERT_GE(lseek(fd, 0, SEEK_SET), 0);
+		ASSERT_EQ(write_size(fd, buffer, FALLOCATE_BUFFER_SIZE), 0);
+
+		daemonize_and_wait();
+		break;
+	case 2:
+		session = luo_retrieve_session(luo_fd, FALLOCATE_SESSION_NAME);
+		ASSERT_GE(session, 0);
+
+		ASSERT_EQ(load_test_data(RANDOM_DATA_FILE_FALLOCATE, buffer, FALLOCATE_BUFFER_SIZE), 0);
+
+		retrieve_arg.token = FALLOCATE_MEMFD_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(fd, buffer, FALLOCATE_BUFFER_SIZE), 0);
+
+		ASSERT_EQ(luo_session_finish(session), 0);
+		break;
+	default:
+		TH_LOG("Unknown stage %d\n", stage);
+		ASSERT_FALSE(true);
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	int session, expected_stage = 0;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer Reviewed-by on v1 6/6
#16 lore raw

Re: [PATCH 6/6] selftests/liveupdate: add fallocate test for memfd

Quoted 9 lines · Pratyush Yadav

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

When memory is added to a memfd via fallocate(), it does not get zeroed immediately. This is tracked by the absence of the uptodate folio flag. Initially, memfd preservation simply saved the folio flags at preserve time. This led to a bug, where all writes to un-initialized fallocated memory after preserve were lost after live update. This is fixed by patch [0] (not in mainline as of writing this).

Quoted 4 lines

Add a test that fallocates some memory in a memfd, preserves it, writes to it. Then in stage 2 it verifies the written content is still present.

[0] https://lore.kernel.org/linux-mm/[email protected]/

Looks like it's already in mm-stable, so it's sha should be stable already.

Quoted 10 lines
---
 .../testing/selftests/liveupdate/luo_memfd.c  | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +0 -0
index 75f88101e7b5..dde3c78db50e 100644

tools/testing/selftests/liveupdate/luo_memfd.c line 49

Quoted 7 lines
tools/testing/selftests/liveupdate/luo_memfd.c +4 -0
@@ -43,6 +43,11 @@
 #define PRESERVED_MEMFD_TOKEN 1
 #define PRESERVED_BUFFER_SIZE SZ_1M
 
+#define FALLOCATE_SESSION_NAME "fallocate_session"
+#define FALLOCATE_MEMFD_TOKEN 1
+#define FALLOCATE_BUFFER_SIZE SZ_1M
+#define RANDOM_DATA_FILE_FALLOCATE "luo_random_data_fallocate.bin"

FILESYSTEM_COPY_ or something like that?

Quoted 14 lines
tools/testing/selftests/liveupdate/luo_memfd.c +7 -0
+
 #define LIVEUPDATE_DEV "/dev/liveupdate"
 static int luo_fd = -1, stage;
 
@@ -193,6 +198,65 @@ TEST(preserved_ops)
 	ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE);
 }
 
+/*
+ * Test that an fallocated memfd is preserved across live update and can be
+ * written to after being preserved.
+ */
+TEST(fallocate_memfd)
+{

tools/testing/selftests/liveupdate/luo_memfd.c TEST(preserved_ops) · line 216

Quoted 10 lines
tools/testing/selftests/liveupdate/luo_memfd.c +10 -0
+	int fd, session;
+	char *buffer;
+	struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+	struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+	buffer = malloc(FALLOCATE_BUFFER_SIZE);
+	ASSERT_NE(buffer, NULL);
+
+	switch (stage) {
+	case 1:

Functions again :)

Signature · 2 lines
Sincerely yours,
Mike.