From: Vipin Sharma <vipinsh@google.com>
To: pasha.tatashin@soleen.com, rppt@kernel.org, pratyush@kernel.org
Cc: tarunsahu@google.com, skhawaja@google.com, shuah@kernel.org,
dmatlack@google.com, linux-kernel@vger.kernel.org,
kexec@lists.infradead.org, linux-kselftest@vger.kernel.org,
Vipin Sharma <vipinsh@google.com>
Subject: [PATCH v2 2/2] selftests/liveupdate: Add helpers to preserve/retrieve FDs
Date: Wed, 10 Jun 2026 16:09:08 -0700 [thread overview]
Message-ID: <20260610230909.876546-3-vipinsh@google.com> (raw)
In-Reply-To: <20260610230909.876546-1-vipinsh@google.com>
Add helper functions to preserve and retrieve file descriptors from an
LUO session. This allows library users to work with FD preservation.
Now that there are helpers, use them instead of direct ioctl calls
in liveupdate.c
No functional change intended.
Co-developed-by: David Matlack <dmatlack@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
.../liveupdate/lib/include/libliveupdate.h | 2 +
.../selftests/liveupdate/lib/liveupdate.c | 41 +++++++++++++++----
.../testing/selftests/liveupdate/liveupdate.c | 38 ++++++-----------
3 files changed, 46 insertions(+), 35 deletions(-)
diff --git a/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h b/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h
index 6ff71d7c3d9c..2c6a6f6a2733 100644
--- a/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h
+++ b/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h
@@ -27,6 +27,8 @@ int luo_retrieve_session(int luo_fd, const char *name);
int luo_session_finish(int session_fd);
int luo_ensure_nofile_limit(long min_limit);
+int luo_session_preserve_fd(int session_fd, int fd, __u64 token);
+int luo_session_retrieve_fd(int session_fd, __u64 token);
int create_and_preserve_memfd(int session_fd, int token, const char *data);
int restore_and_verify_memfd(int session_fd, int token, const char *expected_data);
diff --git a/tools/testing/selftests/liveupdate/lib/liveupdate.c b/tools/testing/selftests/liveupdate/lib/liveupdate.c
index 897c200f3845..961a732b541d 100644
--- a/tools/testing/selftests/liveupdate/lib/liveupdate.c
+++ b/tools/testing/selftests/liveupdate/lib/liveupdate.c
@@ -78,9 +78,35 @@ int luo_retrieve_session(int luo_fd, const char *name)
return arg.fd;
}
+int luo_session_preserve_fd(int session_fd, int fd, __u64 token)
+{
+ struct liveupdate_session_preserve_fd arg = {
+ .size = sizeof(arg),
+ .fd = fd,
+ .token = token,
+ };
+
+ if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0)
+ return -errno;
+
+ return 0;
+}
+
+int luo_session_retrieve_fd(int session_fd, __u64 token)
+{
+ struct liveupdate_session_retrieve_fd arg = {
+ .size = sizeof(arg),
+ .token = token,
+ };
+
+ if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0)
+ return -errno;
+
+ return arg.fd;
+}
+
int create_and_preserve_memfd(int session_fd, int token, const char *data)
{
- struct liveupdate_session_preserve_fd arg = { .size = sizeof(arg) };
long page_size = sysconf(_SC_PAGE_SIZE);
void *map = MAP_FAILED;
int mfd = -1, ret = -1;
@@ -99,9 +125,8 @@ int create_and_preserve_memfd(int session_fd, int token, const char *data)
snprintf(map, page_size, "%s", data);
munmap(map, page_size);
- arg.fd = mfd;
- arg.token = token;
- if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0)
+ ret = luo_session_preserve_fd(session_fd, mfd, token);
+ if (ret)
goto out;
ret = 0;
@@ -116,15 +141,13 @@ int create_and_preserve_memfd(int session_fd, int token, const char *data)
int restore_and_verify_memfd(int session_fd, int token,
const char *expected_data)
{
- struct liveupdate_session_retrieve_fd arg = { .size = sizeof(arg) };
long page_size = sysconf(_SC_PAGE_SIZE);
void *map = MAP_FAILED;
int mfd = -1, ret = -1;
- arg.token = token;
- if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0)
- return -errno;
- mfd = arg.fd;
+ mfd = luo_session_retrieve_fd(session_fd, token);
+ if (mfd < 0)
+ return mfd;
map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, mfd, 0);
if (map == MAP_FAILED)
diff --git a/tools/testing/selftests/liveupdate/liveupdate.c b/tools/testing/selftests/liveupdate/liveupdate.c
index 6bfd4e53528c..e17a13136330 100644
--- a/tools/testing/selftests/liveupdate/liveupdate.c
+++ b/tools/testing/selftests/liveupdate/liveupdate.c
@@ -170,20 +170,6 @@ TEST_F(liveupdate_device, create_distinct_sessions)
ASSERT_EQ(close(session_fd2), 0);
}
-static int preserve_fd(int session_fd, int fd_to_preserve, __u64 token)
-{
- struct liveupdate_session_preserve_fd args = {};
-
- args.size = sizeof(args);
- args.fd = fd_to_preserve;
- args.token = token;
-
- if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &args))
- return -errno;
-
- return 0;
-}
-
/*
* Test Case: Preserve MemFD
*
@@ -208,7 +194,7 @@ TEST_F(liveupdate_device, preserve_memfd)
ASSERT_GE(mem_fd, 0);
ASSERT_EQ(write(mem_fd, test_str, strlen(test_str)), strlen(test_str));
- ASSERT_EQ(preserve_fd(session_fd, mem_fd, 0x1234), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd, mem_fd, 0x1234), 0);
ASSERT_EQ(close(session_fd), 0);
ASSERT_EQ(lseek(mem_fd, 0, SEEK_SET), 0);
@@ -247,8 +233,8 @@ TEST_F(liveupdate_device, preserve_multiple_memfds)
ASSERT_EQ(write(mem_fd1, test_str1, strlen(test_str1)), strlen(test_str1));
ASSERT_EQ(write(mem_fd2, test_str2, strlen(test_str2)), strlen(test_str2));
- ASSERT_EQ(preserve_fd(session_fd, mem_fd1, 0xAAAA), 0);
- ASSERT_EQ(preserve_fd(session_fd, mem_fd2, 0xBBBB), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd, mem_fd1, 0xAAAA), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd, mem_fd2, 0xBBBB), 0);
memset(read_buf, 0, sizeof(read_buf));
ASSERT_EQ(lseek(mem_fd1, 0, SEEK_SET), 0);
@@ -303,10 +289,10 @@ TEST_F(liveupdate_device, preserve_complex_scenario)
mem_fd_empty2 = memfd_create("empty2", 0);
ASSERT_GE(mem_fd_empty2, 0);
- ASSERT_EQ(preserve_fd(session_fd1, mem_fd_data1, 0x1111), 0);
- ASSERT_EQ(preserve_fd(session_fd1, mem_fd_empty1, 0x2222), 0);
- ASSERT_EQ(preserve_fd(session_fd2, mem_fd_data2, 0x3333), 0);
- ASSERT_EQ(preserve_fd(session_fd2, mem_fd_empty2, 0x4444), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd1, mem_fd_data1, 0x1111), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd1, mem_fd_empty1, 0x2222), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd2, mem_fd_data2, 0x3333), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd2, mem_fd_empty2, 0x4444), 0);
ASSERT_EQ(lseek(mem_fd_data1, 0, SEEK_SET), 0);
ASSERT_EQ(read(mem_fd_data1, read_buf, sizeof(read_buf)), strlen(data1));
@@ -355,7 +341,7 @@ TEST_F(liveupdate_device, preserve_unsupported_fd)
unsupported_fd = open("/dev/null", O_RDWR);
ASSERT_GE(unsupported_fd, 0);
- ret = preserve_fd(session_fd, unsupported_fd, 0xDEAD);
+ ret = luo_session_preserve_fd(session_fd, unsupported_fd, 0xDEAD);
EXPECT_EQ(ret, -ENOENT);
ASSERT_EQ(close(unsupported_fd), 0);
@@ -388,14 +374,14 @@ TEST_F(liveupdate_device, prevent_double_preservation)
ASSERT_GE(mem_fd, 0);
/* First preservation should succeed */
- ASSERT_EQ(preserve_fd(session_fd1, mem_fd, 0x1111), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd1, mem_fd, 0x1111), 0);
/* Second preservation in a different session should fail with EBUSY */
- ret = preserve_fd(session_fd2, mem_fd, 0x2222);
+ ret = luo_session_preserve_fd(session_fd2, mem_fd, 0x2222);
EXPECT_EQ(ret, -EBUSY);
/* Second preservation in the same session (different token) should fail with EBUSY */
- ret = preserve_fd(session_fd1, mem_fd, 0x3333);
+ ret = luo_session_preserve_fd(session_fd1, mem_fd, 0x3333);
EXPECT_EQ(ret, -EBUSY);
ASSERT_EQ(close(mem_fd), 0);
@@ -565,7 +551,7 @@ TEST_F(liveupdate_device, preserve_many_files)
for (i = 0; i < MANY_FILES; i++) {
mem_fds[i] = memfd_create("test-memfd", 0);
ASSERT_GE(mem_fds[i], 0);
- ASSERT_EQ(preserve_fd(session_fd, mem_fds[i], i), 0);
+ ASSERT_EQ(luo_session_preserve_fd(session_fd, mem_fds[i], i), 0);
}
for (i = 0; i < MANY_FILES; i++)
--
2.54.0.1099.g489fc7bff1-goog
next prev parent reply other threads:[~2026-06-10 23:09 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-10 23:09 [PATCH v2 0/2] Make liveupdate selftests library Vipin Sharma
2026-06-10 23:09 ` [PATCH v2 1/2] selftests/liveupdate: Move luo_test_utils.* into a reusable library Vipin Sharma
2026-06-12 3:09 ` Pasha Tatashin
2026-06-12 17:29 ` Vipin Sharma
2026-06-10 23:09 ` Vipin Sharma [this message]
2026-06-11 8:56 ` [PATCH v2 2/2] selftests/liveupdate: Add helpers to preserve/retrieve FDs Pratyush Yadav
2026-06-12 3:09 ` Pasha Tatashin
2026-06-12 17:34 ` Vipin Sharma
2026-06-12 19:32 ` Vipin Sharma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260610230909.876546-3-vipinsh@google.com \
--to=vipinsh@google.com \
--cc=dmatlack@google.com \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.org \
--cc=rppt@kernel.org \
--cc=shuah@kernel.org \
--cc=skhawaja@google.com \
--cc=tarunsahu@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®