Skip to content

Commit f20eef4

Browse files
Gao Xianggregkh
authored andcommitted
lib/lz4: explicitly support in-place decompression
commit 89b1586 upstream. 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 59daa70 ("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... arch/x86/lib/memcpy_64.S: 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] lz4/lz4@33cb851 [2] lz4/lz4#717 (comment) [3] https://sourceware.org/bugzilla/show_bug.cgi?id=12518 Link: https://lkml.kernel.org/r/20201122030749.2698994-1-hsiangkao@redhat.com Signed-off-by: Gao Xiang <hsiangkao@redhat.com> Reviewed-by: Nick Terrell <terrelln@fb.com> Cc: Yann Collet <yann.collet.73@gmail.com> Cc: Miao Xie <miaoxie@huawei.com> Cc: Chao Yu <yuchao0@huawei.com> Cc: Li Guifu <bluce.liguifu@huawei.com> Cc: Guo Xuenan <guoxuenan@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 334c59d commit f20eef4

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

‎lib/lz4/lz4_decompress.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ static FORCE_INLINE int LZ4_decompress_generic(
263263
}
264264
}
265265

266-
LZ4_memcpy(op, ip, length);
266+
/*
267+
* supports overlapping memory regions; only matters
268+
* for in-place decompression scenarios
269+
*/
270+
LZ4_memmove(op, ip, length);
267271
ip += length;
268272
op += length;
269273

‎lib/lz4/lz4defs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
146146
* environments. This is needed when decompressing the Linux Kernel, for example.
147147
*/
148148
#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
149+
#define LZ4_memmove(dst, src, size) __builtin_memmove(dst, src, size)
149150

150151
static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
151152
{

0 commit comments

Comments
 (0)