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

Pratyush Yadav
2026-04-05T07:34:08Z

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