[RFC PATCH v2 35/43] mm/zsmalloc: Size static limits for the worst-case page size

Eric Curtin ericcurtin17 at gmail.com
Mon Nov 30 17:52:07 PST 2026


From: Ryan Roberts <ryan.roberts at arm.com>

zsmalloc uses PAGE_SIZE and PAGE_SHIFT in constants and in the size of
an array in struct zs_pool, none of which are compile-time constants
with arm64 boot-time page size selection.

Take the worst case where a constant is needed: _PFN_BITS uses
PAGE_SHIFT_MIN, since the smallest page gives the most PFN bits,
ZS_OBJS_PER_PAGE_BITS uses PAGE_SHIFT_MAX, and the 24-bit first object
offset check in set_first_obj_offset() uses PAGE_SIZE_MAX.
ZS_SIZE_CLASSES now depends on the runtime page size, so the size_class
array in struct zs_pool becomes a pointer that zs_create_pool()
allocates with kcalloc() and zs_destroy_pool() frees.

Signed-off-by: Ryan Roberts <ryan.roberts at arm.com>
Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit dca1629b5374efd825c6a5bc6d4f210dae3b861f)
---
 mm/zsmalloc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 7ef80e0da626..09d131da22ac 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -72,13 +72,13 @@
 #else
 /*
  * If this definition of MAX_PHYSMEM_BITS is used, ZS_OBJ_PFN_SHIFT will
- * just be PAGE_SHIFT
+ * just be PAGE_SHIFT_MIN
  */
 #define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG
 #endif
 #endif
 
-#define _PFN_BITS		(MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)
+#define _PFN_BITS		(MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT_MIN)
 
 /*
  * Head in allocated object should have OBJ_ALLOCATED_TAG
@@ -137,7 +137,7 @@
  * possible object size: log2(PAGE_SIZE / 32) = PAGE_SHIFT - 5.
  * 32 is the hard floor of ZS_MIN_ALLOC_SIZE.
  */
-#define ZS_OBJS_PER_PAGE_BITS	(PAGE_SHIFT - 5)
+#define ZS_OBJS_PER_PAGE_BITS	(PAGE_SHIFT_MAX - 5)
 
 /*
  * Bits to index any object in the densest possible zspage.  Below this,
@@ -270,7 +270,7 @@ static struct kmem_cache *zspage_cachep;
 struct zs_pool {
 	const char *name;
 
-	struct size_class *size_class[ZS_SIZE_CLASSES];
+	struct size_class **size_class;
 
 	atomic_long_t pages_allocated;
 
@@ -502,7 +502,7 @@ static inline unsigned int get_first_obj_offset(struct zpdesc *zpdesc)
 static inline void set_first_obj_offset(struct zpdesc *zpdesc, unsigned int offset)
 {
 	/* With 24 bits available, we can support offsets into 16 MiB pages. */
-	BUILD_BUG_ON(PAGE_SIZE > SZ_16M);
+	BUILD_BUG_ON(PAGE_SIZE_MAX > SZ_16M);
 	VM_WARN_ON_ONCE(!PageZsmalloc(zpdesc_page(zpdesc)));
 	VM_WARN_ON_ONCE(offset & ~FIRST_OBJ_PAGE_TYPE_MASK);
 	zpdesc->first_obj_offset &= ~FIRST_OBJ_PAGE_TYPE_MASK;
@@ -2151,6 +2151,13 @@ struct zs_pool *zs_create_pool(const char *name)
 	rwlock_init(&pool->lock);
 	atomic_set(&pool->compaction_in_progress, 0);
 
+	pool->size_class = kcalloc(ZS_SIZE_CLASSES,
+					sizeof(*pool->size_class), GFP_KERNEL);
+	if (!pool->size_class) {
+		kfree(pool);
+		return NULL;
+	}
+
 	pool->name = kstrdup(name, GFP_KERNEL);
 	if (!pool->name)
 		goto err;
@@ -2276,6 +2283,7 @@ void zs_destroy_pool(struct zs_pool *pool)
 		kfree(class);
 	}
 
+	kfree(pool->size_class);
 	kfree(pool->name);
 	kfree(pool);
 }



More information about the linux-arm-kernel mailing list