[RFC PATCH v2 14/22] cramfs: Allocate the read buffers at init time

From: Eric Curtin

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


From: Eric Curtin <eric.curtin@xxxxxxxxxx>

With arm64 boot-time page size selection PAGE_SIZE is not a compile-time
constant, so the statically sized read_buffers[] array, whose size is
BLKS_PER_BUF * PAGE_SIZE, cannot be declared as before.

Make read_buffers[] an array of pointers and allocate each buffer with
kmalloc() in cramfs_alloc_read_buffers(), called from init_cramfs_fs().
The buffers are only needed when CONFIG_CRAMFS_BLOCKDEV is enabled, so
nothing is allocated otherwise. Free them on the init error paths and on
module exit.

The buffers are now allocated from the slab instead of living in .bss.

Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
---
fs/cramfs/inode.c | 41 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index d4cd03f4f60d..49acb5afd6a4 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -181,7 +181,7 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
#define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)

-static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
+static unsigned char *read_buffers[READ_BUFFERS];
static unsigned buffer_blocknr[READ_BUFFERS];
static struct super_block *buffer_dev[READ_BUFFERS];
static int next_buffer;
@@ -987,16 +987,50 @@ static struct file_system_type cramfs_fs_type = {
};
MODULE_ALIAS_FS("cramfs");

+static void cramfs_free_read_buffers(void)
+{
+ int i;
+
+ for (i = 0; i < READ_BUFFERS; i++)
+ kfree(read_buffers[i]);
+}
+
+static int __init cramfs_alloc_read_buffers(void)
+{
+ int i;
+
+ if (!IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
+ return 0;
+
+ for (i = 0; i < READ_BUFFERS; i++) {
+ read_buffers[i] = kmalloc(BUFFER_SIZE, GFP_KERNEL);
+ if (!read_buffers[i]) {
+ cramfs_free_read_buffers();
+ return -ENOMEM;
+ }
+ }
+ return 0;
+}
+
static int __init init_cramfs_fs(void)
{
int rv;

- rv = cramfs_uncompress_init();
+ rv = cramfs_alloc_read_buffers();
if (rv < 0)
return rv;
+ rv = cramfs_uncompress_init();
+ if (rv < 0)
+ goto err_buffers;
rv = register_filesystem(&cramfs_fs_type);
if (rv < 0)
- cramfs_uncompress_exit();
+ goto err_uncompress;
+ return 0;
+
+err_uncompress:
+ cramfs_uncompress_exit();
+err_buffers:
+ cramfs_free_read_buffers();
return rv;
}

@@ -1004,6 +1038,7 @@ static void __exit exit_cramfs_fs(void)
{
cramfs_uncompress_exit();
unregister_filesystem(&cramfs_fs_type);
+ cramfs_free_read_buffers();
}

module_init(init_cramfs_fs)