From: sashiko-bot@kernel.org
To: "Nanzhe Zhao" <zhaonanzhe@xiaomi.com>,
"Andrew Morton" <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, "Jens Axboe" <axboe@kernel.dk>,
"Sergey Senozhatsky" <senozhatsky@chromium.org>,
"Minchan Kim" <minchan@kernel.org>,
"Kairui Song" <kasong@tencent.com>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Nhat Pham" <nphamcs@gmail.com>,
"Usama Arif" <usama.arif@linux.dev>,
"Yosry Ahmed" <yosry@kernel.org>,
"Chengming Zhou" <chengming.zhou@linux.dev>,
"Chris Li" <chrisl@kernel.org>,
"David Hildenbrand" <david@kernel.org>,
"Ying Huang" <ying.huang@intel.com>,
"Suren Baghdasaryan" <surenb@google.com>,
"Ryan Roberts" <ryan.roberts@arm.com>,
"Kanchana P Sridhar" <kanchana.p.sridhar@intel.com>,
"Wajdi K Feghali" <wajdi.k.feghali@intel.com>,
"Bala Seshasayee" <bala.seshasayee@linux.intel.com>,
"Nick Terrell" <terrelln@fb.com>,
"Barry Song" <baohua@kernel.org>,
"Tangquan Zheng" <zhengtangquan@oppo.com>,
"Bo Zhang" <zhangbo56@xiaomi.com>
Subject: Re: [PATCH 1/4] mm: zsmalloc: support objects compressed based on multiple pages
Date: Sun, 06 Dec 2026 02:20:07 +0000 [thread overview]
Message-ID: <sashiko-outbox-252053@kernel.org> (raw)
In-Reply-To: <20261205021803.3704464-2-zhaonanzhe@xiaomi.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] mm: zsmalloc: zs_rebind_handle() uses offset_in_page() on
multi-page zpdescs
- [Medium] mm: zsmalloc: CLASS_BITS bump expands ZS_OBJ_CLASS_BITS to 10
bits
- [Medium] mm: zsmalloc: __zs_compact() write-locks pool for empty
multi-page classes
- [Low] mm: zsmalloc: pages_per_zspage doc mismatches
zs_stats_size_show() output
--
commit 2914fdff611ccc07d7896b5a0cd7f8a5cb3a06ab
Author: Tangquan Zheng <zhengtangquan@oppo.com>
mm: zsmalloc: support objects compressed based on multiple pages
This patch adds CONFIG_ZSMALLOC_MULTI_PAGES support to zsmalloc so it can
allocate and store compressed objects larger than PAGE_SIZE (up to
PAGE_SIZE << CONFIG_ZSMALLOC_MULTI_PAGES_ORDER) using higher-order
compound pages and additional multi-page size classes.
> diff --git a/Documentation/mm/zsmalloc.rst b/Documentation/mm/zsmalloc.rst
> index 8132cd70a61f2..283d22cb8abac 100644
> --- a/Documentation/mm/zsmalloc.rst
> +++ b/Documentation/mm/zsmalloc.rst
[ ... ]
> @@ -85,7 +98,9 @@ obj_used
> pages_used
> the number of pages allocated for the class
> pages_per_zspage
> - the number of 0-order pages to make a zspage
> + the number of pages (or higher-order allocation units) to make a zspage
> +objs_per_zspage
> + the total number of objects that a single zspage of this class can hold
> freeable
> the approximate number of pages class compaction can free
[Severity: Low]
Does this description of pages_per_zspage match what zs_stats_size_show()
prints?
zs_stats_size_show() outputs class_pages_per_zspage(class), which shifts
zpdescs_per_zspage by the class order and reports the total number of base
order-0 physical pages in a zspage rather than the count of higher-order
allocation units.
[ ... ]
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index f263bd2b8d468..171cb787fe6b5 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
[ ... ]
> @@ -115,7 +121,7 @@
>
> #define HUGE_BITS 1
> #define FULLNESS_BITS 4
> -#define CLASS_BITS 8
> +#define CLASS_BITS 9
> #define MAGIC_VAL_BITS 8
[Severity: Medium]
Does raising CLASS_BITS to 9 unnecessarily expand ZS_OBJ_CLASS_BITS to 10
bits and regress riscv64?
ZS_SIZE_CLASSES is at most 481 even with CONFIG_ZSMALLOC_MULTI_PAGES
enabled at order 3, which already fits in the existing 9-bit
CLASS_BITS + 1 field. Unconditionally bumping CLASS_BITS to 9 increases
ZS_OBJ_CLASS_BITS to 10 bits even when CONFIG_ZSMALLOC_MULTI_PAGES is
disabled.
On riscv64 (56-bit physical addresses, 4 KiB pages, ZS_OBJ_PFN_SHIFT = 20),
this folds ZS_OBJ_CLASS_BITS to 0 for CONFIG_ZSMALLOC_CHAIN_SIZE in 9..16,
disabling the lockless class lookup in obj_class_get_and_lock() and forcing
every zs_free() and zs_rebind_handle() onto read_lock(&pool->lock). With
the default chain size of 8, it shrinks ZS_OBJ_IDX_BITS from 11 to 10 and
inflates ZS_MIN_ALLOC_SIZE from 32 to 40 bytes.
[ ... ]
> +#ifdef CONFIG_ZSMALLOC_MULTI_PAGES
> +#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))
> +#define ZS_SIZE_CLASSES (ZS_PAGE_SIZE_CLASSES + ZS_MULTI_PAGES_SIZE_CLASSES)
> +#else
[Severity: Medium]
Can appending these multi-page size classes cause zs_compact() to
repeatedly stall concurrent readers on empty classes?
zs_compact() iterates over all ZS_SIZE_CLASSES and calls __zs_compact()
for every distinct class, where __zs_compact() acquires
write_lock(&pool->lock) and spin_lock(&class->lock) before checking
zs_can_compact(class). With CONFIG_ZSMALLOC_MULTI_PAGES enabled, every
pool (including zswap and 4 KiB-only zram pools) now takes
write_lock(&pool->lock) for each empty multi-page class, blocking
concurrent zs_obj_read_sg_begin() readers during reclaim or compaction.
Pre-checking zs_can_compact(class) before taking write_lock(&pool->lock),
as zs_shrinker_count() already does, would avoid these stalls.
[ ... ]
> @@ -1551,6 +1631,8 @@ static void zs_object_copy(struct size_class *class, unsigned long dst,
> struct zpdesc *s_zpdesc, *d_zpdesc;
> unsigned int s_objidx, d_objidx;
> unsigned long s_off, d_off;
> + unsigned long page_size = class_size_to_zs_size(class->size);
> + unsigned long page_mask = ~(page_size - 1);
> void *s_addr, *d_addr;
> int s_size, d_size, size;
> int written = 0;
[Severity: High]
Was zs_rebind_handle() missed when updating object offset calculations for
multi-page zpdescs?
mm/zsmalloc.c:zs_rebind_handle() {
...
if (likely(!ZsHugePage(zspage))) {
obj_to_location(obj, &zpdesc, &obj_idx);
off = offset_in_page(class->size * obj_idx);
vaddr = kmap_local_zpdesc(zpdesc);
link = (struct link_free *)(vaddr + off);
link->handle = (unsigned long)dst | OBJ_ALLOCATED_TAG;
kunmap_local(vaddr);
}
...
}
For any non-huge multi-page object whose offset within its compound zpdesc
is at least PAGE_SIZE, offset_in_page() wraps into the first base page,
corrupting 8 bytes of another object's data or header with the dst handle
address while leaving the real object header pointing to src. If the
caller then frees src as documented on struct zs_handle, a subsequent
zs_compact() -> migrate_zspage() reads the stale src handle from the real
header, causing a use-after-free while leaving dst dangling.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205021803.3704464-1-zhaonanzhe@xiaomi.com?part=1
next prev parent reply other threads:[~2026-12-06 2:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 2:17 [RFC PATCH v5 0/4] zram/zsmalloc: multi-page compression for large folios Nanzhe Zhao
2026-12-05 2:18 ` [PATCH 1/4] mm: zsmalloc: support objects compressed based on multiple pages Nanzhe Zhao
2026-12-06 2:20 ` sashiko-bot [this message]
2026-12-05 2:18 ` [PATCH 2/4] zram: support compression at the granularity of multi-pages Nanzhe Zhao
2026-12-06 2:20 ` sashiko-bot
2026-12-05 2:18 ` [PATCH 3/4] zram: backend_zstd: use multi-page size for zstd parameter setup Nanzhe Zhao
2026-12-05 2:18 ` [PATCH 4/4] mm: swap: allow direct reclaim for synchronous multi-page swapin Nanzhe Zhao
2026-12-06 2:20 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-252053@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=bala.seshasayee@linux.intel.com \
--cc=baohua@kernel.org \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kanchana.p.sridhar@intel.com \
--cc=kasong@tencent.com \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=ryan.roberts@arm.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=senozhatsky@chromium.org \
--cc=surenb@google.com \
--cc=terrelln@fb.com \
--cc=usama.arif@linux.dev \
--cc=wajdi.k.feghali@intel.com \
--cc=ying.huang@intel.com \
--cc=yosry@kernel.org \
--cc=zhangbo56@xiaomi.com \
--cc=zhaonanzhe@xiaomi.com \
--cc=zhengtangquan@oppo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.