[RFC PATCH v2 11/22] erofs: Make the decompressor bounce buffers flexible arrays

From: Eric Curtin

Date: Tue Dec 01 2026 - 22:17:02 EST


From: Eric Curtin <eric.curtin@xxxxxxxxxx>

With arm64 boot-time page size selection PAGE_SIZE is not a compile-time
constant, so it cannot be used as the size of the bounce[] member in the
deflate, lzma and zstd stream structures.

Turn bounce[] into a flexible array member in struct z_erofs_deflate,
z_erofs_lzma and z_erofs_zstd, move it to the end of struct
z_erofs_zstd, and allocate the structures with kzalloc_flex(*strm,
bounce, PAGE_SIZE) so that the run-time page size sets the buffer size.

In zdata.c, compute Z_EROFS_PCLUSTER_MAX_PAGES with PAGE_SIZE_MIN. It is
used as an upper bound and must be constant.

No functional change for compile-time page sizes.

Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
---
fs/erofs/decompressor_deflate.c | 4 ++--
fs/erofs/decompressor_lzma.c | 4 ++--
fs/erofs/decompressor_zstd.c | 4 ++--
fs/erofs/zdata.c | 2 +-
4 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/erofs/decompressor_deflate.c b/fs/erofs/decompressor_deflate.c
index 6167af1ffeb9..4cd441cb9070 100644
--- a/fs/erofs/decompressor_deflate.c
+++ b/fs/erofs/decompressor_deflate.c
@@ -5,7 +5,7 @@
struct z_erofs_deflate {
struct z_erofs_deflate *next;
struct z_stream_s z;
- u8 bounce[PAGE_SIZE];
+ u8 bounce[];
};

static DEFINE_SPINLOCK(z_erofs_deflate_lock);
@@ -71,7 +71,7 @@ static int z_erofs_load_deflate_config(struct super_block *sb,
++z_erofs_deflate_avail_strms) {
struct z_erofs_deflate *strm;

- strm = kzalloc_obj(*strm);
+ strm = kzalloc_flex(*strm, bounce, PAGE_SIZE);
if (!strm)
goto failed;
/* XXX: in-kernel zlib cannot customize windowbits */
diff --git a/fs/erofs/decompressor_lzma.c b/fs/erofs/decompressor_lzma.c
index 9d15f94cbee1..5a5897b12f83 100644
--- a/fs/erofs/decompressor_lzma.c
+++ b/fs/erofs/decompressor_lzma.c
@@ -6,7 +6,7 @@ struct z_erofs_lzma {
struct z_erofs_lzma *next;
struct xz_dec_microlzma *state;
unsigned int dict_size;
- u8 bounce[PAGE_SIZE];
+ u8 bounce[];
};

/* considering the LZMA performance, no need to use a lockless list for now */
@@ -56,7 +56,7 @@ static int __init z_erofs_lzma_init(void)
CONFIG_EROFS_FS_ZIP_LZMA_DEFAULT_MAX_STREAMS);

for (i = 0; i < z_erofs_lzma_nstrms; ++i) {
- struct z_erofs_lzma *strm = kzalloc_obj(*strm);
+ struct z_erofs_lzma *strm = kzalloc_flex(*strm, bounce, PAGE_SIZE);

if (!strm) {
z_erofs_lzma_exit();
diff --git a/fs/erofs/decompressor_zstd.c b/fs/erofs/decompressor_zstd.c
index ab318a2400f9..e9dc6eb67561 100644
--- a/fs/erofs/decompressor_zstd.c
+++ b/fs/erofs/decompressor_zstd.c
@@ -4,9 +4,9 @@

struct z_erofs_zstd {
struct z_erofs_zstd *next;
- u8 bounce[PAGE_SIZE];
void *wksp;
unsigned int wkspsz;
+ u8 bounce[];
};

static DEFINE_SPINLOCK(z_erofs_zstd_lock);
@@ -59,7 +59,7 @@ static int __init z_erofs_zstd_init(void)
++z_erofs_zstd_avail_strms) {
struct z_erofs_zstd *strm;

- strm = kzalloc_obj(*strm);
+ strm = kzalloc_flex(*strm, bounce, PAGE_SIZE);
if (!strm) {
z_erofs_zstd_exit();
return -ENOMEM;
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index e981e371d6c2..bc4a7133a097 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -10,7 +10,7 @@
#include <trace/events/erofs.h>

#define Z_EROFS_MAX_SYNC_DECOMPRESS_BYTES 12288
-#define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE)
+#define Z_EROFS_PCLUSTER_MAX_PAGES (Z_EROFS_PCLUSTER_MAX_SIZE / PAGE_SIZE_MIN)
#define Z_EROFS_INLINE_BVECS 2

struct z_erofs_bvec {