Browse Source

lib/lz4: explicitly support in-place decompression

LZ4 final literal copy could be overlapped when doing
in-place decompression, so it's unsafe to just use memcpy()
on an optimized memcpy approach but memmove() instead.

Upstream LZ4 has updated this years ago [1] (and the impact
is non-sensible [2] plus only a few bytes remain), this commit
just synchronizes LZ4 upstream code to the kernel side as well.

It can be observed as EROFS in-place decompression failure
on specific files when X86_FEATURE_ERMS is unsupported,
memcpy() optimization of commit 59daa706fb ("x86, mem:
Optimize memcpy by avoiding memory false dependece") will
be enabled then.

Currently most modern x86-CPUs support ERMS, these CPUs just
use "rep movsb" approach so no problem at all. However, it can
still be verified with forcely disabling ERMS feature...

        ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
-                     "jmp memcpy_erms", X86_FEATURE_ERMS
+                     "jmp memcpy_orig", X86_FEATURE_ERMS

We didn't observe any strange on arm64/arm/x86 platform before
since most memcpy() would behave in an increasing address order
("copy upwards" [3]) and it's the correct order of in-place
decompression but it really needs an update to memmove() for sure
considering it's an undefined behavior according to the standard
and some unique optimization already exists in the kernel.

[1] 33cb8518ac

Signed-off-by: Gao Xiang <>
Reviewed-by: Nick Terrell <>
Cc: Yann Collet <>
Cc: Miao Xie <>
Cc: Chao Yu <>
Cc: Li Guifu <>
Cc: Guo Xuenan <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>
Gao Xiang 5 months ago
committed by Linus Torvalds
2 changed files with 6 additions and 1 deletions
  1. +5
  2. +1

+ 5
- 1
lib/lz4/lz4_decompress.c View File

@ -263,7 +263,11 @@ static FORCE_INLINE int LZ4_decompress_generic(
LZ4_memcpy(op, ip, length);
* supports overlapping memory regions; only matters
* for in-place decompression scenarios
LZ4_memmove(op, ip, length);
ip += length;
op += length;

+ 1
- 0
lib/lz4/lz4defs.h View File

@ -146,6 +146,7 @@ static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
* environments. This is needed when decompressing the Linux Kernel, for example.
#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
#define LZ4_memmove(dst, src, size) __builtin_memmove(dst, src, size)
static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)