From: Matteo Croce <mcroce@xxxxxxxxxxxxx> Subject: lib/string: optimized memmove 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. This "optimization" only covers a common case. In future, proper code which does the same thing as memcpy() does but backwards can be done. Link: https://lkml.kernel.org/r/20210702123153.14093-3-mcroce@xxxxxxxxxxxxxxxxxxx Signed-off-by: Matteo Croce <mcroce@xxxxxxxxxxxxx> Cc: Christoph Hellwig <hch@xxxxxxxxxxxxx> Cc: David Laight <David.Laight@xxxxxxxxxx> Cc: Drew Fustini <drew@xxxxxxxxxxxxxxx> Cc: Emil Renner Berthing <kernel@xxxxxxxx> Cc: Guo Ren <guoren@xxxxxxxxxx> Cc: Nick Desaulniers <ndesaulniers@xxxxxxxxxx> Cc: Nick Kossifidis <mick@xxxxxxxxxxxx> Cc: Palmer Dabbelt <palmer@xxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- lib/string.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) --- a/lib/string.c~lib-string-optimized-memmove +++ a/lib/string.c @@ -975,19 +975,13 @@ EXPORT_SYMBOL(memcpy); */ void *memmove(void *dest, const void *src, size_t count) { - char *tmp; - const char *s; + if (dest < src || src + count <= dest) + return memcpy(dest, src, count); + + if (dest > src) { + const char *s = src + count; + char *tmp = dest + count; - if (dest <= src) { - tmp = dest; - s = src; - while (count--) - *tmp++ = *s++; - } else { - tmp = dest; - tmp += count; - s = src; - s += count; while (count--) *--tmp = *--s; } _