Re: [RFC PATCH v4 2/4] zram: support compression at the granularity of multi-pages

From: Nanzhe Zhao

Date: Fri Nov 27 2026 - 03:29:19 EST


> [Severity: Medium]
> Can zstrm->excl_lock and zstrm->buf_lock be acquired in opposite orders?
>
> In zram_read_partial_from_zspool(), an exclusive decompression backend
> (ZCOMP_DSTRM_EXCLUSIVE) takes zstrm->excl_lock inside zcomp_dstrm_get()
> and then locks zstrm->buf_lock. A shared decompression backend
> (ZCOMP_DSTRM_SHARED) locks zstrm->buf_lock first and then calls
> zcomp_decompress(), which locks zstrm->excl_lock when a two-page object
> falls back to zstrm->local_copy:
>
> CPU0 CPU1
> ---- ----
> lock(&zstrm->excl_lock)
> lock(&zstrm->buf_lock)
> lock(&zstrm->buf_lock)
> lock(&zstrm->excl_lock)
>
> *** DEADLOCK ***

This is a valid issue. I think I can eliminate the lock inversion by
dropping buf_lock entirely and letting partial reads always acquire the
stream in ZCOMP_DSTRM_EXCLUSIVE mode, where decomp_buf is already
serialized under excl_lock.

With patch 4 (allowing direct reclaim for synchronous multi-page swapin
in do_swap_page()), I found partial reads are quite rare. So using
exclusive mode here will not cause heavy lock contention.

--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -79,7 +79,6 @@ struct zcomp_dstrm {
void *local_copy;
#ifdef CONFIG_ZRAM_MULTI_PAGES
void *decomp_buf;
- struct mutex buf_lock;
#endif
};

--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -120,7 +120,6 @@ static int zcomp_dstrm_init(struct zcomp *comp, struct zcomp_dstrm *zstrm)
zcomp_dstrm_free(comp, zstrm);
return -ENOMEM;
}
- mutex_init(&zstrm->buf_lock);
#endif
return 0;
}

--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2907,17 +2907,13 @@ static int zram_read_partial_from_zspool(struct zram *zram, struct page *page,
prio = get_slot_comp_priority(zram, index);
- mode = ZCOMP_DSTRM_EXCLUSIVE;
- if (zcomp_shared_decomp(zram->comps[prio]))
- mode = ZCOMP_DSTRM_SHARED;
-
- zstrm = zcomp_dstrm_get(zram->comps[prio], mode);
- mutex_lock(&zstrm->buf_lock);
+ zstrm = zcomp_dstrm_get(zram->comps[prio], ZCOMP_DSTRM_EXCLUSIVE);
ret = zcomp_decompress(zram->comps[prio], zstrm, sg, size,
zstrm->decomp_buf, page_size);
if (!ret) {
dst = kmap_local_page(page);
memcpy(dst, zstrm->decomp_buf + offset, PAGE_SIZE);
kunmap_local(dst);
}
- mutex_unlock(&zstrm->buf_lock);
- zcomp_dstrm_put(zram->comps[prio], zstrm, mode);
+ zcomp_dstrm_put(zram->comps[prio], zstrm, ZCOMP_DSTRM_EXCLUSIVE);
}

> [Severity: Medium]
> Can __test_multi_pages_comp() access past the end of zram->table when
> disksize is not a multiple of ZCOMP_MULTI_PAGES_SIZE?
>
> disksize_store() only aligns disksize with PAGE_ALIGN(disksize). When
> num_pages is not a multiple of ZCOMP_MULTI_PAGES_NR, an access to the
> final slots has head + i >= num_pages, so __test_multi_pages_comp() and
> the set_slot_flag() loops in the multi-page write path read and write past
> the end of zram->table.

This is a valid issue (and was also caught by syzbot KASAN). I will align
disksize to ZCOMP_MULTI_PAGES_SIZE in disksize_store() so that multi-page
groups never cross the end of zram->table.

--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -3298,7 +3298,7 @@ static ssize_t disksize_store(struct device *dev, struct device_attribute *attr,
if (init_done(zram)) {
pr_info("Cannot change disksize for initialized device\n");
return -EBUSY;
}

- disksize = PAGE_ALIGN(disksize);
+ disksize = ALIGN(disksize, ZCOMP_MULTI_PAGES_SIZE);
num_pages = disksize >> PAGE_SHIFT;

> [Severity: Medium]
> Does ZRAM_FLAG_SHIFT overflow the 32-bit attr.flags field when
> PAGE_SHIFT + CONFIG_ZSMALLOC_MULTI_PAGES_ORDER > 21?
>
> CONFIG_ZSMALLOC_MULTI_PAGES_ORDER allows values up to 9, and enum
> zram_pageflags defines 10 flag bits above ZRAM_FLAG_SHIFT. On 16K or 64K
> page configurations with larger orders (for example, PAGE_SHIFT == 16 with
> order >= 6), __NR_ZRAM_PAGEFLAGS exceeds 32 and triggers
> BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.attr.flags) * 8) in
> zram_init().

This is valid. While configurations exceeding the 32-bit limit are already
rejected at build time, I will replace BUILD_BUG_ON() with an informative
static_assert() and add comments documenting the bit budget (PAGE_SHIFT +
ZSMALLOC_MULTI_PAGES_ORDER <= 21).

--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -3680,2 +3680,3 @@ static int __init zram_init(void)
- BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.attr.flags) * 8);
+ static_assert(__NR_ZRAM_PAGEFLAGS <= sizeof(zram_te.attr.flags) * 8,
+ "zram pageflags overflow: PAGE_SHIFT + ZSMALLOC_MULTI_PAGES_ORDER must be <= 21");

> [Severity: Low]
> Should this find_alloced_obj() change be folded into the preceding commit
> "mm: zsmalloc: support objects compressed based on multiple pages" so the
> series stays bisectable?
>
> Also, the commit message ends with "The following data illustrates the
> time and compressed data for typical anonymous pages gathered from Android
> phones." without including the data table, and multi_pages_debug_stat_show()
> is missing a colon or space before "%8llu" on the failed and partial count
> lines.

Agreed. I will fold the find_alloced_obj() change into patch 1, drop the
sentence that promises a data table which is not included, and fix the
missing delimiter in multi_pages_debug_stat_show:

--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2011,3 +2011,3 @@ static ssize_t multi_pages_debug_stat_show(struct device *dev,
- "multi_pages_failed_writes%8llu\n"
- "zram_bio_write_multi_pages_partial_count%8llu\n"
- "zram_bio_read_multi_pages_partial_count%8llu\n"
+ "multi_pages_failed_writes: %8llu\n"
+ "zram_bio_write_multi_pages_partial_count: %8llu\n"
+ "zram_bio_read_multi_pages_partial_count: %8llu\n"