* [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress()
@ 2026-04-13 19:07 Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag Enzo Matsumiya
` (6 more replies)
0 siblings, 7 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
@dst buffer is allocated with same size as @src, which, for good
compression cases, works fine.
However, when compression goes bad (e.g. random bytes payloads), the
compressed size can increase significantly, and even by stopping the
main loop at 7/8 of @slen, writing leftover literals could write past
the end of @dst because of LZ77 metadata.
To fix this, add lz77_compressed_alloc_size() helper to compute the
correct allocation size for @dst, accounting for metadata and worst
cast scenario (all literals).
While this is overprovisioning memory, it's not only correct, but also
allows lz77_compress() main loop to run without ever checking @dst
limits (i.e. a perf improvement).
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress.c | 6 +-----
fs/smb/client/compress/lz77.c | 14 ++++----------
fs/smb/client/compress/lz77.h | 28 ++++++++++++++++++++++++++++
3 files changed, 33 insertions(+), 15 deletions(-)
diff --git a/fs/smb/client/compress.c b/fs/smb/client/compress.c
index 3d1e73f5d9af..be9023f841e6 100644
--- a/fs/smb/client/compress.c
+++ b/fs/smb/client/compress.c
@@ -329,11 +329,7 @@ int smb_compress(struct TCP_Server_Info *server, struct smb_rqst *rq, compress_s
goto err_free;
}
- /*
- * This is just overprovisioning, as the algorithm will error out if @dst reaches 7/8
- * of @slen.
- */
- dlen = slen;
+ dlen = lz77_compressed_alloc_size(slen);
dst = kvzalloc(dlen, GFP_KERNEL);
if (!dst) {
ret = -ENOMEM;
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 96e8a8057a77..16c7d8f3ef17 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -137,6 +137,10 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
long flag = 0;
u64 *htable;
+ /* This is probably a bug, so throw a warning. */
+ if (WARN_ON_ONCE(*dlen < lz77_compressed_alloc_size(slen)))
+ return -EINVAL;
+
srcp = src;
end = src + slen;
dstp = dst;
@@ -180,15 +184,6 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
continue;
}
- /*
- * Bail out if @dstp reached >= 7/8 of @slen -- already compressed badly, not worth
- * going further.
- */
- if (unlikely(dstp - dst >= slen - (slen >> 3))) {
- *dlen = slen;
- goto out;
- }
-
dstp = lz77_write_match(dstp, &nib, dist, len);
srcp += len;
@@ -225,7 +220,6 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
lz77_write32(flag_pos, flag);
*dlen = dstp - dst;
-out:
kvfree(htable);
if (*dlen < slen)
diff --git a/fs/smb/client/compress/lz77.h b/fs/smb/client/compress/lz77.h
index cdcb191b48a2..2603eab9e071 100644
--- a/fs/smb/client/compress/lz77.h
+++ b/fs/smb/client/compress/lz77.h
@@ -11,5 +11,33 @@
#include <linux/kernel.h>
+/**
+ * lz77_compressed_alloc_size() - Compute compressed buffer size.
+ * @size: uncompressed (src) size
+ *
+ * Compute allocation size for the compressed buffer based on uncompressed size.
+ * Accounts for metadata and overprovision for the worst case scenario.
+ *
+ * LZ77 metadata is a 4-byte flag that is written:
+ * - on dst begin (pos 0)
+ * - every 32 literals or matches
+ * - on end-of-stream (possibly, if last write was another flag)
+ *
+ * Worst case scenario is an all-literal compression, which means:
+ * metadata bytes = 4 + ((@size / 32) * 4) + 4, or, simplified, (@size >> 3) + 8
+ *
+ * The worst case scenario rarely happens, but such overprovisioning also allows lz77_compress()
+ * main loop to run without ever bound checking dst, which is a huge perf improvement, while also
+ * being safe when compression goes bad.
+ *
+ * Return: required (*) allocation size for compressed buffer.
+ *
+ * (*) checked once in the beginning of lz77_compress()
+ */
+static __always_inline u32 lz77_compressed_alloc_size(const u32 size)
+{
+ return size + (size >> 3) + 8;
+}
+
int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen);
#endif /* _SMB_COMPRESS_LZ77_H */
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-19 1:35 ` Steve French
2026-04-13 19:07 ` [PATCH 3/8] smb: client: compress: fix counting in LZ77 match finding Enzo Matsumiya
` (5 subsequent siblings)
6 siblings, 1 reply; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
End-of-stream flag could lead to UB because of int promotion
(overwriting signed bit).
Fix it by changing operand from '1' to '1UL'.
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/lz77.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 16c7d8f3ef17..c1e7fada6e61 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -216,7 +216,7 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
}
flag <<= (32 - flag_count);
- flag |= (1 << (32 - flag_count)) - 1;
+ flag |= (1UL << (32 - flag_count)) - 1;
lz77_write32(flag_pos, flag);
*dlen = dstp - dst;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 3/8] smb: client: compress: fix counting in LZ77 match finding
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 4/8] smb: client: compress: increase LZ77_MATCH_MAX_DIST Enzo Matsumiya
` (4 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
- lz77_match_len() increments @cur before checking for equality,
leading to off-by-one match len in some cases.
Fix by moving pointers increment to inside the loop.
Also rename @wnd arg to @match (more accurate name).
- both lz77_match_len() and lz77_compress() checked for
"buf + step < end" when the correct is "<=" for such cases.
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/lz77.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index c1e7fada6e61..61cdf1c14612 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -48,17 +48,17 @@ static __always_inline void lz77_write32(u32 *ptr, u32 v)
put_unaligned_le32(v, ptr);
}
-static __always_inline u32 lz77_match_len(const void *wnd, const void *cur, const void *end)
+static __always_inline u32 lz77_match_len(const void *match, const void *cur, const void *end)
{
const void *start = cur;
u64 diff;
/* Safe for a do/while because otherwise we wouldn't reach here from the main loop. */
do {
- diff = lz77_read64(cur) ^ lz77_read64(wnd);
+ diff = lz77_read64(cur) ^ lz77_read64(match);
if (!diff) {
cur += LZ77_STEP_SIZE;
- wnd += LZ77_STEP_SIZE;
+ match += LZ77_STEP_SIZE;
continue;
}
@@ -67,10 +67,13 @@ static __always_inline u32 lz77_match_len(const void *wnd, const void *cur, cons
cur += count_trailing_zeros(diff) >> 3;
return (cur - start);
- } while (likely(cur + LZ77_STEP_SIZE < end));
+ } while (likely(cur + LZ77_STEP_SIZE <= end));
- while (cur < end && lz77_read8(cur++) == lz77_read8(wnd++))
- ;
+ /* Fallback to byte-by-byte comparison for last <8 bytes. */
+ while (cur < end && lz77_read8(cur) == lz77_read8(match)) {
+ cur++;
+ match++;
+ }
return (cur - start);
}
@@ -195,7 +198,7 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
flag_pos = dstp;
dstp += 4;
}
- } while (likely(srcp + LZ77_STEP_SIZE < end));
+ } while (likely(srcp + LZ77_STEP_SIZE <= end));
while (srcp < end) {
u32 c = umin(end - srcp, 32 - flag_count);
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 4/8] smb: client: compress: increase LZ77_MATCH_MAX_DIST
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 3/8] smb: client: compress: fix counting in LZ77 match finding Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 5/8] smb: client: compress: LZ77 optimizations Enzo Matsumiya
` (3 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
Increase max distance (i.e. window size) from 1k to 8k.
This allows better compression and is just as fast.
Other:
- drop LZ77_MATCH_MIN_DIST as it's nused -- main loop
already checks if dist > 0
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/lz77.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 61cdf1c14612..480927dcd4c6 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -17,8 +17,7 @@
* Compression parameters.
*/
#define LZ77_MATCH_MIN_LEN 4
-#define LZ77_MATCH_MIN_DIST 1
-#define LZ77_MATCH_MAX_DIST SZ_1K
+#define LZ77_MATCH_MAX_DIST SZ_8K
#define LZ77_HASH_LOG 15
#define LZ77_HASH_SIZE (1 << LZ77_HASH_LOG)
#define LZ77_STEP_SIZE sizeof(u64)
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 5/8] smb: client: compress: LZ77 optimizations
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
` (2 preceding siblings ...)
2026-04-13 19:07 ` [PATCH 4/8] smb: client: compress: increase LZ77_MATCH_MAX_DIST Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 6/8] smb: client: compress: add code docs to lz77.c Enzo Matsumiya
` (2 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
This patch implements several micro-optimizations on lz77_compress()
with the goal of reducing the number of instructions per [input]
byte (a.k.a. IPB).
Changes:
- change hashtable to be u32 (instead of u64) -- change the hash
function to reflect that (adds lz77_hash() and lz77_read32() helpers)
- batch-write literals instead of 1 by 1 -- now that we have a well
defined hot path (match finding) and a cold path (encode literals +
match), batch writing makes a significant difference
- implement adaptive skipping of input bytes -- skip input bytes more
aggressively if too few matches are being found
- name some constants for more meaningful context
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/lz77.c | 173 +++++++++++++++++++++-------------
fs/smb/client/compress/lz77.h | 4 +-
2 files changed, 108 insertions(+), 69 deletions(-)
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 480927dcd4c6..96744f52e364 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Copyright (C) 2024, SUSE LLC
+ * Copyright (C) 2024-2026, SUSE LLC
*
* Authors: Enzo Matsumiya <ematsumiya@suse.de>
*
@@ -16,17 +16,26 @@
/*
* Compression parameters.
*/
-#define LZ77_MATCH_MIN_LEN 4
#define LZ77_MATCH_MAX_DIST SZ_8K
#define LZ77_HASH_LOG 15
#define LZ77_HASH_SIZE (1 << LZ77_HASH_LOG)
-#define LZ77_STEP_SIZE sizeof(u64)
+#define LZ77_RSTEP_SIZE sizeof(u32)
+#define LZ77_MSTEP_SIZE sizeof(u64)
+#define LZ77_SKIP_TRIGGER 4
+
+#define LZ77_PREFETCH(ptr) __builtin_prefetch((ptr), 0, 3)
+#define LZ77_FLAG_MAX 32
static __always_inline u8 lz77_read8(const u8 *ptr)
{
return get_unaligned(ptr);
}
+static __always_inline u32 lz77_read32(const u32 *ptr)
+{
+ return get_unaligned(ptr);
+}
+
static __always_inline u64 lz77_read64(const u64 *ptr)
{
return get_unaligned(ptr);
@@ -50,14 +59,14 @@ static __always_inline void lz77_write32(u32 *ptr, u32 v)
static __always_inline u32 lz77_match_len(const void *match, const void *cur, const void *end)
{
const void *start = cur;
- u64 diff;
/* Safe for a do/while because otherwise we wouldn't reach here from the main loop. */
do {
- diff = lz77_read64(cur) ^ lz77_read64(match);
+ const u64 diff = lz77_read64(cur) ^ lz77_read64(match);
+
if (!diff) {
- cur += LZ77_STEP_SIZE;
- match += LZ77_STEP_SIZE;
+ cur += LZ77_MSTEP_SIZE;
+ match += LZ77_MSTEP_SIZE;
continue;
}
@@ -66,7 +75,7 @@ static __always_inline u32 lz77_match_len(const void *match, const void *cur, co
cur += count_trailing_zeros(diff) >> 3;
return (cur - start);
- } while (likely(cur + LZ77_STEP_SIZE <= end));
+ } while (likely(cur + LZ77_MSTEP_SIZE <= end));
/* Fallback to byte-by-byte comparison for last <8 bytes. */
while (cur < end && lz77_read8(cur) == lz77_read8(match)) {
@@ -77,7 +86,7 @@ static __always_inline u32 lz77_match_len(const void *match, const void *cur, co
return (cur - start);
}
-static __always_inline void *lz77_write_match(void *dst, void **nib, u32 dist, u32 len)
+static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist, u32 len)
{
len -= 3;
dist--;
@@ -131,94 +140,124 @@ static __always_inline void *lz77_write_match(void *dst, void **nib, u32 dist, u
return dst + 4;
}
-noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
+static __always_inline void *lz77_encode_literals(const void *start, const void *end, void *dst,
+ long *f, u32 *fc, void **fp)
+{
+ if (start >= end)
+ return dst;
+
+ do {
+ const u32 len = umin(end - start, LZ77_FLAG_MAX - *fc);
+
+ memcpy(dst, start, len);
+
+ dst += len;
+ start += len;
+
+ *f <<= len;
+ *fc += len;
+ if (*fc == LZ77_FLAG_MAX) {
+ lz77_write32(*fp, *f);
+ *fc = 0;
+ *fp = dst;
+ dst += 4;
+ }
+ } while (start < end);
+
+ return dst;
+}
+
+static __always_inline u32 lz77_hash(const u32 v)
+{
+ return ((v ^ 0x9E3779B9) * 0x85EBCA6B) >> (32 - LZ77_HASH_LOG);
+}
+
+noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen)
{
- const void *srcp, *end;
+ const void *srcp, *rlim, *end, *anchor;
+ u32 *htable, hash, flag_count = 0;
void *dstp, *nib, *flag_pos;
- u32 flag_count = 0;
long flag = 0;
- u64 *htable;
/* This is probably a bug, so throw a warning. */
if (WARN_ON_ONCE(*dlen < lz77_compressed_alloc_size(slen)))
return -EINVAL;
- srcp = src;
- end = src + slen;
+ srcp = anchor = src;
+ end = srcp + slen; /* absolute end */
+ rlim = end - LZ77_MSTEP_SIZE; /* read limit (for lz77_match_len()) */
dstp = dst;
- nib = NULL;
flag_pos = dstp;
dstp += 4;
+ nib = NULL;
htable = kvcalloc(LZ77_HASH_SIZE, sizeof(*htable), GFP_KERNEL);
if (!htable)
return -ENOMEM;
- /* Main loop. */
- do {
- u32 dist, len = 0;
- const void *wnd;
- u64 hash;
-
- hash = ((lz77_read64(srcp) << 24) * 889523592379ULL) >> (64 - LZ77_HASH_LOG);
- wnd = src + htable[hash];
- htable[hash] = srcp - src;
- dist = srcp - wnd;
-
- if (dist && dist < LZ77_MATCH_MAX_DIST)
- len = lz77_match_len(wnd, srcp, end);
+ LZ77_PREFETCH(srcp + LZ77_RSTEP_SIZE);
- if (len < LZ77_MATCH_MIN_LEN) {
- lz77_write8(dstp, lz77_read8(srcp));
-
- dstp++;
- srcp++;
-
- flag <<= 1;
- flag_count++;
- if (flag_count == 32) {
- lz77_write32(flag_pos, flag);
- flag_count = 0;
- flag_pos = dstp;
- dstp += 4;
- }
-
- continue;
- }
+ hash = lz77_hash(lz77_read32(srcp++));
+ htable[hash] = 0;
+ hash = lz77_hash(lz77_read32(srcp));
- dstp = lz77_write_match(dstp, &nib, dist, len);
+ /*
+ * Main loop.
+ *
+ * @dlen is >= lz77_compressed_alloc_size(), so run without bound-checking @dstp.
+ *
+ * This code was crafted in a way to best utilise fetch-decode-execute CPU flow.
+ * Any attempt to optimize it, or even organize it, can lead to huge performance loss.
+ */
+ do {
+ const void *match, *next = srcp;
+ u32 len, step = 1, skip = 1U << LZ77_SKIP_TRIGGER;
+
+ /* Match finding (hot path -- don't change the read/check/write order). */
+ do {
+ const u32 cur_hash = hash;
+
+ srcp = next;
+ next += step;
+ step = (skip++ >> LZ77_SKIP_TRIGGER);
+ if (unlikely(next > rlim))
+ goto out;
+
+ hash = lz77_hash(lz77_read32(next));
+ match = src + htable[cur_hash];
+ htable[cur_hash] = srcp - src;
+ } while (likely(match + LZ77_MATCH_MAX_DIST < srcp) ||
+ lz77_read32(match) != lz77_read32(srcp));
+
+ dstp = lz77_encode_literals(anchor, srcp, dstp, &flag, &flag_count, &flag_pos);
+ len = lz77_match_len(match, srcp, end);
+ dstp = lz77_encode_match(dstp, &nib, srcp - match, len);
srcp += len;
+ anchor = srcp;
+
+ LZ77_PREFETCH(srcp);
flag = (flag << 1) | 1;
flag_count++;
- if (flag_count == 32) {
+ if (flag_count == LZ77_FLAG_MAX) {
lz77_write32(flag_pos, flag);
flag_count = 0;
flag_pos = dstp;
dstp += 4;
}
- } while (likely(srcp + LZ77_STEP_SIZE <= end));
-
- while (srcp < end) {
- u32 c = umin(end - srcp, 32 - flag_count);
- memcpy(dstp, srcp, c);
+ if (unlikely(srcp > rlim))
+ break;
- dstp += c;
- srcp += c;
-
- flag <<= c;
- flag_count += c;
- if (flag_count == 32) {
- lz77_write32(flag_pos, flag);
- flag_count = 0;
- flag_pos = dstp;
- dstp += 4;
- }
- }
+ /* Prepare for next loop. */
+ hash = lz77_hash(lz77_read32(srcp));
+ } while (srcp < end);
+out:
+ dstp = lz77_encode_literals(anchor, end, dstp, &flag, &flag_count, &flag_pos);
- flag <<= (32 - flag_count);
- flag |= (1UL << (32 - flag_count)) - 1;
+ flag_count = LZ77_FLAG_MAX - flag_count;
+ flag <<= flag_count;
+ flag |= (1UL << flag_count) - 1;
lz77_write32(flag_pos, flag);
*dlen = dstp - dst;
diff --git a/fs/smb/client/compress/lz77.h b/fs/smb/client/compress/lz77.h
index 2603eab9e071..4e570846aefa 100644
--- a/fs/smb/client/compress/lz77.h
+++ b/fs/smb/client/compress/lz77.h
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2024, SUSE LLC
+ * Copyright (C) 2024-2026, SUSE LLC
*
* Authors: Enzo Matsumiya <ematsumiya@suse.de>
*
@@ -39,5 +39,5 @@ static __always_inline u32 lz77_compressed_alloc_size(const u32 size)
return size + (size >> 3) + 8;
}
-int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen);
+int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen);
#endif /* _SMB_COMPRESS_LZ77_H */
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 6/8] smb: client: compress: add code docs to lz77.c
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
` (3 preceding siblings ...)
2026-04-13 19:07 ` [PATCH 5/8] smb: client: compress: LZ77 optimizations Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 7/8] smb: client: compress: add compress/common.h Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 8/8] smb: common: add SMB3_COMPRESS_MAX_ALGS Enzo Matsumiya
6 siblings, 0 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
Document parts of the code, especially the apparently
non-sense parts.
Other:
- change pointer increment constants to sizeof() values
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/lz77.c | 81 +++++++++++++++++++++++++++++++----
1 file changed, 73 insertions(+), 8 deletions(-)
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 96744f52e364..7365d0f97396 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -15,6 +15,20 @@
/*
* Compression parameters.
+ *
+ * LZ77_MATCH_MAX_DIST: Farthest back a match can be from current position (can be 1 - 8K).
+ * LZ77_HASH_LOG:
+ * LZ77_HASH_SIZE: ilog2 hash size (recommended to be 13 - 18, default 15 (hash size
+ * 32k)).
+ * LZ77_RSTEP_SIZE: Number of bytes to read from input buffer for hashing and initial
+ * match check (default 4 bytes, this effectivelly makes this the min
+ * match len).
+ * LZ77_MSTEP_SIZE: Number of bytes to extend-compare a found match (default 8 bytes).
+ * LZ77_SKIP_TRIGGER: ilog2 value for adaptive skipping, i.e. to progressively skip input
+ * bytes when we can't find matches. Default is 4.
+ * Higher values (>0) will decrease compression time, but will result
+ * in worse compression ratio. Lower values will give better
+ * compression ratio (more matches found), but will increase time.
*/
#define LZ77_MATCH_MAX_DIST SZ_8K
#define LZ77_HASH_LOG 15
@@ -86,6 +100,19 @@ static __always_inline u32 lz77_match_len(const void *match, const void *cur, co
return (cur - start);
}
+/**
+ * lz77_encode_match() - Match encoding.
+ * @dst: compressed buffer
+ * @nib: pointer to an address in @dst
+ * @dist: match distance
+ * @len: match length
+ *
+ * Assumes all args were previously checked.
+ *
+ * Return: @dst advanced to new position
+ *
+ * Ref: MS-XCA 2.3.4 "Plain LZ77 Compression Algorithm Details" - "Processing"
+ */
static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist, u32 len)
{
len -= 3;
@@ -95,12 +122,12 @@ static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist,
if (len < 7) {
lz77_write16(dst, dist + len);
- return dst + 2;
+ return dst + sizeof(u16);
}
dist |= 7;
lz77_write16(dst, dist);
- dst += 2;
+ dst += sizeof(u16);
len -= 7;
if (!*nib) {
@@ -130,16 +157,32 @@ static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist,
if (len <= 0xffff) {
lz77_write16(dst, len);
- return dst + 2;
+ return dst + sizeof(u16);
}
lz77_write16(dst, 0);
- dst += 2;
+ dst += sizeof(u16);
lz77_write32(dst, len);
- return dst + 4;
+ return dst + sizeof(u32);
}
+/**
+ * lz77_encode_literals() - Literals encoding.
+ * @start: where to start copying literals (uncompressed buffer)
+ * @end: when to stop copying (uncompressed buffer)
+ * @dst: compressed buffer
+ * @f: pointer to current flag value
+ * @fc: pointer to current flag count
+ * @fp: pointer to current flag address
+ *
+ * Batch copy literals from @start to @dst, updating flag values accordingly.
+ * Assumes all args were previously checked.
+ *
+ * Return: @dst advanced to new position
+ *
+ * MS-XCA 2.3.4 "Plain LZ77 Compression Algorithm Details" - "Processing"
+ */
static __always_inline void *lz77_encode_literals(const void *start, const void *end, void *dst,
long *f, u32 *fc, void **fp)
{
@@ -160,7 +203,7 @@ static __always_inline void *lz77_encode_literals(const void *start, const void
lz77_write32(*fp, *f);
*fc = 0;
*fp = dst;
- dst += 4;
+ dst += sizeof(u32);
}
} while (start < end);
@@ -188,7 +231,7 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
rlim = end - LZ77_MSTEP_SIZE; /* read limit (for lz77_match_len()) */
dstp = dst;
flag_pos = dstp;
- dstp += 4;
+ dstp += sizeof(u32);
nib = NULL;
htable = kvcalloc(LZ77_HASH_SIZE, sizeof(*htable), GFP_KERNEL);
@@ -197,6 +240,10 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
LZ77_PREFETCH(srcp + LZ77_RSTEP_SIZE);
+ /*
+ * Adjust @srcp so we don't get a false positive match on first iteration.
+ * Then prepare hash for first loop iteration (don't advance @srcp again).
+ */
hash = lz77_hash(lz77_read32(srcp++));
htable[hash] = 0;
hash = lz77_hash(lz77_read32(srcp));
@@ -219,6 +266,14 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
srcp = next;
next += step;
+
+ /*
+ * Adaptive skipping.
+ *
+ * Increment @step every (1 << LZ77_SKIP_TRIGGER, 16 in our case) bytes
+ * without a match.
+ * Reset to 1 when a match is found.
+ */
step = (skip++ >> LZ77_SKIP_TRIGGER);
if (unlikely(next > rlim))
goto out;
@@ -229,6 +284,16 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
} while (likely(match + LZ77_MATCH_MAX_DIST < srcp) ||
lz77_read32(match) != lz77_read32(srcp));
+ /*
+ * Match found. Warm/cold path; begin parsing @srcp and writing to @dstp:
+ * - flush literals
+ * - compute match length (*)
+ * - encode match
+ *
+ * (*) Current minimum match length is defined by the memory read size above, so
+ * here we already know that we have 4 matching bytes, but it's just faster to
+ * redundantly compute it again in lz77_match_len() than to adjust pointers/len.
+ */
dstp = lz77_encode_literals(anchor, srcp, dstp, &flag, &flag_count, &flag_pos);
len = lz77_match_len(match, srcp, end);
dstp = lz77_encode_match(dstp, &nib, srcp - match, len);
@@ -243,7 +308,7 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
lz77_write32(flag_pos, flag);
flag_count = 0;
flag_pos = dstp;
- dstp += 4;
+ dstp += sizeof(u32);
}
if (unlikely(srcp > rlim))
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 7/8] smb: client: compress: add compress/common.h
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
` (4 preceding siblings ...)
2026-04-13 19:07 ` [PATCH 6/8] smb: client: compress: add code docs to lz77.c Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
2026-04-20 21:24 ` Nathan Chancellor
2026-04-13 19:07 ` [PATCH 8/8] smb: common: add SMB3_COMPRESS_MAX_ALGS Enzo Matsumiya
6 siblings, 1 reply; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
Add compress/common.h to aggregate helpers and definitions that will be
shared with other compression algorithms.
Also add a few build time checks for proper support.
Changes:
- update affected call paths in compress/lz77.c
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/client/compress/common.h | 152 ++++++++++++++++++++++++++++++++
fs/smb/client/compress/lz77.c | 122 +++++--------------------
2 files changed, 175 insertions(+), 99 deletions(-)
create mode 100644 fs/smb/client/compress/common.h
diff --git a/fs/smb/client/compress/common.h b/fs/smb/client/compress/common.h
new file mode 100644
index 000000000000..b5ccf5debd22
--- /dev/null
+++ b/fs/smb/client/compress/common.h
@@ -0,0 +1,152 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026, SUSE LLC
+ *
+ * Authors: Enzo Matsumiya <ematsumiya@suse.de>
+ *
+ * Common helpers and definitions for compression/decompression.
+ *
+ * Naming convention:
+ * - smb_compress_*: MS-SMB2 related API
+ * - compress_*: Compression internals
+ * - <alg>_*: Compression algorithm specifics
+ *
+ * Upper case for macros and constants, lower case for everything else.
+ */
+#ifndef _COMPRESS_COMMON_H
+#define _COMPRESS_COMMON_H
+
+#include <linux/kernel.h>
+#include <linux/count_zeros.h>
+#include <linux/string.h>
+#include <linux/sizes.h>
+#include <linux/slab.h>
+
+/*
+ * Build time checks/asserts.
+ * These are assumptions/expectations for all algorithms implemented.
+ */
+#ifndef __LITTLE_ENDIAN /* TODO */
+# error "SMB3 compression is only supported on little endian architectures"
+#endif /* !__LITTLE_ENDIAN */
+
+#if BITS_PER_LONG < 64 /* TODO */
+# error "SMB3 compression is only supported on 64-bit architectures"
+#endif /* BITS_PER_LONG < 64 */
+
+/* Build time double checks (probably unnecessary) */
+static_assert(sizeof(u16) == 2);
+static_assert(sizeof(u32) == 4);
+static_assert(sizeof(u64) == 8);
+static_assert(sizeof(size_t) == 8);
+
+#ifdef CONFIG_CIFS_DEBUG2
+# define COMPRESS_DEBUG
+#endif /* CONFIG_CIFS_DEBUG2 */
+
+#define COMPRESS_LOG_FMT "CIFS: %s:%d %s(): "
+
+#ifdef COMPRESS_DEBUG
+# define compress_log(fmt, ...) \
+ pr_err(COMPRESS_LOG_FMT fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__)
+
+/* Make sure to handle assertion failures! */
+# define COMPRESS_ASSERT(cond) \
+ !WARN_ONCE(!(cond), COMPRESS_LOG_FMT "assertion failed: %s\n", \
+ __FILE__, __LINE__, __func__, #cond)
+#else /* COMPRESS_DEBUG */
+# define compress_log(...)
+/* XXX: should not fail silently on non-debug? */
+# define COMPRESS_ASSERT(cond) likely(cond)
+#endif /* !COMPRESS_DEBUG */
+
+/*
+ * Memory ops helpers.
+ */
+#undef MEM_UNALIGNED_READ
+#undef MEM_UNALIGNED_WRITE
+
+/* Read prefetch */
+#define MEM_PREFETCH(ptr) __builtin_prefetch((ptr), 0, 3)
+
+#ifdef CONFIG_X86
+# define MEM_UNALIGNED_READ(ptr, t) (*(const t *)(ptr))
+# define MEM_UNALIGNED_WRITE(ptr, v, t) (*(t *)(ptr) = (t)(v))
+#else
+# include <linux/unaligned.h>
+# define MEM_UNALIGNED_READ(ptr, t) get_unaligned((const t *)(ptr))
+# define MEM_UNALIGNED_WRITE(ptr, v, t) put_unaligned((v), (t *)(ptr))
+#endif /* !CONFIG_X86 */
+
+#define mem_read8(ptr) MEM_UNALIGNED_READ(ptr, u8)
+#define mem_read16(ptr) MEM_UNALIGNED_READ(ptr, u16)
+#define mem_read32(ptr) MEM_UNALIGNED_READ(ptr, u32)
+#define mem_read64(ptr) MEM_UNALIGNED_READ(ptr, u64)
+#define mem_write8(ptr, v) MEM_UNALIGNED_WRITE(ptr, v, u8)
+#define mem_write16(ptr, v) MEM_UNALIGNED_WRITE(ptr, v, u16)
+#define mem_write32(ptr, v) MEM_UNALIGNED_WRITE(ptr, v, u32)
+/* mem_write64() not implemented because it's not used anywhere. */
+
+/*
+ * COMPRESS_RSTEP_SIZE: Number of bytes to read from input buffer for hashing and initial
+ * match check (default 4 bytes).
+ * COMPRESS_MSTEP_SIZE: Number of bytes to extend-compare a found match (default 8 bytes).
+ */
+#define COMPRESS_RSTEP_SIZE sizeof(u32)
+#define COMPRESS_MSTEP_SIZE sizeof(u64)
+
+static __always_inline size_t mem_match_len(const void *match, const void *cur, const void *end)
+{
+ const void *start = cur;
+
+ /* Callers must ensure @cur + COMPRESS_MSTEP_SIZE < @end. */
+ do {
+ const u64 diff = mem_read64(cur) ^ mem_read64(match);
+
+ if (!diff) {
+ cur += COMPRESS_MSTEP_SIZE;
+ match += COMPRESS_MSTEP_SIZE;
+
+ continue;
+ }
+
+ /* This computes the number of common bytes in @diff. */
+ cur += count_trailing_zeros(diff) >> 3;
+
+ return (cur - start);
+ } while (likely(cur + COMPRESS_MSTEP_SIZE <= end));
+
+ /* Fallback to byte-by-byte comparison for last bytes (< COMPRESS_MSTEP_SIZE). */
+ while (cur < end && mem_read8(match) == mem_read8(cur)) {
+ cur++;
+ match++;
+ }
+
+ return (cur - start);
+}
+
+/*
+ * Hashing
+ *
+ * Same for all algorithms.
+ *
+ * XXX: these are fixed for now, might make them tunables in the future.
+ */
+
+/*
+ * COMPRESS_HASH_LOG: ilog2 hash size (recommended to be 13 - 18, default 15)
+ * COMPRESS_HASH_SIZE: Hashtable size (default is 32k (1 << COMPRESS_HASH_LOG))).
+ */
+#define COMPRESS_HASH_LOG 15
+#define COMPRESS_HASH_SIZE (1U << COMPRESS_HASH_LOG)
+
+static __always_inline u32 compress_hash(const u32 v)
+{
+ return ((v ^ 0x9E3779B9U) * 0x85EBCA6BU) >> (32 - COMPRESS_HASH_LOG);
+}
+
+static __always_inline u32 compress_hash_ptr(const void *ptr)
+{
+ return compress_hash(mem_read32(ptr));
+}
+#endif /* _COMPRESS_COMMON_H */
diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
index 7365d0f97396..d8c73f7f7483 100644
--- a/fs/smb/client/compress/lz77.c
+++ b/fs/smb/client/compress/lz77.c
@@ -11,19 +11,13 @@
#include <linux/count_zeros.h>
#include <linux/unaligned.h>
+#include "common.h"
#include "lz77.h"
/*
* Compression parameters.
*
* LZ77_MATCH_MAX_DIST: Farthest back a match can be from current position (can be 1 - 8K).
- * LZ77_HASH_LOG:
- * LZ77_HASH_SIZE: ilog2 hash size (recommended to be 13 - 18, default 15 (hash size
- * 32k)).
- * LZ77_RSTEP_SIZE: Number of bytes to read from input buffer for hashing and initial
- * match check (default 4 bytes, this effectivelly makes this the min
- * match len).
- * LZ77_MSTEP_SIZE: Number of bytes to extend-compare a found match (default 8 bytes).
* LZ77_SKIP_TRIGGER: ilog2 value for adaptive skipping, i.e. to progressively skip input
* bytes when we can't find matches. Default is 4.
* Higher values (>0) will decrease compression time, but will result
@@ -31,75 +25,10 @@
* compression ratio (more matches found), but will increase time.
*/
#define LZ77_MATCH_MAX_DIST SZ_8K
-#define LZ77_HASH_LOG 15
-#define LZ77_HASH_SIZE (1 << LZ77_HASH_LOG)
-#define LZ77_RSTEP_SIZE sizeof(u32)
-#define LZ77_MSTEP_SIZE sizeof(u64)
#define LZ77_SKIP_TRIGGER 4
-#define LZ77_PREFETCH(ptr) __builtin_prefetch((ptr), 0, 3)
#define LZ77_FLAG_MAX 32
-static __always_inline u8 lz77_read8(const u8 *ptr)
-{
- return get_unaligned(ptr);
-}
-
-static __always_inline u32 lz77_read32(const u32 *ptr)
-{
- return get_unaligned(ptr);
-}
-
-static __always_inline u64 lz77_read64(const u64 *ptr)
-{
- return get_unaligned(ptr);
-}
-
-static __always_inline void lz77_write8(u8 *ptr, u8 v)
-{
- put_unaligned(v, ptr);
-}
-
-static __always_inline void lz77_write16(u16 *ptr, u16 v)
-{
- put_unaligned_le16(v, ptr);
-}
-
-static __always_inline void lz77_write32(u32 *ptr, u32 v)
-{
- put_unaligned_le32(v, ptr);
-}
-
-static __always_inline u32 lz77_match_len(const void *match, const void *cur, const void *end)
-{
- const void *start = cur;
-
- /* Safe for a do/while because otherwise we wouldn't reach here from the main loop. */
- do {
- const u64 diff = lz77_read64(cur) ^ lz77_read64(match);
-
- if (!diff) {
- cur += LZ77_MSTEP_SIZE;
- match += LZ77_MSTEP_SIZE;
-
- continue;
- }
-
- /* This computes the number of common bytes in @diff. */
- cur += count_trailing_zeros(diff) >> 3;
-
- return (cur - start);
- } while (likely(cur + LZ77_MSTEP_SIZE <= end));
-
- /* Fallback to byte-by-byte comparison for last <8 bytes. */
- while (cur < end && lz77_read8(cur) == lz77_read8(match)) {
- cur++;
- match++;
- }
-
- return (cur - start);
-}
-
/**
* lz77_encode_match() - Match encoding.
* @dst: compressed buffer
@@ -120,24 +49,24 @@ static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist,
dist <<= 3;
if (len < 7) {
- lz77_write16(dst, dist + len);
+ mem_write16(dst, dist + len);
return dst + sizeof(u16);
}
dist |= 7;
- lz77_write16(dst, dist);
+ mem_write16(dst, dist);
dst += sizeof(u16);
len -= 7;
if (!*nib) {
- lz77_write8(dst, umin(len, 15));
+ mem_write8(dst, umin(len, 15));
*nib = dst;
dst++;
} else {
u8 *b = *nib;
- lz77_write8(b, *b | umin(len, 15) << 4);
+ mem_write8(b, *b | umin(len, 15) << 4);
*nib = NULL;
}
@@ -146,23 +75,23 @@ static __always_inline void *lz77_encode_match(void *dst, void **nib, u16 dist,
len -= 15;
if (len < 255) {
- lz77_write8(dst, len);
+ mem_write8(dst, len);
return dst + 1;
}
- lz77_write8(dst, 0xff);
+ mem_write8(dst, 0xff);
dst++;
len += 7 + 15;
if (len <= 0xffff) {
- lz77_write16(dst, len);
+ mem_write16(dst, len);
return dst + sizeof(u16);
}
- lz77_write16(dst, 0);
+ mem_write16(dst, 0);
dst += sizeof(u16);
- lz77_write32(dst, len);
+ mem_write32(dst, len);
return dst + sizeof(u32);
}
@@ -200,7 +129,7 @@ static __always_inline void *lz77_encode_literals(const void *start, const void
*f <<= len;
*fc += len;
if (*fc == LZ77_FLAG_MAX) {
- lz77_write32(*fp, *f);
+ mem_write32(*fp, *f);
*fc = 0;
*fp = dst;
dst += sizeof(u32);
@@ -210,11 +139,6 @@ static __always_inline void *lz77_encode_literals(const void *start, const void
return dst;
}
-static __always_inline u32 lz77_hash(const u32 v)
-{
- return ((v ^ 0x9E3779B9) * 0x85EBCA6B) >> (32 - LZ77_HASH_LOG);
-}
-
noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen)
{
const void *srcp, *rlim, *end, *anchor;
@@ -228,25 +152,25 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
srcp = anchor = src;
end = srcp + slen; /* absolute end */
- rlim = end - LZ77_MSTEP_SIZE; /* read limit (for lz77_match_len()) */
+ rlim = end - COMPRESS_MSTEP_SIZE; /* read limit (for mem_match_len()) */
dstp = dst;
flag_pos = dstp;
dstp += sizeof(u32);
nib = NULL;
- htable = kvcalloc(LZ77_HASH_SIZE, sizeof(*htable), GFP_KERNEL);
+ htable = kvcalloc(COMPRESS_HASH_SIZE, sizeof(*htable), GFP_KERNEL);
if (!htable)
return -ENOMEM;
- LZ77_PREFETCH(srcp + LZ77_RSTEP_SIZE);
+ MEM_PREFETCH(srcp + COMPRESS_RSTEP_SIZE);
/*
* Adjust @srcp so we don't get a false positive match on first iteration.
* Then prepare hash for first loop iteration (don't advance @srcp again).
*/
- hash = lz77_hash(lz77_read32(srcp++));
+ hash = compress_hash_ptr(srcp++);
htable[hash] = 0;
- hash = lz77_hash(lz77_read32(srcp));
+ hash = compress_hash_ptr(srcp);
/*
* Main loop.
@@ -278,11 +202,11 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
if (unlikely(next > rlim))
goto out;
- hash = lz77_hash(lz77_read32(next));
+ hash = compress_hash_ptr(next);
match = src + htable[cur_hash];
htable[cur_hash] = srcp - src;
} while (likely(match + LZ77_MATCH_MAX_DIST < srcp) ||
- lz77_read32(match) != lz77_read32(srcp));
+ mem_read32(match) != mem_read32(srcp));
/*
* Match found. Warm/cold path; begin parsing @srcp and writing to @dstp:
@@ -295,17 +219,17 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
* redundantly compute it again in lz77_match_len() than to adjust pointers/len.
*/
dstp = lz77_encode_literals(anchor, srcp, dstp, &flag, &flag_count, &flag_pos);
- len = lz77_match_len(match, srcp, end);
+ len = mem_match_len(match, srcp, end);
dstp = lz77_encode_match(dstp, &nib, srcp - match, len);
srcp += len;
anchor = srcp;
- LZ77_PREFETCH(srcp);
+ MEM_PREFETCH(srcp);
flag = (flag << 1) | 1;
flag_count++;
if (flag_count == LZ77_FLAG_MAX) {
- lz77_write32(flag_pos, flag);
+ mem_write32(flag_pos, flag);
flag_count = 0;
flag_pos = dstp;
dstp += sizeof(u32);
@@ -315,7 +239,7 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
break;
/* Prepare for next loop. */
- hash = lz77_hash(lz77_read32(srcp));
+ hash = compress_hash_ptr(srcp);
} while (srcp < end);
out:
dstp = lz77_encode_literals(anchor, end, dstp, &flag, &flag_count, &flag_pos);
@@ -323,7 +247,7 @@ noinline int lz77_compress(const void *src, const u32 slen, void *dst, u32 *dlen
flag_count = LZ77_FLAG_MAX - flag_count;
flag <<= flag_count;
flag |= (1UL << flag_count) - 1;
- lz77_write32(flag_pos, flag);
+ mem_write32(flag_pos, flag);
*dlen = dstp - dst;
kvfree(htable);
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH 8/8] smb: common: add SMB3_COMPRESS_MAX_ALGS
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
` (5 preceding siblings ...)
2026-04-13 19:07 ` [PATCH 7/8] smb: client: compress: add compress/common.h Enzo Matsumiya
@ 2026-04-13 19:07 ` Enzo Matsumiya
6 siblings, 0 replies; 11+ messages in thread
From: Enzo Matsumiya @ 2026-04-13 19:07 UTC (permalink / raw)
To: linux-cifs
Cc: smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
Set it to number of currently defined algorithms (6 as of now).
Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
fs/smb/common/smb2pdu.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/fs/smb/common/smb2pdu.h b/fs/smb/common/smb2pdu.h
index e482c86ceb00..cda71964ed01 100644
--- a/fs/smb/common/smb2pdu.h
+++ b/fs/smb/common/smb2pdu.h
@@ -510,6 +510,8 @@ struct smb2_encryption_neg_context {
/* Pattern scanning algorithm See MS-SMB2 3.1.4.4.1 */
#define SMB3_COMPRESS_PATTERN cpu_to_le16(0x0004) /* Pattern_V1 */
#define SMB3_COMPRESS_LZ4 cpu_to_le16(0x0005)
+/* Account for NONE for easier array indexing */
+#define SMB3_COMPRESS_MAX_ALGS 6
/* Compression Flags */
#define SMB2_COMPRESSION_CAPABILITIES_FLAG_NONE cpu_to_le32(0x00000000)
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag
2026-04-13 19:07 ` [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag Enzo Matsumiya
@ 2026-04-19 1:35 ` Steve French
0 siblings, 0 replies; 11+ messages in thread
From: Steve French @ 2026-04-19 1:35 UTC (permalink / raw)
To: Enzo Matsumiya
Cc: linux-cifs, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
merged into cifs-2.6.git for-next
On Mon, Apr 13, 2026 at 2:07 PM Enzo Matsumiya <ematsumiya@suse.de> wrote:
>
> End-of-stream flag could lead to UB because of int promotion
> (overwriting signed bit).
>
> Fix it by changing operand from '1' to '1UL'.
>
> Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
> ---
> fs/smb/client/compress/lz77.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/smb/client/compress/lz77.c b/fs/smb/client/compress/lz77.c
> index 16c7d8f3ef17..c1e7fada6e61 100644
> --- a/fs/smb/client/compress/lz77.c
> +++ b/fs/smb/client/compress/lz77.c
> @@ -216,7 +216,7 @@ noinline int lz77_compress(const void *src, u32 slen, void *dst, u32 *dlen)
> }
>
> flag <<= (32 - flag_count);
> - flag |= (1 << (32 - flag_count)) - 1;
> + flag |= (1UL << (32 - flag_count)) - 1;
> lz77_write32(flag_pos, flag);
>
> *dlen = dstp - dst;
> --
> 2.53.0
>
--
Thanks,
Steve
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 7/8] smb: client: compress: add compress/common.h
2026-04-13 19:07 ` [PATCH 7/8] smb: client: compress: add compress/common.h Enzo Matsumiya
@ 2026-04-20 21:24 ` Nathan Chancellor
2026-04-20 21:31 ` Steve French
0 siblings, 1 reply; 11+ messages in thread
From: Nathan Chancellor @ 2026-04-20 21:24 UTC (permalink / raw)
To: Enzo Matsumiya
Cc: linux-cifs, smfrench, pc, ronniesahlberg, sprasad, tom, bharathsm,
henrique.carvalho
Hi Enzo,
On Mon, Apr 13, 2026 at 04:07:12PM -0300, Enzo Matsumiya wrote:
> Add compress/common.h to aggregate helpers and definitions that will be
> shared with other compression algorithms.
>
> Also add a few build time checks for proper support.
>
> Changes:
> - update affected call paths in compress/lz77.c
>
> Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
> ---
...
> diff --git a/fs/smb/client/compress/common.h b/fs/smb/client/compress/common.h
> new file mode 100644
> index 000000000000..b5ccf5debd22
> --- /dev/null
> +++ b/fs/smb/client/compress/common.h
...
> +/*
> + * Build time checks/asserts.
> + * These are assumptions/expectations for all algorithms implemented.
> + */
> +#ifndef __LITTLE_ENDIAN /* TODO */
> +# error "SMB3 compression is only supported on little endian architectures"
> +#endif /* !__LITTLE_ENDIAN */
> +
> +#if BITS_PER_LONG < 64 /* TODO */
> +# error "SMB3 compression is only supported on 64-bit architectures"
> +#endif /* BITS_PER_LONG < 64 */
> +
> +/* Build time double checks (probably unnecessary) */
> +static_assert(sizeof(u16) == 2);
> +static_assert(sizeof(u32) == 4);
> +static_assert(sizeof(u64) == 8);
> +static_assert(sizeof(size_t) == 8);
Steve pushed this change into -next as commit 20a248d5e77a ("smb:
client: compress: add compress/common.h"), where it breaks the build for
many 32-bit architectures for obvious reasons:
$ make -skj"$(nproc)" ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- mrproper allmodconfig fs/smb/
In file included from fs/smb/client/compress/lz77.c:14:
fs/smb/client/compress/common.h:34:3: error: #error "SMB3 compression is only supported on 64-bit architectures"
34 | # error "SMB3 compression is only supported on 64-bit architectures"
| ^~~~~
In file included from include/linux/init.h:5,
from include/linux/printk.h:6,
from include/asm-generic/bug.h:31,
from arch/arm/include/asm/bug.h:60,
from include/linux/bug.h:5,
from include/linux/slab.h:15,
from fs/smb/client/compress/lz77.c:9:
include/linux/build_bug.h:80:41: error: static assertion failed: "sizeof(size_t) == 8"
80 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
| ^~~~~~~~~~~~~~
include/linux/build_bug.h:79:34: note: in expansion of macro '__static_assert'
79 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
| ^~~~~~~~~~~~~~~
fs/smb/client/compress/common.h:41:1: note: in expansion of macro 'static_assert'
41 | static_assert(sizeof(size_t) == 8);
| ^~~~~~~~~~~~~
It is unacceptable for the build to break in this manner in my opinion.
You should really use Kconfig to gate this code in a proper manner and
not emit any errors or warnings when it is unsupported.
Cheers,
Nathan
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 7/8] smb: client: compress: add compress/common.h
2026-04-20 21:24 ` Nathan Chancellor
@ 2026-04-20 21:31 ` Steve French
0 siblings, 0 replies; 11+ messages in thread
From: Steve French @ 2026-04-20 21:31 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Enzo Matsumiya, linux-cifs, pc, ronniesahlberg, sprasad, tom,
bharathsm, henrique.carvalho
On Mon, Apr 20, 2026 at 4:24 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> Hi Enzo,
>
> On Mon, Apr 13, 2026 at 04:07:12PM -0300, Enzo Matsumiya wrote:
> > Add compress/common.h to aggregate helpers and definitions that will be
> > shared with other compression algorithms.
> >
> > Also add a few build time checks for proper support.
> >
> > Changes:
> > - update affected call paths in compress/lz77.c
> >
> > Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
> > ---
> ...
> > diff --git a/fs/smb/client/compress/common.h b/fs/smb/client/compress/common.h
> > new file mode 100644
> > index 000000000000..b5ccf5debd22
> > --- /dev/null
> > +++ b/fs/smb/client/compress/common.h
> ...
> > +/*
> > + * Build time checks/asserts.
> > + * These are assumptions/expectations for all algorithms implemented.
> > + */
> > +#ifndef __LITTLE_ENDIAN /* TODO */
> > +# error "SMB3 compression is only supported on little endian architectures"
> > +#endif /* !__LITTLE_ENDIAN */
> > +
> > +#if BITS_PER_LONG < 64 /* TODO */
> > +# error "SMB3 compression is only supported on 64-bit architectures"
> > +#endif /* BITS_PER_LONG < 64 */
> > +
> > +/* Build time double checks (probably unnecessary) */
> > +static_assert(sizeof(u16) == 2);
> > +static_assert(sizeof(u32) == 4);
> > +static_assert(sizeof(u64) == 8);
> > +static_assert(sizeof(size_t) == 8);
>
> Steve pushed this change into -next as commit 20a248d5e77a ("smb:
> client: compress: add compress/common.h"), where it breaks the build for
> many 32-bit architectures for obvious reasons:
>
> $ make -skj"$(nproc)" ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- mrproper allmodconfig fs/smb/
> In file included from fs/smb/client/compress/lz77.c:14:
> fs/smb/client/compress/common.h:34:3: error: #error "SMB3 compression is only supported on 64-bit architectures"
> 34 | # error "SMB3 compression is only supported on 64-bit architectures"
> | ^~~~~
> In file included from include/linux/init.h:5,
> from include/linux/printk.h:6,
> from include/asm-generic/bug.h:31,
> from arch/arm/include/asm/bug.h:60,
> from include/linux/bug.h:5,
> from include/linux/slab.h:15,
> from fs/smb/client/compress/lz77.c:9:
> include/linux/build_bug.h:80:41: error: static assertion failed: "sizeof(size_t) == 8"
> 80 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
> | ^~~~~~~~~~~~~~
> include/linux/build_bug.h:79:34: note: in expansion of macro '__static_assert'
> 79 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
> | ^~~~~~~~~~~~~~~
> fs/smb/client/compress/common.h:41:1: note: in expansion of macro 'static_assert'
> 41 | static_assert(sizeof(size_t) == 8);
> | ^~~~~~~~~~~~~
>
> It is unacceptable for the build to break in this manner in my opinion.
> You should really use Kconfig to gate this code in a proper manner and
> not emit any errors or warnings when it is unsupported.
>
Thx for pointing this out. I have removed the patch from cifs-2.6.git for-next
Let me know if you spot any additional problems
--
Thanks,
Steve
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-04-20 21:31 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-13 19:07 [PATCH 1/8] smb: client: compress: fix buffer overrun in lz77_compress() Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 2/8] smb: client: compress: fix bad encoding on last LZ77 flag Enzo Matsumiya
2026-04-19 1:35 ` Steve French
2026-04-13 19:07 ` [PATCH 3/8] smb: client: compress: fix counting in LZ77 match finding Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 4/8] smb: client: compress: increase LZ77_MATCH_MAX_DIST Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 5/8] smb: client: compress: LZ77 optimizations Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 6/8] smb: client: compress: add code docs to lz77.c Enzo Matsumiya
2026-04-13 19:07 ` [PATCH 7/8] smb: client: compress: add compress/common.h Enzo Matsumiya
2026-04-20 21:24 ` Nathan Chancellor
2026-04-20 21:31 ` Steve French
2026-04-13 19:07 ` [PATCH 8/8] smb: common: add SMB3_COMPRESS_MAX_ALGS Enzo Matsumiya
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.