Re: [RFC PATCH v4 1/4] mm: zsmalloc: support objects compressed based on multiple pages
From: Nanzhe Zhao
Date: Fri Nov 27 2026 - 03:10:30 EST
> [Severity: High]
> Does this break compilation at this commit?
>
> obj_allocated() only takes two arguments (zpdesc and obj) and returns the
> handle pointer, so passing &handle fails to build until commit "zram: support
> compression at the granularity of multi-pages" reverts this line back to the
> two-argument call.
Yes, patch 1 breaks compilation on its own. I will fold the mm/zsmalloc.c
change from patch 2 into patch 1 to keep bisectability intact:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1698,2 +1698,3 @@ static struct zs_handle *find_alloced_obj(struct size_class *class,
- if (obj_allocated(zpdesc, addr + offset, &handle))
+ handle = obj_allocated(zpdesc, addr + offset);
+ if (handle)
break;
> [Severity: High]
> Does calling memcpy_to_page() here trigger VM_BUG_ON(offset + len > PAGE_SIZE)
> when writing a multi-page object that spans two zpdescs?
>
> For a multi-page class, page_size is larger than PAGE_SIZE, so off + sizes[0]
> equals page_size and exceeds PAGE_SIZE in the first memcpy_to_page() call
> (and sizes[1] can also exceed PAGE_SIZE in the second call).
Agreed. I will replace it with kmap_local_zpdesc() and memcpy():
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1270,16 +1270,18 @@ static void obj_write(struct size_class *class, struct zspage *zspage,
} else {
/* this object spans two pages */
size_t sizes[2];
+ void *dst;
sizes[0] = page_size - off;
sizes[1] = size - sizes[0];
- memcpy_to_page(zpdesc_page(zpdesc), off, src, sizes[0]);
+ dst = kmap_local_zpdesc(zpdesc);
+ memcpy(dst + off, src, sizes[0]);
+ kunmap_local(dst);
zpdesc = get_next_zpdesc(zpdesc);
- memcpy_to_page(zpdesc_page(zpdesc), 0, src + sizes[0], sizes[1]);
+ dst = kmap_local_zpdesc(zpdesc);
+ memcpy(dst, src + sizes[0], sizes[1]);
+ kunmap_local(dst);
}
> [Severity: Medium]
> Should these helpers use zone_stat_add_folio() and zone_stat_sub_folio()
> instead of the double-underscore variants?
>
> __zone_stat_add_folio() and __zone_stat_sub_folio() require preemption or
> interrupts to be disabled, whereas alloc_zspage(), zs_free() ->
> __free_zspage_lockless(), and zs_page_migrate() call these helpers in
> preemptible context with no locks held, triggering preemption-disabled
> assertions and racy per-CPU vmstat updates.
Agreed. I will switch to the non-preemption-disabled variants:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -345,4 +345,4 @@ static inline void zpdesc_inc_zone_page_state(struct zpdesc *zpdesc)
{
- __zone_stat_add_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
+ zone_stat_add_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
}
static inline void zpdesc_dec_zone_page_state(struct zpdesc *zpdesc)
{
- __zone_stat_sub_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
+ zone_stat_sub_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
}
> [Severity: Medium]
> Can ZSMALLOC_MULTI_PAGES_ORDER=9 overflow the 24-bit first_obj_offset field
> on 64 KiB page systems?
>
> With 64 KiB pages, order 9 gives a 32 MiB zpdesc, while
> set_first_obj_offset() masks offsets with FIRST_OBJ_PAGE_TYPE_MASK (16 MiB)
> and only checks BUILD_BUG_ON(PAGE_SIZE > SZ_16M):
Agreed. I will check ZS_MAX_ALLOC_SIZE instead of PAGE_SIZE:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -551,3 +551,3 @@ static 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(ZS_MAX_ALLOC_SIZE > SZ_16M);
> [Severity: High]
> Can the last base-page size class exceed PAGE_SIZE when
> CONFIG_ZSMALLOC_MULTI_PAGES is enabled?
>
> Previously, ZS_MAX_ALLOC_SIZE was PAGE_SIZE, which clamped class
> ZS_PAGE_SIZE_CLASSES - 1 to PAGE_SIZE when PAGE_SIZE - ZS_MIN_ALLOC_SIZE was
> not a multiple of ZS_SIZE_CLASS_DELTA (such as on 16 KiB and 64 KiB page
> kernels). With ZS_MAX_ALLOC_SIZE raised to ZSMALLOC_MULTI_PAGES_SIZE, class
> ZS_PAGE_SIZE_CLASSES - 1 gets size == PAGE_SIZE + 32 on 16 KiB and 64 KiB
> page systems...
>
> Should sizes for i < ZS_PAGE_SIZE_CLASSES be clamped to PAGE_SIZE, and should
> the multi-page classes start at i - ZS_PAGE_SIZE_CLASSES + 1?
Yes. I will clamp base size classes to PAGE_SIZE, start multi-page classes
at i - ZS_PAGE_SIZE_CLASSES + 1, and adjust get_size_class_index() accordingly:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -211,4 +211,4 @@
#define ZS_MULTI_PAGES_SIZE_CLASS_DELTA (ZSMALLOC_MULTI_PAGES_SIZE >> (CLASS_BITS - 1))
#define ZS_MULTI_PAGES_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - PAGE_SIZE, \
- ZS_MULTI_PAGES_SIZE_CLASS_DELTA) + 1)
+ ZS_MULTI_PAGES_SIZE_CLASS_DELTA))
#define ZS_SIZE_CLASSES (ZS_PAGE_SIZE_CLASSES + ZS_MULTI_PAGES_SIZE_CLASSES)
@@ -586,5 +586,5 @@ static int get_size_class_index(int size)
idx = ZS_PAGE_SIZE_CLASSES;
- idx += DIV_ROUND_UP(size - PAGE_SIZE,
- ZS_MULTI_PAGES_SIZE_CLASS_DELTA);
+ idx += DIV_ROUND_UP(size - PAGE_SIZE,
+ ZS_MULTI_PAGES_SIZE_CLASS_DELTA) - 1;
return min_t(int, ZS_SIZE_CLASSES - 1, idx);
}
@@ -2305,4 +2305,4 @@ struct zs_pool *zs_create_pool(const char *name)
if (i < ZS_PAGE_SIZE_CLASSES)
- size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
+ size = min_t(int, ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA, PAGE_SIZE);
#ifdef CONFIG_ZSMALLOC_MULTI_PAGES
if (i >= ZS_PAGE_SIZE_CLASSES)
- size = PAGE_SIZE + (i - ZS_PAGE_SIZE_CLASSES) *
+ size = PAGE_SIZE + (i - ZS_PAGE_SIZE_CLASSES + 1) *
ZS_MULTI_PAGES_SIZE_CLASS_DELTA;
> [Severity: Low]
> Does the format string need a trailing space after the third "%5u" to match
> the header and Total lines? Without it, all subsequent columns in each
> class row are shifted left by one character.
Agreed. I will add the trailing space:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -670,2 +670,2 @@ static int zs_stats_size_show(struct seq_file *s, void *v)
- seq_printf(s, " %5u %5u %5u", i, class->size,
+ seq_printf(s, " %5u %5u %5u ", i, class->size,
> [Severity: Low]
> The kernel-doc comment above zs_huge_class_size() is missing an @type entry
> for the new parameter.
Agreed. I will document @type:
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1292,2 +1292,3 @@
* @pool: zsmalloc pool to use
+ * @type: zsmalloc type (base page or multi-page)