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: [RESEND RFC PATCH v5 1/4] mm: zsmalloc: support objects compressed based on multiple pages
Date: Sun, 06 Dec 2026 02:34:43 +0000 [thread overview]
Message-ID: <sashiko-outbox-252066@kernel.org> (raw)
In-Reply-To: <20261205023024.3718932-2-zhaonanzhe@xiaomi.com>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] mm: zsmalloc: zs_rebind_handle() truncates multi-page offsets
- [High] mm: zsmalloc: zs_obj_read_sg_begin() emits zero-len sg at
page_size offset
- [Medium] mm: zsmalloc: CLASS_BITS bump regresses riscv64 min alloc and
class lookup
- [Medium] mm: zsmalloc: amplified pool->lock contention in zs_compact()
- [Low] docs: zsmalloc: pages_per_zspage description mismatches
zs_stats_size_show()
- [Low] mm: zsmalloc.rst contradicts multi-page higher-order allocation
behavior
--
commit 17b06563390b5e16f8e087ce4f24dcfd81cb6e69
Author: Tangquan Zheng <zhengtangquan@oppo.com>
mm: zsmalloc: support objects compressed based on multiple pages
This commit adds CONFIG_ZSMALLOC_MULTI_PAGES and
CONFIG_ZSMALLOC_MULTI_PAGES_ORDER so zsmalloc can store compressed objects
larger than PAGE_SIZE in higher-order compound zpdescs up to order 3. It
appends multi-page size classes, updates zspage offset and accounting
helpers for variable zpdesc sizes, and adds a type parameter to
zs_huge_class_size().
> 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
> @@ -16,11 +16,21 @@ pages act as a single higher-order page i.e. an object can span 0-order
> page boundaries. The code refers to these linked pages as a single entity
> called zspage.
>
> -For simplicity, zsmalloc can only allocate objects of size up to PAGE_SIZE
> -since this satisfies the requirements of all its current users (in the
> -worst case, page is incompressible and is thus stored "as-is" i.e. in
> -uncompressed form). For allocation requests larger than this size, failure
> -is returned (see zs_store).
> +By default, zsmalloc allocates objects of size up to PAGE_SIZE, since this
> +satisfies the requirements of single-page compression (in the worst case,
> +a page is incompressible and is thus stored "as-is", i.e. in uncompressed form).
> +
> +When CONFIG_ZSMALLOC_MULTI_PAGES is enabled, zsmalloc extends this limit up
> +to ZS_MAX_ALLOC_SIZE (PAGE_SIZE << CONFIG_ZSMALLOC_MULTI_PAGES_ORDER).
> +CONFIG_ZSMALLOC_MULTI_PAGES_ORDER is the configuration option that specifies
> +the allocation order for multi-page objects, currently supported up to order 3
> +(PAGE_ALLOC_COSTLY_ORDER).
[Severity: Low]
Does the opening paragraph right above this hunk need to be updated to match
CONFIG_ZSMALLOC_MULTI_PAGES? It still states unconditionally that zsmalloc
"never attempts higher order page allocation", which now contradicts
alloc_zspage() allocating order 1..3 compound pages when multi-page support
is enabled.
[ ... ]
> @@ -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
[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
computes zpdescs_per_zspage << order, so the column always reports order-0
physical pages rather than 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 CLASS_BITS need to increase from 8 to 9? Both struct zspage.class and
ZS_OBJ_CLASS_BITS are sized as CLASS_BITS + 1, so CLASS_BITS = 8 already
provides 9 bits (512 classes), while ZS_SIZE_CLASSES is at most 481 even at
order 3 with 64 KiB pages.
On 64-bit architectures with 56-bit MAX_POSSIBLE_PHYSMEM_BITS and 4 KiB
pages (such as riscv64, where ZS_OBJ_PFN_SHIFT is 20), widening
CLASS_BITS + 1 to 10 bits causes two regressions even when
CONFIG_ZSMALLOC_MULTI_PAGES is disabled:
- With CONFIG_ZSMALLOC_CHAIN_SIZE=16, ZS_OBJ_CLASS_BITS folds to 0
(20 < 10 + 11), disabling the lockless class lookup in
obj_class_get_and_lock() and taking global read_lock(&pool->lock) on every
zs_free() and zs_rebind_handle().
- With the default CONFIG_ZSMALLOC_CHAIN_SIZE=8, ZS_OBJ_IDX_BITS shrinks
from 11 to 10, raising ZS_MIN_ALLOC_SIZE from 32 to 40 bytes and
increasing fragmentation for small objects.
Keeping CLASS_BITS at 8 with ZS_SIZE_CLASS_DELTA = PAGE_SIZE >> CLASS_BITS
(and a static_assert that ZS_SIZE_CLASSES <= (1U << (CLASS_BITS + 1)))
would avoid both regressions.
[ ... ]
> @@ -1154,12 +1221,13 @@ void zs_obj_read_sg_begin(struct zs_pool *pool, const struct zs_handle *handle,
> read_unlock(&pool->lock);
>
> class = zspage_class(pool, zspage);
> - off = offset_in_page(class->size * obj_idx);
> + page_size = class_size_to_zs_size(class->size);
> + off = (class->size * obj_idx) & (page_size - 1);
>
> if (!ZsHugePage(zspage))
> off += ZS_HANDLE_SIZE;
>
> - if (off + mem_len <= PAGE_SIZE) {
> + if (off + mem_len <= page_size) {
> /* this object is contained entirely within a page */
> sg_init_table(sg, 1);
> sg_set_page(sg, zpdesc_page(zpdesc), mem_len, off);
> @@ -1167,7 +1235,7 @@ void zs_obj_read_sg_begin(struct zs_pool *pool, const struct zs_handle *handle,
> size_t sizes[2];
>
> /* this object spans two pages */
> - sizes[0] = PAGE_SIZE - off;
> + sizes[0] = page_size - off;
> sizes[1] = mem_len - sizes[0];
>
> sg_init_table(sg, 2);
[Severity: High]
Can zs_obj_read_sg_begin() emit a zero-length sg[0] with offset equal to
page_size when an object header sits at page_size - ZS_HANDLE_SIZE?
With CLASS_BITS bumped to 9, riscv64 with default CONFIG_ZSMALLOC_CHAIN_SIZE
= 8 gets ZS_MIN_ALLOC_SIZE = 40 (an odd multiple of 8). Class size 56 then
places obj_idx 73 at offset 4088 (56 * 73), so adding ZS_HANDLE_SIZE sets
off == 4096 == page_size and sizes[0] = 0.
When zram passes that scatterlist to sg_copy_to_buffer() in
zcomp_decompress_buf() or read_from_zspool_raw(), sg_miter_next() advances
piter.sg_pgoffset to 1 on sg[0] and maps sg_page(&sg[0]) + 1 out of bounds
via kmap_local_page(). On the next iteration __sg_page_iter_next()
increments sg_pgoffset to 2, leaves 1 on sg[1], and skips sg[1] completely,
so sg_copy_to_buffer() returns 0 bytes copied. Since zram ignores that
return value, it decompresses stale zstrm->local_copy or uninitialized
memory, causing read failures or silent data corruption.
Advancing zpdesc = get_next_zpdesc(zpdesc) and resetting off = 0 when
off == page_size after adding ZS_HANDLE_SIZE avoids emitting the empty
entry.
[ ... ]
> @@ -1376,13 +1454,15 @@ static void obj_free(int class_size, unsigned long obj)
> struct link_free *link;
> struct zspage *zspage;
> struct zpdesc *f_zpdesc;
> - unsigned long f_offset;
> + unsigned long f_offset, page_size, page_mask;
> unsigned int f_objidx;
> void *vaddr;
>
>
> obj_to_location(obj, &f_zpdesc, &f_objidx);
> - f_offset = offset_in_page(class_size * f_objidx);
> + page_size = class_size_to_zs_size(class_size);
> + page_mask = ~(page_size - 1);
> + f_offset = (class_size * f_objidx) & ~page_mask;
[Severity: High]
Was zs_rebind_handle() missed when updating object offset calculations to
mask by class_size_to_zs_size(class->size) - 1?
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 a non-huge multi-page object at an in-zpdesc offset >= PAGE_SIZE (for
example order 2, class->size = 4160, obj_idx = 1), offset_in_page()
truncates 4160 to 64. zs_rebind_handle() then overwrites 8 bytes inside
object 0's payload while leaving object 1's actual header pointing to the
zeroed src handle, which leads to a use-after-free or invalid PFN decode
when zs_compact() later scans the zpdesc via find_alloced_obj().
[ ... ]
> @@ -2052,7 +2135,7 @@ static unsigned long __zs_compact(struct zs_pool *pool,
> fg = putback_zspage(class, src_zspage);
> if (fg == ZS_INUSE_RATIO_0) {
> free_zspage(pool, class, src_zspage);
> - pages_freed += class->pages_per_zspage;
> + pages_freed += class_pages_per_zspage(class);
> }
> src_zspage = NULL;
[Severity: Medium]
Does adding the multi-page size classes significantly amplify
write_lock(&pool->lock) contention and reader stall latency in zs_compact()?
zs_compact() loops over every distinct class in ZS_SIZE_CLASSES and calls
__zs_compact() without pre-checking zs_can_compact(class), so every
compaction pass now acquires global write_lock(&pool->lock) across dozens
of additional empty multi-page classes even on pools such as zswap that
never allocate multi-page objects. When multi-page classes are populated,
migrate_zspage() copies an entire multi-page src_zspage (2x to 8x larger,
up to 128 KiB at default order 2 and chain size 8) while holding
write_lock(&pool->lock) before checking rwlock_is_contended(&pool->lock),
blocking concurrent zs_obj_read_sg_begin() readers pool-wide.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205023024.3718932-1-zhaonanzhe@xiaomi.com?part=1
next prev parent reply other threads:[~2026-12-06 2:34 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 2:30 [RESEND RFC PATCH v5 0/4] zram/zsmalloc: multi-page compression for large folios Nanzhe Zhao
2026-12-05 2:30 ` [RESEND RFC PATCH v5 1/4] mm: zsmalloc: support objects compressed based on multiple pages Nanzhe Zhao
2026-12-06 2:34 ` sashiko-bot [this message]
2026-12-05 2:30 ` [RESEND RFC PATCH v5 2/4] zram: support compression at the granularity of multi-pages Nanzhe Zhao
2026-12-05 2:30 ` [RESEND RFC PATCH v5 3/4] zram: backend_zstd: use multi-page size for zstd parameter setup Nanzhe Zhao
2026-12-05 2:30 ` [RESEND RFC PATCH v5 4/4] mm: swap: allow direct reclaim for synchronous multi-page swapin Nanzhe Zhao
2026-12-06 2:34 ` 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-252066@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.