All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pratyush Yadav <pratyush@kernel.org>
To: Zhu Yanjun <yanjun.zhu@linux.dev>
Cc: Pratyush Yadav <pratyush@kernel.org>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	 Mike Rapoport <rppt@kernel.org>,  Shuah Khan <shuah@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>,
	 Usama Arif <usama.arif@linux.dev>,
	 linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 linux-mm@kvack.org
Subject: Re: [PATCH v3 2/6] selftests/liveupdate: add helper functions for memfd tests
Date: Tue, 07 Apr 2026 10:30:58 +0000	[thread overview]
Message-ID: <2vxz5x63awpp.fsf@kernel.org> (raw)
In-Reply-To: <b7af6516-1593-4e2b-9149-fc839e2b88bd@linux.dev> (Zhu Yanjun's message of "Sun, 5 Apr 2026 09:30:17 -0700")

On Sun, Apr 05 2026, Zhu Yanjun wrote:

> 在 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.

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.

>
> 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;
>>>> +}
>>>> +
>> [...]
>>

-- 
Regards,
Pratyush Yadav

  reply	other threads:[~2026-04-07 10:31 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 17:53   ` Zhu Yanjun
2026-04-05  7:34     ` Pratyush Yadav
2026-04-05 16:30       ` Zhu Yanjun
2026-04-07 10:30         ` Pratyush Yadav [this message]
2026-04-04 10:24 ` [PATCH v3 3/6] selftests/liveupdate: add test for memfd content preservation Pratyush Yadav
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 ` [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
2026-04-28 19:31 ` [PATCH v3 0/6] selftests/liveupdate: add memfd tests Pasha Tatashin
2026-04-29 13:20   ` Pratyush Yadav
2026-05-01 19:08     ` Pasha Tatashin
2026-05-05 16:32       ` Pasha Tatashin

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=2vxz5x63awpp.fsf@kernel.org \
    --to=pratyush@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=yanjun.zhu@linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.