[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <c6066469-7bc9-4232-b600-0b167193f13f@ics.forth.gr>
Date: Tue, 30 Jan 2024 18:52:24 +0200
From: Nick Kossifidis <mick@....forth.gr>
To: Jisheng Zhang <jszhang@...nel.org>
Cc: Paul Walmsley <paul.walmsley@...ive.com>,
Palmer Dabbelt <palmer@...belt.com>, Albert Ou <aou@...s.berkeley.edu>,
linux-riscv@...ts.infradead.org, linux-kernel@...r.kernel.org,
Matteo Croce <mcroce@...rosoft.com>, kernel test robot <lkp@...el.com>
Subject: Re: [PATCH 2/3] riscv: optimized memmove
On 1/30/24 15:12, Jisheng Zhang wrote:
> On Tue, Jan 30, 2024 at 01:39:10PM +0200, Nick Kossifidis wrote:
>> On 1/28/24 13:10, Jisheng Zhang wrote:
>>> From: Matteo Croce <mcroce@...rosoft.com>
>>>
>>> When the destination buffer is before the source one, or when the
>>> buffers doesn't overlap, it's safe to use memcpy() instead, which is
>>> optimized to use a bigger data size possible.
>>>
>>> Signed-off-by: Matteo Croce <mcroce@...rosoft.com>
>>> Reported-by: kernel test robot <lkp@...el.com>
>>> Signed-off-by: Jisheng Zhang <jszhang@...nel.org>
>>
>> I'd expect to have memmove handle both fw/bw copying and then memcpy being
>> an alias to memmove, to also take care when regions overlap and avoid
>> undefined behavior.
>
> Hi Nick,
>
> Here is somthing from man memcpy:
>
> "void *memcpy(void dest[restrict .n], const void src[restrict .n],
> size_t n);
>
> The memcpy() function copies n bytes from memory area src to memory area dest.
> The memory areas must not overlap. Use memmove(3) if the memory areas do over‐
> lap."
>
> IMHO, the "restrict" implies that there's no overlap. If overlap
> happens, the manual doesn't say what will happen.
>
> From another side, I have a concern: currently, other arch don't have
> this alias behavior, IIUC(at least, per my understanding of arm and arm64
> memcpy implementations)they just copy forward. I want to keep similar behavior
> for riscv.
>
> So I want to hear more before going towards alias-memcpy-to-memmove direction.
>
> Thanks
If you read Matteo's original post that was also his suggestion, and
Linus has also commented on that. In general it's better to handle the
case where the regions provided to memcpy() overlap than to resort to
"undefined behavior", I provided a backwards copy example that you can
use so that we can have both fw and bw copying for memmove(), and use
memmove() in any case. The [restrict .n] in the prototype is just there
to say that the size of src is restricted by n (the next argument). If
someone uses memcpy() with overlapping regions, which is always a
possibility, in your case it'll result corrupted data, we won't even get
a warning (still counts as undefined behavior) about it.
Regards,
Nick
Powered by blists - more mailing lists