selftests/liveupdate: add memfd tests
Subsystems (3)
Pratyush Yadav
maintainerv3 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]/
Changes in v3:
- Retry on EINTR instead of giving up in read_size() and write_size().
- Use O_TRUNC in save_test_data().
- Return the value of write_size() directly in create_random_memfd(). No need to check errno since write_size() already returns -errno.
- Save errno before calling close() in create_random_memfd() to avoid overwriting the value.
- Use memcmp() instead of open-coding the comparison loop.
- Return value of test_harness_run() from main().
Changes in v2:
- Drop LIVEUPDATE_DEV since it isn't used.
- Split luo_fd and stage declaration on separate lines.
- Split all 2-stage tests into helper function for each test.
- Fix wording and argument order when LUO_DEVICE fails to open.
- Rename verify_fd_content() to verify_fd_content_read().
- Advance buffer pointer in {read,write}_size().
- Add a check to make sure current working directory is not on a tmpfs mount. If so, skip the tests since saved data files will be lost.
- Rename RANDOM_DATA_FILE and RANDOM_DATA_FILE_FALLOCATE to MEMFD_DATA_FS_COPY and FALLOCATE_DATA_FS_COPY respectively. Also update the file names to reflect this naming convention.
- Collect R-bys.
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 | 355 ++++++++++++++++++ .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 4 files changed, 557 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.c
base-commit: 98ddd87f9704ef2fc837ec2ca38877c364d8dada
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerPATCH v3 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 | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.c
tools/testing/selftests/liveupdate/Makefile +1 -0
@@ -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 +78 -0
78 lines not on this page · raw message
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerPATCH v3 2/6 selftests/liveupdate: add helper functions for memfd tests
In reply to Pratyush YadavFrom: "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 | 191 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 200 insertions(+), 1 deletion(-)
tools/testing/selftests/liveupdate/luo_test_utils.c +190 -1
@@ -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
@@ -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 );
tools/testing/selftests/liveupdate/luo_test_utils.h +10 -0
@@ -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);
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
Quoted 48 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.
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>Signed-off-by Pratyush Yadav (Google) <[email protected]>--- .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 200 insertions(+), 1 deletion(-)tools/testing/selftests/liveupdate/luo_test_utils.c +14
-1@@ -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@@ -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; +
tools/testing/selftests/liveupdate/luo_test_utils.c line 48
Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10
-0+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) ?
Quoted 14 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +14
-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; + } +
tools/testing/selftests/liveupdate/luo_test_utils.c line 72
Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10
-0+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
Quoted 166 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +142
-0+ 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 );tools/testing/selftests/liveupdate/luo_test_utils.h +10
-0@@ -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);
Pratyush Yadav
maintainerRe: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
Quoted 47 lines · Zhu Yanjun
Quoted 46 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.Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>Signed-off-by Pratyush Yadav (Google) <[email protected]>--- .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 200 insertions(+), 1 deletion(-)tools/testing/selftests/liveupdate/luo_test_utils.c +17
-1@@ -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@@ -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. */
Quoted 8 lines
tools/testing/selftests/liveupdate/luo_test_utils.c line 48
Quoted 6 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +6
-0+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.
Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +17
-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 ; +} +
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
Quoted 56 lines · Pratyush Yadav
Quoted 46 lines · Zhu Yanjun
Quoted 45 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.Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>Signed-off-by Pratyush Yadav (Google) <[email protected]>--- .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 200 insertions(+), 1 deletion(-)tools/testing/selftests/liveupdate/luo_test_utils.c +16
-1@@ -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@@ -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 ; +} +Quoted 8 lines
tools/testing/selftests/liveupdate/luo_test_utils.c line 48
Quoted 7 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +7
-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; + +while (remain) {while (remain > 0) ?
Quoted 3 lines
I don't think it is needed.
bytes_readwill 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
Quoted 20 lines
Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +17
-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 ; +} +[...]
Signature · 2 lines
Best Regards, Yanjun.Zhu
Pratyush Yadav
maintainerRe: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
Quoted 57 lines · Zhu Yanjun
Quoted 56 lines · Pratyush Yadav
Quoted 54 lines · Zhu Yanjun
Quoted 42 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.Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Signed-off-by Pratyush Yadav <[email protected]>Signed-off-by Pratyush Yadav (Google) <[email protected]>--- .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 2 files changed, 200 insertions(+), 1 deletion(-)tools/testing/selftests/liveupdate/luo_test_utils.c +13
-1@@ -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@@ -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; +tools/testing/selftests/liveupdate/luo_test_utils.c line 46
Quoted 10 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +10
-0+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) ?
Quoted 6 lines
Quoted 3 lines
I don't think it is needed.
bytes_readwill 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.
That can only happen if read() returns more then the requested number of bytes. That is, if bytes_read > remain. And read() will never do so since that can be a buffer overflow and would break userspace left and right.
So, I don't see how this is a problem.
Quoted 25 lines
This while (remain > 0) will avoid this kind of scenario.
ZhuYanjun
Quoted 20 lines
Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/liveupdate/luo_test_utils.c +17
-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 ; +} +[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pratyush Yadav
maintainerPATCH v3 3/6 selftests/liveupdate: add test for memfd content preservation
In reply to Pratyush YadavFrom: "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 | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+)
tools/testing/selftests/liveupdate/Makefile +1 -0
@@ -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 +80 -0
106 lines not on this page · raw message
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerPATCH v3 4/6 selftests/liveupdate: add test for zero-size memfd preservation
In reply to Pratyush YadavFrom: "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 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.
--- .../testing/selftests/liveupdate/luo_memfd.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +57 -0
@@ -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 ;
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerPATCH v3 5/6 selftests/liveupdate: add test for operations on a preserved memfd
In reply to Pratyush YadavFrom: "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
@@ -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 ;
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerPATCH v3 6/6 selftests/liveupdate: add fallocate test for memfd
In reply to Pratyush YadavFrom: "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 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.
--- .../testing/selftests/liveupdate/luo_memfd.c | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+)
tools/testing/selftests/liveupdate/luo_memfd.c +83 -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 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 ;
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Pasha Tatashin
maintainerQuoted 8 lines · Pratyush Yadav
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_simpleorluo_multi_session. Add a dedicated test suite for it.
Quoted 5 lines
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.
Here are few observations that I noticed when I tried to run your tests:
1. The '-h' tells you nothing about --stage argument:
root@liveupdate-vm:~/liveupdate# ./luo_memfd -h Usage: ./luo_memfd [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] -h print help -l list all tests -d enable debug prints
-t name include test -T name exclude test -v name include variant -V name exclude variant -f name include fixture -F name exclude fixture -r name run specified test ...
2. '-l' does not work after you run stage1, do you keep /dev/liveupdate open? That is not needed, we only need to keep session open.
root@liveupdate-vm:~/liveupdate# ./luo_memfd -l 1..0 # SKIP Failed to open /dev/liveupdate (Device or resource busy) device. Is LUO enabled?
3. Stage 1 has proper [STAGE 1] prefix, but no [STAGE 2] prefix for stage 2:
# Starting 4 tests from 1 test cases.
# RUN global.memfd_data ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 245. Resources are pinned.
# [STAGE 1] You may now perform kexec reboot.
# OK global.memfd_data
ok 1 global.memfd_data
# RUN global.zero_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 247. Resources are pinned.
# [STAGE 1] You may now perform kexec reboot.
# OK global.zero_memfd
ok 2 global.zero_memfd
# RUN global.preserved_ops ...
# OK global.preserved_ops
ok 3 global.preserved_ops
# RUN global.fallocate_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 250. Resources are pinned.
# [STAGE 1] You may now perform kexec reboot.
# OK global.fallocate_memfd
ok 4 global.fallocate_memfd
# PASSED: 4 / 4 tests passed.
# Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0
./do_kexec
root@liveupdate-vm:~/liveupdate# ./luo_memfd TAP version 13 1..4 # Starting 4 tests from 1 test cases. # RUN global.memfd_data ... # OK global.memfd_data ok 1 global.memfd_data # RUN global.zero_memfd ... # OK global.zero_memfd ok 2 global.zero_memfd # RUN global.preserved_ops ... # SKIP test only expected to run on stage 1 # OK global.preserved_ops ok 3 global.preserved_ops # SKIP test only expected to run on stage 1 # RUN global.fallocate_memfd ... # OK global.fallocate_memfd ok 4 global.fallocate_memfd # PASSED: 4 / 4 tests passed. # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:1 error:0
4. I also do not like that we now have duplicated stage parsing code in luo_test(), perhaps we should add our own test_harness_run() variant that depends on stage, and use it in both current tests, and the new memfd tests.
Pasha
Quoted 48 lines
Changes in v3:
- Retry on EINTR instead of giving up inread_size()andwrite_size().
- UseO_TRUNCinsave_test_data().
- Return the value ofwrite_size()directly increate_random_memfd(). No need to check errno sincewrite_size()already returns -errno.
- Save errno before callingclose()increate_random_memfd()to avoid overwriting the value.
- Usememcmp()instead of open-coding the comparison loop.
- Return value oftest_harness_run()frommain().Changes in v2:
- DropLIVEUPDATE_DEVsince it isn't used.
- Splitluo_fdand stage declaration on separate lines.
- Split all 2-stage tests into helper function for each test.
- Fix wording and argument order whenLUO_DEVICEfails to open.
- Renameverify_fd_content()toverify_fd_content_read().
- Advance buffer pointer in {read,write}_size().
- Add a check to make sure current working directory is not on a tmpfs mount. If so, skip the tests since saved data files will be lost.
- RenameRANDOM_DATA_FILEandRANDOM_DATA_FILE_FALLOCATEtoMEMFD_DATA_FS_COPYandFALLOCATE_DATA_FS_COPYrespectively. Also update the file names to reflect this naming convention.
- Collect R-bys.Regards,
Pratyush YadavPratyush 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 memfdtools/testing/selftests/liveupdate/Makefile | 2 + .../testing/selftests/liveupdate/luo_memfd.c | 355 ++++++++++++++++++ .../selftests/liveupdate/luo_test_utils.c | 191 +++++++++- .../selftests/liveupdate/luo_test_utils.h | 10 + 4 files changed, 557 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/liveupdate/luo_memfd.cbase-commit:
98ddd87f9704ef2fc837ec2ca38877c364d8dadaSignature · 1 line
2.53.0.1213.gd9a14994de-goog
Pratyush Yadav
maintainerQuoted 25 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav
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_simpleorluo_multi_session. Add a dedicated test suite for it.Quoted 5 lines
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.
Here are few observations that I noticed when I tried to run your tests:
1. The '-h' tells you nothing about --stage argument:
root@liveupdate-vm:~/liveupdate# ./luo_memfd -h Usage: ./luo_memfd [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] -h print help -l list all tests -d enable debug prints
Quoted 8 lines
-t name include test -T name exclude test -v name include variant -V name exclude variant -f name include fixture -F name exclude fixture -r name run specified test ...
Yeah, unfortunately that is a side effect of using test_harness_run(), which does not know anything about the options specific to our test.
Quoted 3 lines
2. '-l' does not work after you run stage1, do you keep
/dev/liveupdateopen? That is not needed, we only need to keep session open.
Oh yeah, I keep forgetting that is no longer needed. The main process closes the FD but the forked daemons hold a reference. I can clean that up via a fixture.
Quoted 5 lines
root@liveupdate-vm:~/liveupdate# ./luo_memfd -l 1..0 # SKIP Failed to open /dev/liveupdate (Device or resource busy) device. Is LUO enabled?3. Stage 1 has proper [STAGE 1] prefix, but no [STAGE 2] prefix for
Because stage 2 has no prints, all the prints are coming from the selftest harness. Those same lines are also not prefixed in stage 1. If you'd like, I can add a print beforehand that shows which stage is running. Other than that, I don't see what else we can do. I don't want to modify the selftest harness.
Quoted 49 lines
stage 2:
# Starting 4 tests from 1 test cases.
# RUN global.memfd_data ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 245. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.memfd_data
ok 1 global.memfd_data
# RUN global.zero_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 247. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.zero_memfd
ok 2 global.zero_memfd
# RUN global.preserved_ops ...
# OK global.preserved_ops
ok 3 global.preserved_ops
# RUN global.fallocate_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 250. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.fallocate_memfd
ok 4 global.fallocate_memfd
# PASSED: 4 / 4 tests passed.
# Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0./do_kexec
root@liveupdate-vm:~/liveupdate# ./luo_memfd TAP version 13 1..4 # Starting 4 tests from 1 test cases. # RUN global.memfd_data ... # OK global.memfd_data ok 1 global.memfd_data # RUN global.zero_memfd ... # OK global.zero_memfd ok 2 global.zero_memfd # RUN global.preserved_ops ... # SKIP test only expected to run on stage 1 # OK global.preserved_ops ok 3 global.preserved_ops # SKIP test only expected to run on stage 1 # RUN global.fallocate_memfd ... # OK global.fallocate_memfd ok 4 global.fallocate_memfd # PASSED: 4 / 4 tests passed. # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:1 error:0
Quoted 4 lines
4. I also do not like that we now have duplicated stage parsing code in
luo_test(), perhaps we should add our owntest_harness_run()variant that depends on stage, and use it in both current tests, and the new memfd tests.
Sounds good in principle, but unfortunately ends up duplicating a lot of logic in test_harness_run() that is not a good idea IMO. We should work with the harness not fork off into our own.
I suppose we can refactor some of the logic there to split into functions that we can then use in our luo_test_harness_run(), but keeping the option parsing logic in sync is going to be difficult.
And for the duplicated logic, I agree. I thought about cleaning it up but was feeling lazy... Well now that you have called it out let me see what I can do.
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pasha Tatashin
maintainerQuoted 48 lines · Pratyush Yadav
Quoted 25 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav
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_simpleorluo_multi_session. Add a dedicated test suite for it.Quoted 5 lines
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.
Here are few observations that I noticed when I tried to run your tests:
1. The '-h' tells you nothing about --stage argument:
root@liveupdate-vm:~/liveupdate# ./luo_memfd -h Usage: ./luo_memfd [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] -h print help -l list all tests -d enable debug printsQuoted 8 lines
-t name include test -T name exclude test -v name include variant -V name exclude variant -f name include fixture -F name exclude fixture -r name run specified test ...Yeah, unfortunately that is a side effect of using
test_harness_run(), which does not know anything about the options specific to our test.Quoted 3 lines
2. '-l' does not work after you run stage1, do you keep
/dev/liveupdateopen? That is not needed, we only need to keep session open.Oh yeah, I keep forgetting that is no longer needed. The main process closes the FD but the forked daemons hold a reference. I can clean that up via a fixture.
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
root@liveupdate-vm:~/liveupdate# ./luo_memfd -l 1..0 # SKIP Failed to open /dev/liveupdate (Device or resource busy) device. Is LUO enabled?3. Stage 1 has proper [STAGE 1] prefix, but no [STAGE 2] prefix for
Because stage 2 has no prints, all the prints are coming from the selftest harness. Those same lines are also not prefixed in stage 1. If you'd like, I can add a print beforehand that shows which stage is
Yes, please, add a least one [STAGE 2] print.
Quoted 61 lines
running. Other than that, I don't see what else we can do. I don't want to modify the selftest harness.
Quoted 49 lines
stage 2:
# Starting 4 tests from 1 test cases.
# RUN global.memfd_data ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 245. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.memfd_data
ok 1 global.memfd_data
# RUN global.zero_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 247. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.zero_memfd
ok 2 global.zero_memfd
# RUN global.preserved_ops ...
# OK global.preserved_ops
ok 3 global.preserved_ops
# RUN global.fallocate_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 250. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.fallocate_memfd
ok 4 global.fallocate_memfd
# PASSED: 4 / 4 tests passed.
# Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0./do_kexec
root@liveupdate-vm:~/liveupdate# ./luo_memfd TAP version 13 1..4 # Starting 4 tests from 1 test cases. # RUN global.memfd_data ... # OK global.memfd_data ok 1 global.memfd_data # RUN global.zero_memfd ... # OK global.zero_memfd ok 2 global.zero_memfd # RUN global.preserved_ops ... # SKIP test only expected to run on stage 1 # OK global.preserved_ops ok 3 global.preserved_ops # SKIP test only expected to run on stage 1 # RUN global.fallocate_memfd ... # OK global.fallocate_memfd ok 4 global.fallocate_memfd # PASSED: 4 / 4 tests passed. # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:1 error:0Quoted 4 lines
4. I also do not like that we now have duplicated stage parsing code in
luo_test(), perhaps we should add our owntest_harness_run()variant that depends on stage, and use it in both current tests, and the new memfd tests.Sounds good in principle, but unfortunately ends up duplicating a lot of logic in
test_harness_run()that is not a good idea IMO. We should work with the harness not fork off into our own.
Quoted 7 lines
I suppose we can refactor some of the logic there to split into functions that we can then use in our
luo_test_harness_run(), but keeping the option parsing logic in sync is going to be difficult.And for the duplicated logic, I agree. I thought about cleaning it up but was feeling lazy... Well now that you have called it out let me see what I can do.
The main point is that the luo_sessions and luo_memfds tests should use a common framework, whether that's luo_test_harness_run() or the generic test_harness_run(). I don't have a definitive answer for this, so I recommend tinkering with it to see what works best.
Pasha
Quoted 6 lines
[...]
Signature · 2 lines
Regards, Pratyush Yadav
Pasha Tatashin
maintainerQuoted 52 lines · Pasha Tatashin
Quoted 51 lines · Pratyush Yadav
Quoted 25 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav
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_simpleorluo_multi_session. Add a dedicated test suite for it.Quoted 5 lines
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.
Here are few observations that I noticed when I tried to run your tests:
1. The '-h' tells you nothing about --stage argument:
root@liveupdate-vm:~/liveupdate# ./luo_memfd -h Usage: ./luo_memfd [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] -h print help -l list all tests -d enable debug printsQuoted 8 lines
-t name include test -T name exclude test -v name include variant -V name exclude variant -f name include fixture -F name exclude fixture -r name run specified test ...Yeah, unfortunately that is a side effect of using
test_harness_run(), which does not know anything about the options specific to our test.Quoted 3 lines
2. '-l' does not work after you run stage1, do you keep
/dev/liveupdateopen? That is not needed, we only need to keep session open.Oh yeah, I keep forgetting that is no longer needed. The main process closes the FD but the forked daemons hold a reference. I can clean that up via a fixture.
Quoted 4 lines
root@liveupdate-vm:~/liveupdate# ./luo_memfd -l 1..0 # SKIP Failed to open /dev/liveupdate (Device or resource busy) device. Is LUO enabled?
Quoted 64 lines
Quoted 5 lines
Quoted 1 line
3. Stage 1 has proper [STAGE 1] prefix, but no [STAGE 2] prefix for
Because stage 2 has no prints, all the prints are coming from the selftest harness. Those same lines are also not prefixed in stage 1. If you'd like, I can add a print beforehand that shows which stage is
Yes, please, add a least one [STAGE 2] print.
Quoted 56 lines
running. Other than that, I don't see what else we can do. I don't want to modify the selftest harness.
Quoted 53 lines
stage 2:
# Starting 4 tests from 1 test cases.
# RUN global.memfd_data ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 245. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.memfd_data
ok 1 global.memfd_data
# RUN global.zero_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 247. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.zero_memfd
ok 2 global.zero_memfd
# RUN global.preserved_ops ...
# OK global.preserved_ops
ok 3 global.preserved_ops
# RUN global.fallocate_memfd ...
# [STAGE 1] Forking persistent child to hold sessions... # [STAGE 1] Child PID: 250. Resources are pinned. # [STAGE 1] You may now perform kexec reboot.
# OK global.fallocate_memfd
ok 4 global.fallocate_memfd
# PASSED: 4 / 4 tests passed.
# Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0./do_kexec
root@liveupdate-vm:~/liveupdate# ./luo_memfd TAP version 13 1..4 # Starting 4 tests from 1 test cases. # RUN global.memfd_data ... # OK global.memfd_data ok 1 global.memfd_data # RUN global.zero_memfd ... # OK global.zero_memfd ok 2 global.zero_memfd # RUN global.preserved_ops ... # SKIP test only expected to run on stage 1 # OK global.preserved_ops ok 3 global.preserved_ops # SKIP test only expected to run on stage 1 # RUN global.fallocate_memfd ... # OK global.fallocate_memfd ok 4 global.fallocate_memfd # PASSED: 4 / 4 tests passed. # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:1 error:04. I also do not like that we now have duplicated stage parsing code in
luo_test(), perhaps we should add our owntest_harness_run()variant that depends on stage, and use it in both current tests, and the new memfd tests.
One more comment:
root@liveupdate-vm:~/liveupdate# ./luo_memfd --stage 2 Bail out! Stage mismatch: expected 2, got 1 # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0
Should be: excpected 1, got 2..
Quoted 26 lines
Quoted 5 lines
Sounds good in principle, but unfortunately ends up duplicating a lot of logic in
test_harness_run()that is not a good idea IMO. We should work with the harness not fork off into our own.Quoted 7 lines
I suppose we can refactor some of the logic there to split into functions that we can then use in our
luo_test_harness_run(), but keeping the option parsing logic in sync is going to be difficult.And for the duplicated logic, I agree. I thought about cleaning it up but was feeling lazy... Well now that you have called it out let me see what I can do.
The main point is that the
luo_sessionsandluo_memfdstests should use a common framework, whether that'sluo_test_harness_run()or the generictest_harness_run(). I don't have a definitive answer for this, so I recommend tinkering with it to see what works best.Pasha
Quoted 6 lines
[...]
Signature · 2 lines
Regards, Pratyush Yadav