[PATCH] Squashfs: allocate entries with cache

From: Rosen Penev

Date: Mon Mar 30 2026 - 17:07:28 EST


Use a flexible array member with kzalloc_flex to combine allocations and
simplify code slightly.

Moved struct as flexible array members require full definitions.

Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
fs/squashfs/cache.c | 13 +++----------
fs/squashfs/squashfs_fs_sb.h | 28 ++++++++++++++--------------
2 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/fs/squashfs/cache.c b/fs/squashfs/cache.c
index 67abd4dff222..1231667b59e5 100644
--- a/fs/squashfs/cache.c
+++ b/fs/squashfs/cache.c
@@ -210,7 +210,6 @@ void squashfs_cache_delete(struct squashfs_cache *cache)
kfree(cache->entry[i].actor);
}

- kfree(cache->entry);
kfree(cache);
}

@@ -229,22 +228,16 @@ struct squashfs_cache *squashfs_cache_init(char *name, int entries,
if (entries == 0)
return NULL;

- cache = kzalloc_obj(*cache);
+ cache = kzalloc_flex(*cache, entry, entries);
if (cache == NULL) {
ERROR("Failed to allocate %s cache\n", name);
return ERR_PTR(-ENOMEM);
}

- cache->entry = kzalloc_objs(*(cache->entry), entries);
- if (cache->entry == NULL) {
- ERROR("Failed to allocate %s cache\n", name);
- goto cleanup;
- }
-
+ cache->entries = entries;
+ cache->unused = entries;
cache->curr_blk = 0;
cache->next_blk = 0;
- cache->unused = entries;
- cache->entries = entries;
cache->block_size = block_size;
cache->pages = block_size >> PAGE_SHIFT;
cache->pages = cache->pages ? cache->pages : 1;
diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h
index c01998eec146..344b78b23f9d 100644
--- a/fs/squashfs/squashfs_fs_sb.h
+++ b/fs/squashfs/squashfs_fs_sb.h
@@ -12,20 +12,6 @@

#include "squashfs_fs.h"

-struct squashfs_cache {
- char *name;
- int entries;
- int curr_blk;
- int next_blk;
- int num_waiters;
- int unused;
- int block_size;
- int pages;
- spinlock_t lock;
- wait_queue_head_t wait_queue;
- struct squashfs_cache_entry *entry;
-};
-
struct squashfs_cache_entry {
u64 block;
int length;
@@ -40,6 +26,20 @@ struct squashfs_cache_entry {
struct squashfs_page_actor *actor;
};

+struct squashfs_cache {
+ char *name;
+ int entries;
+ int curr_blk;
+ int next_blk;
+ int num_waiters;
+ int unused;
+ int block_size;
+ int pages;
+ spinlock_t lock;
+ wait_queue_head_t wait_queue;
+ struct squashfs_cache_entry entry[] __counted_by(entries);
+};
+
struct squashfs_sb_info {
const struct squashfs_decompressor *decompressor;
int devblksize;
--
2.53.0