Skip to contentSkip to conversation

lib/lz4: explicitly support in-place decompression

PATCH Superseded Gao Xiang 2 participants 4 messages Nov 21, 2020 – Nov 30, 2020

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 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]), 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 59daa706fbec ("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 observed any strange on arm64/arm platform before, but it really needs an update for sure considering this unsafe overlapping memcpy() behavior for LZ4 in-place decompression.

[1] https://github.com/lz4/lz4/commit/33cb8518ac385835cc17be9a770b27b40cd0e15b
[2] https://github.com/lz4/lz4/pull/717#issuecomment-497818921

Cc Yann Collet <[email protected]>
Cc Miao Xie <[email protected]>
Cc Chao Yu <[email protected]>

---
Hi Andrew,

Could you kindly consider picking this patch up, although the impact is EROFS but it touchs in-kernel lz4 library anyway...

Thanks,
Gao Xiang

 lib/lz4/lz4_decompress.c | 2 +-
 lib/lz4/lz4defs.h        | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
lib/lz4/lz4_decompress.c +1 -1
index 00cb0d0b73e1..fa88e13ef00b 100644
@@ -263,7 +263,7 @@ static FORCE_INLINE int LZ4_decompress_generic(
 				}
 			}
 
-			LZ4_memcpy(op, ip, length);
+			LZ4_memmove(op, ip, length);
 			ip += length;
 			op += length;
 
lib/lz4/lz4defs.h +1 -0
index c91dd96ef629..673bd206aa98 100644
@@ -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)
 {
Signature · 1 line
2.18.4
author
#1 lore raw

PATCH v2 1/1 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 59daa706fbec ("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] https://github.com/lz4/lz4/commit/33cb8518ac385835cc17be9a770b27b40cd0e15b
[2] https://github.com/lz4/lz4/pull/717#issuecomment-497818921
[3] https://sourceware.org/bugzilla/show_bug.cgi?id=12518

Cc Yann Collet <[email protected]>
Cc Nick Terrell <[email protected]>
Cc Miao Xie <[email protected]>
Cc Chao Yu <[email protected]>
Cc Li Guifu <[email protected]>
Cc Guo Xuenan <[email protected]>

---
changes since v1:
- refine commit message;
- Cc more people.

Hi Andrew,

Could you kindly consider picking this patch up, although the impact is EROFS but it touchs in-kernel lz4 library anyway...

Thanks,
Gao Xiang

 lib/lz4/lz4_decompress.c | 6 +++++-
 lib/lz4/lz4defs.h        | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)
lib/lz4/lz4_decompress.c +5 -1
index 00cb0d0b73e1..8a7724a6ce2f 100644
@@ -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;
 
lib/lz4/lz4defs.h +1 -0
index c91dd96ef629..673bd206aa98 100644
@@ -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)
 {
Signature · 1 line
2.18.4
Reviewed-by on v2 1/1
#2 lore raw
Quoted 94 lines · Gao Xiang

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 59daa706fbec ("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, \
(quoted diff) +1 -1
-                     "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] https://github.com/lz4/lz4/commit/33cb8518ac385835cc17be9a770b27b40cd0e15b
[2] https://github.com/lz4/lz4/pull/717#issuecomment-497818921
[3] https://sourceware.org/bugzilla/show_bug.cgi?id=12518

Cc Yann Collet <[email protected]>
Cc Nick Terrell <[email protected]>
Cc Miao Xie <[email protected]>
Cc Chao Yu <[email protected]>
Cc Li Guifu <[email protected]>
Cc Guo Xuenan <[email protected]>

---
changes since v1:
- refine commit message;
- Cc more people.

Hi Andrew,

Could you kindly consider picking this patch up, although the impact is EROFS but it touchs in-kernel lz4 library anyway...

Thanks,
Gao Xiang

lib/lz4/lz4_decompress.c | 6 +++++-
lib/lz4/lz4defs.h | 1 +

2 files changed, 6 insertions(+), 1 deletion(-)
lib/lz4/lz4_decompress.c +5 -1
index 00cb0d0b73e1..8a7724a6ce2f 100644
@@ -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;
 
lib/lz4/lz4defs.h +1 -0
index c91dd96ef629..673bd206aa98 100644
@@ -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)
 {
Signature · 1 line
2.18.4

Looks good to me! You can add: