* [PATCH v3 1/6] selftests/liveupdate: add framework for memfd tests
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 2/6] selftests/liveupdate: add helper functions " Pratyush Yadav
` (4 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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().
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
tools/testing/selftests/liveupdate/Makefile | 1 +
.../testing/selftests/liveupdate/luo_memfd.c | 78 +++++++++++++++++++
2 files changed, 79 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..c1f3275963dc
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2026, Google LLC.
+ * Pratyush Yadav (Google) <pratyush@kernel.org>
+ */
+
+/*
+ * 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
+
+static int luo_fd = -1;
+static int stage;
+
+int main(int argc, char *argv[])
+{
+ int expected_stage = 0;
+ int session;
+
+ /*
+ * 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) device. Is LUO enabled?\n",
+ LUO_DEVICE, strerror(errno));
+
+ session = luo_retrieve_session(luo_fd, STATE_SESSION_NAME);
+ if (session == -ENOENT)
+ stage = 1;
+ else if (session >= 0)
+ stage = 2;
+ else
+ ksft_exit_fail_perror("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);
+
+ return test_harness_run(argc, argv);
+}
--
2.53.0.1213.gd9a14994de-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 1/6] selftests/liveupdate: add framework for " Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
2026-04-04 17:53 ` Zhu Yanjun
2026-04-04 10:24 ` [PATCH v3 3/6] selftests/liveupdate: add test for memfd content preservation Pratyush Yadav
` (3 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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.
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
.../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++-
.../selftests/liveupdate/luo_test_utils.h | 10 +
2 files changed, 200 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c b/tools/testing/selftests/liveupdate/luo_test_utils.c
index 3c8721c505df..ceb918ef9813 100644
--- a/tools/testing/selftests/liveupdate/luo_test_utils.c
+++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Copyright (c) 2025, Google LLC.
+ * Copyright (c) 2025-2026, Google LLC.
* Pasha Tatashin <pasha.tatashin@soleen.com>
+ * Pratyush Yadav (Google) <pratyush@kernel.org>
+ *
+ * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
+ * Pratyush Yadav <ptyadav@amazon.de>
*/
#define _GNU_SOURCE
@@ -20,9 +24,194 @@
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
#include "luo_test_utils.h"
+int cwd_is_tmpfs(void)
+{
+ struct statfs buf;
+
+ if (statfs(".", &buf) < 0)
+ return -errno;
+
+ return buf.f_type == TMPFS_MAGIC;
+}
+
+/* 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) {
+ if (errno == EINTR)
+ continue;
+ else
+ return -errno;
+ }
+
+ remain -= bytes_read;
+ buffer += 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) {
+ if (errno == EINTR)
+ continue;
+ else
+ return -errno;
+ }
+
+ remain -= written;
+ buffer += 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 | O_TRUNC, 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;
+ }
+
+ ret = write_size(fd, buffer, size);
+ if (ret < 0) {
+ close(fd);
+ return ret;
+ }
+
+ /* Reset file position to beginning */
+ if (lseek(fd, 0, SEEK_SET) < 0) {
+ ret = -errno;
+ close(fd);
+ return ret;
+ }
+
+ 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_read(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;
+ munmap(mapped_mem, size);
+ return ret;
+}
+
int luo_open_device(void)
{
return open(LUO_DEVICE, O_RDWR);
diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.h b/tools/testing/selftests/liveupdate/luo_test_utils.h
index 90099bf49577..a69dce2649b0 100644
--- a/tools/testing/selftests/liveupdate/luo_test_utils.h
+++ b/tools/testing/selftests/liveupdate/luo_test_utils.h
@@ -35,6 +35,16 @@ void restore_and_read_stage(int state_session_fd, int token, int *stage);
void daemonize_and_wait(void);
+int cwd_is_tmpfs(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_read(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);
--
2.53.0.1213.gd9a14994de-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
2026-04-04 10:24 ` [PATCH v3 2/6] selftests/liveupdate: add helper functions " Pratyush Yadav
@ 2026-04-04 17:53 ` Zhu Yanjun
2026-04-05 7:34 ` Pratyush Yadav
0 siblings, 1 reply; 10+ messages in thread
From: Zhu Yanjun @ 2026-04-04 17:53 UTC (permalink / raw)
To: Pratyush Yadav, Pasha Tatashin, Mike Rapoport, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
在 2026/4/4 3:24, Pratyush Yadav 写道:
> From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
>
> 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.
>
> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
> Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
> ---
> .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++-
> .../selftests/liveupdate/luo_test_utils.h | 10 +
> 2 files changed, 200 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c b/tools/testing/selftests/liveupdate/luo_test_utils.c
> index 3c8721c505df..ceb918ef9813 100644
> --- a/tools/testing/selftests/liveupdate/luo_test_utils.c
> +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
> @@ -1,8 +1,12 @@
> // SPDX-License-Identifier: GPL-2.0-only
>
> /*
> - * Copyright (c) 2025, Google LLC.
> + * Copyright (c) 2025-2026, Google LLC.
> * Pasha Tatashin <pasha.tatashin@soleen.com>
> + * Pratyush Yadav (Google) <pratyush@kernel.org>
> + *
> + * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
> + * Pratyush Yadav <ptyadav@amazon.de>
> */
>
> #define _GNU_SOURCE
> @@ -20,9 +24,194 @@
> #include <sys/stat.h>
> #include <errno.h>
> #include <stdarg.h>
> +#include <sys/vfs.h>
> +#include <linux/magic.h>
>
> #include "luo_test_utils.h"
>
> +int cwd_is_tmpfs(void)
> +{
> + struct statfs buf;
> +
> + if (statfs(".", &buf) < 0)
> + return -errno;
> +
> + return buf.f_type == TMPFS_MAGIC;
> +}
> +
> +/* 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) {
while (remain > 0) ?
> + bytes_read = read(fd, buffer, remain);
> + if (bytes_read == 0)
> + return -ENODATA;
> + if (bytes_read < 0) {
> + if (errno == EINTR)
> + continue;
> + else
> + return -errno;
> + }
> +
> + remain -= bytes_read;
> + buffer += 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) {
while (remain>0) is better?
Zhu Yanjun
> + written = write(fd, buffer, remain);
> + if (written == 0)
> + return -EIO;
> + if (written < 0) {
> + if (errno == EINTR)
> + continue;
> + else
> + return -errno;
> + }
> +
> + remain -= written;
> + buffer += 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 | O_TRUNC, 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;
> + }
> +
> + ret = write_size(fd, buffer, size);
> + if (ret < 0) {
> + close(fd);
> + return ret;
> + }
> +
> + /* Reset file position to beginning */
> + if (lseek(fd, 0, SEEK_SET) < 0) {
> + ret = -errno;
> + close(fd);
> + return ret;
> + }
> +
> + 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_read(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;
> + munmap(mapped_mem, size);
> + return ret;
> +}
> +
> int luo_open_device(void)
> {
> return open(LUO_DEVICE, O_RDWR);
> diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.h b/tools/testing/selftests/liveupdate/luo_test_utils.h
> index 90099bf49577..a69dce2649b0 100644
> --- a/tools/testing/selftests/liveupdate/luo_test_utils.h
> +++ b/tools/testing/selftests/liveupdate/luo_test_utils.h
> @@ -35,6 +35,16 @@ void restore_and_read_stage(int state_session_fd, int token, int *stage);
>
> void daemonize_and_wait(void);
>
> +int cwd_is_tmpfs(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_read(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);
>
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
2026-04-04 17:53 ` Zhu Yanjun
@ 2026-04-05 7:34 ` Pratyush Yadav
2026-04-05 16:30 ` Zhu Yanjun
0 siblings, 1 reply; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-05 7:34 UTC (permalink / raw)
To: Zhu Yanjun
Cc: Pratyush Yadav, Pasha Tatashin, Mike Rapoport, Shuah Khan,
Andrew Morton, Usama Arif, linux-kernel, linux-kselftest,
linux-mm
On Sat, Apr 04 2026, Zhu Yanjun wrote:
> 在 2026/4/4 3:24, Pratyush Yadav 写道:
>> From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
>> 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.
>> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
>> Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
>> Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
>> ---
>> .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++-
>> .../selftests/liveupdate/luo_test_utils.h | 10 +
>> 2 files changed, 200 insertions(+), 1 deletion(-)
>> diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c
>> b/tools/testing/selftests/liveupdate/luo_test_utils.c
>> index 3c8721c505df..ceb918ef9813 100644
>> --- a/tools/testing/selftests/liveupdate/luo_test_utils.c
>> +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
>> @@ -1,8 +1,12 @@
>> // SPDX-License-Identifier: GPL-2.0-only
>> /*
>> - * Copyright (c) 2025, Google LLC.
>> + * Copyright (c) 2025-2026, Google LLC.
>> * Pasha Tatashin <pasha.tatashin@soleen.com>
>> + * Pratyush Yadav (Google) <pratyush@kernel.org>
>> + *
>> + * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
>> + * Pratyush Yadav <ptyadav@amazon.de>
>> */
>> #define _GNU_SOURCE
>> @@ -20,9 +24,194 @@
>> #include <sys/stat.h>
>> #include <errno.h>
>> #include <stdarg.h>
>> +#include <sys/vfs.h>
>> +#include <linux/magic.h>
>> #include "luo_test_utils.h"
>> +int cwd_is_tmpfs(void)
>> +{
>> + struct statfs buf;
>> +
>> + if (statfs(".", &buf) < 0)
>> + return -errno;
>> +
>> + return buf.f_type == TMPFS_MAGIC;
>> +}
>> +
>> +/* 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) {
>
> while (remain > 0) ?
I don't think it is needed. bytes_read will always be smaller than or
equal to remain, so remain will never go below 0 anyway. And while
(remain) is nicer to read.
>
>> + bytes_read = read(fd, buffer, remain);
>> + if (bytes_read == 0)
>> + return -ENODATA;
>> + if (bytes_read < 0) {
>> + if (errno == EINTR)
>> + continue;
>> + else
>> + return -errno;
>> + }
>> +
>> + remain -= bytes_read;
>> + buffer += bytes_read;
>> + }
>> +
>> + return 0;
>> +}
>> +
[...]
--
Regards,
Pratyush Yadav
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
2026-04-05 7:34 ` Pratyush Yadav
@ 2026-04-05 16:30 ` Zhu Yanjun
0 siblings, 0 replies; 10+ messages in thread
From: Zhu Yanjun @ 2026-04-05 16:30 UTC (permalink / raw)
To: Pratyush Yadav
Cc: Pasha Tatashin, Mike Rapoport, Shuah Khan, Andrew Morton,
Usama Arif, linux-kernel, linux-kselftest, linux-mm
在 2026/4/5 0:34, Pratyush Yadav 写道:
> On Sat, Apr 04 2026, Zhu Yanjun wrote:
>
>> 在 2026/4/4 3:24, Pratyush Yadav 写道:
>>> From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
>>> 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.
>>> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
>>> Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
>>> Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
>>> ---
>>> .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++-
>>> .../selftests/liveupdate/luo_test_utils.h | 10 +
>>> 2 files changed, 200 insertions(+), 1 deletion(-)
>>> diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c
>>> b/tools/testing/selftests/liveupdate/luo_test_utils.c
>>> index 3c8721c505df..ceb918ef9813 100644
>>> --- a/tools/testing/selftests/liveupdate/luo_test_utils.c
>>> +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
>>> @@ -1,8 +1,12 @@
>>> // SPDX-License-Identifier: GPL-2.0-only
>>> /*
>>> - * Copyright (c) 2025, Google LLC.
>>> + * Copyright (c) 2025-2026, Google LLC.
>>> * Pasha Tatashin <pasha.tatashin@soleen.com>
>>> + * Pratyush Yadav (Google) <pratyush@kernel.org>
>>> + *
>>> + * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
>>> + * Pratyush Yadav <ptyadav@amazon.de>
>>> */
>>> #define _GNU_SOURCE
>>> @@ -20,9 +24,194 @@
>>> #include <sys/stat.h>
>>> #include <errno.h>
>>> #include <stdarg.h>
>>> +#include <sys/vfs.h>
>>> +#include <linux/magic.h>
>>> #include "luo_test_utils.h"
>>> +int cwd_is_tmpfs(void)
>>> +{
>>> + struct statfs buf;
>>> +
>>> + if (statfs(".", &buf) < 0)
>>> + return -errno;
>>> +
>>> + return buf.f_type == TMPFS_MAGIC;
>>> +}
>>> +
>>> +/* 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) {
>> while (remain > 0) ?
> I don't think it is needed. bytes_read will always be smaller than or
> equal to remain, so remain will never go below 0 anyway. And while
> (remain) is nicer to read.
If a wrong number is transferred, this makes remain less than 0. Then
this loop will run again and again.
This while (remain > 0) will avoid this kind of scenario.
ZhuYanjun
>
>>> + bytes_read = read(fd, buffer, remain);
>>> + if (bytes_read == 0)
>>> + return -ENODATA;
>>> + if (bytes_read < 0) {
>>> + if (errno == EINTR)
>>> + continue;
>>> + else
>>> + return -errno;
>>> + }
>>> +
>>> + remain -= bytes_read;
>>> + buffer += bytes_read;
>>> + }
>>> +
>>> + return 0;
>>> +}
>>> +
> [...]
>
--
Best Regards,
Yanjun.Zhu
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3 3/6] selftests/liveupdate: add test for memfd content preservation
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 1/6] selftests/liveupdate: add framework for " Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 2/6] selftests/liveupdate: add helper functions " Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 4/6] selftests/liveupdate: add test for zero-size memfd preservation Pratyush Yadav
` (2 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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 c1f3275963dc..068c7f30c4de 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
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v3 4/6] selftests/liveupdate: add test for zero-size memfd preservation
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
` (2 preceding siblings ...)
2026-04-04 10:24 ` [PATCH v3 3/6] selftests/liveupdate: add test for memfd content preservation Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 5/6] selftests/liveupdate: add test for operations on a preserved memfd Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 6/6] selftests/liveupdate: add fallocate test for memfd Pratyush Yadav
5 siblings, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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 is 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.
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
.../testing/selftests/liveupdate/luo_memfd.c | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing/selftests/liveupdate/luo_memfd.c
index 068c7f30c4de..2889a21523fd 100644
--- a/tools/testing/selftests/liveupdate/luo_memfd.c
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -36,6 +36,9 @@
#define MEMFD_DATA_BUFFER_SIZE SZ_1M
#define MEMFD_DATA_FS_COPY "memfd_data_fs_copy.bin"
+#define ZERO_SESSION_NAME "zero_session"
+#define ZERO_MEMFD_TOKEN 1
+
static int luo_fd = -1;
static int stage;
@@ -108,6 +111,60 @@ TEST(memfd_data)
}
}
+static void zero_memfd_stage_1(struct __test_metadata *_metadata)
+{
+ int zero_fd, session;
+ struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+
+ 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);
+
+ daemonize_and_wait();
+}
+
+static void zero_memfd_stage_2(struct __test_metadata *_metadata)
+{
+ int zero_fd, session;
+ struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+ 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);
+}
+
+/*
+ * Test that a zero-sized memfd is preserved across live update.
+ */
+TEST(zero_memfd)
+{
+ switch (stage) {
+ case 1:
+ zero_memfd_stage_1(_metadata);
+ break;
+ case 2:
+ zero_memfd_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
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v3 5/6] selftests/liveupdate: add test for operations on a preserved memfd
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
` (3 preceding siblings ...)
2026-04-04 10:24 ` [PATCH v3 4/6] selftests/liveupdate: add test for zero-size memfd preservation Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
2026-04-04 10:24 ` [PATCH v3 6/6] selftests/liveupdate: add fallocate test for memfd Pratyush Yadav
5 siblings, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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.
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Pratyush Yadav <ptyadav@amazon.de>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
.../testing/selftests/liveupdate/luo_memfd.c | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing/selftests/liveupdate/luo_memfd.c
index 2889a21523fd..661a7c922e9d 100644
--- a/tools/testing/selftests/liveupdate/luo_memfd.c
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -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
+
static int luo_fd = -1;
static int stage;
@@ -165,6 +169,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_read(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 expected_stage = 0;
--
2.53.0.1213.gd9a14994de-goog
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH v3 6/6] selftests/liveupdate: add fallocate test for memfd
2026-04-04 10:24 [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pratyush Yadav
` (4 preceding siblings ...)
2026-04-04 10:24 ` [PATCH v3 5/6] selftests/liveupdate: add test for operations on a preserved memfd Pratyush Yadav
@ 2026-04-04 10:24 ` Pratyush Yadav
5 siblings, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2026-04-04 10:24 UTC (permalink / raw)
To: Pasha Tatashin, Mike Rapoport, Pratyush Yadav, Shuah Khan,
Andrew Morton, Usama Arif
Cc: linux-kernel, linux-kselftest, linux-mm
From: "Pratyush Yadav (Google)" <pratyush@kernel.org>
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
commit 50d7b4332f27 ("mm: memfd_luo: always make all folios uptodate").
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.
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Pratyush Yadav (Google) <pratyush@kernel.org>
---
.../testing/selftests/liveupdate/luo_memfd.c | 83 +++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git a/tools/testing/selftests/liveupdate/luo_memfd.c b/tools/testing/selftests/liveupdate/luo_memfd.c
index 661a7c922e9d..4a90044ecf1d 100644
--- a/tools/testing/selftests/liveupdate/luo_memfd.c
+++ b/tools/testing/selftests/liveupdate/luo_memfd.c
@@ -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 FALLOCATE_DATA_FS_COPY "fallocate_data_fs_copy.bin"
+
static int luo_fd = -1;
static int stage;
@@ -222,6 +227,84 @@ TEST(preserved_ops)
ASSERT_EQ(lseek(fd, 0, SEEK_END), PRESERVED_BUFFER_SIZE);
}
+static void fallocate_memfd_stage_1(struct __test_metadata *_metadata)
+{
+ int fd, session;
+ char *buffer;
+ struct liveupdate_session_preserve_fd preserve_arg = { .size = sizeof(preserve_arg) };
+
+ buffer = malloc(FALLOCATE_BUFFER_SIZE);
+ ASSERT_NE(buffer, NULL);
+
+ 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(FALLOCATE_DATA_FS_COPY, 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();
+}
+
+static void fallocate_memfd_stage_2(struct __test_metadata *_metadata)
+{
+ int fd, session;
+ char *buffer;
+ struct liveupdate_session_retrieve_fd retrieve_arg = { .size = sizeof(retrieve_arg) };
+
+ buffer = malloc(FALLOCATE_BUFFER_SIZE);
+ ASSERT_NE(buffer, NULL);
+
+ session = luo_retrieve_session(luo_fd, FALLOCATE_SESSION_NAME);
+ ASSERT_GE(session, 0);
+
+ ASSERT_EQ(load_test_data(FALLOCATE_DATA_FS_COPY, 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_read(fd, buffer, FALLOCATE_BUFFER_SIZE), 0);
+
+ ASSERT_EQ(luo_session_finish(session), 0);
+}
+
+/*
+ * Test that an fallocated memfd is preserved across live update and can be
+ * written to after being preserved.
+ */
+TEST(fallocate_memfd)
+{
+ if (cwd_is_tmpfs())
+ SKIP(return, "test saves data to rootfs, cannot run on tmpfs");
+
+ switch (stage) {
+ case 1:
+ fallocate_memfd_stage_1(_metadata);
+ break;
+ case 2:
+ fallocate_memfd_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
^ permalink raw reply related [flat|nested] 10+ messages in thread