Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nanzhe Zhao" <zhaonanzhe@xiaomi.com>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Tangquan Zheng" <zhengtangquan@oppo.com>,
	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>,
	"Bo Zhang" <zhangbo56@xiaomi.com>
Subject: Re: [RFC PATCH v4 1/4] mm: zsmalloc: support objects compressed based on multiple pages
Date: Mon, 16 Nov 2026 02:46:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-216088@kernel.org> (raw)
In-Reply-To: <20261115024455.1465726-2-zhaonanzhe@xiaomi.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] mm: zsmalloc: build failure in find_alloced_obj() calling
  obj_allocated()
- [High] mm: zsmalloc: VM_BUG_ON in obj_write() calling single-page
  memcpy_to_page()
- [High] mm: zsmalloc: memory corruption in zs_rebind_handle() using
  offset_in_page()
- [High] mm: zsmalloc: unclamped basepage size class in zs_create_pool()
- [Medium] mm: zsmalloc: 24-bit offset overflow in
  set_first_obj_offset() on 64K pages
- [Medium] mm: zsmalloc: preemptible __zone_stat_add_folio() in zpdesc
  state helpers
- [Low] mm: zsmalloc: missing @type kernel-doc in zs_huge_class_size()
- [Low] mm: zsmalloc: missing space after order column in
  zs_stats_size_show()
--

commit de88105665ba4d7f0292945bc4a5ae1786f09efe
Author: Tangquan Zheng <zhengtangquan@oppo.com>

mm: zsmalloc: support objects compressed based on multiple pages

This commit adds multi-page object support to zsmalloc under
CONFIG_ZSMALLOC_MULTI_PAGES, introducing compound zpdesc allocations and size
classes above PAGE_SIZE up to ZSMALLOC_MULTI_PAGES_ORDER.

> diff --git a/mm/Kconfig b/mm/Kconfig
> index afff1b25b5438..b68720f6fb6bd 100644
> --- a/mm/Kconfig
> +++ b/mm/Kconfig
> @@ -157,6 +158,24 @@ config ZSMALLOC_CHAIN_SIZE
>  
>  	  For more information, see zsmalloc documentation.
>  
> +config ZSMALLOC_MULTI_PAGES
> +	bool "support zsmalloc multiple pages"
> +	depends on ZSMALLOC && !HIGHMEM
> +	help
> +	  This option configures zsmalloc to support allocations larger than
> +	  PAGE_SIZE, enabling compression across multiple pages. The size of
> +	  these multiple pages is determined by the configured
> +	  ZSMALLOC_MULTI_PAGES_ORDER.
> +
> +config ZSMALLOC_MULTI_PAGES_ORDER
> +	int "zsmalloc multiple pages order"
> +	default 2
> +	range 1 9
> +	depends on ZSMALLOC_MULTI_PAGES
> +	help
> +	  This option is used to configure zsmalloc to support the compression
> +	  of multiple pages.
> +
>  endmenu

[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):

mm/zsmalloc.c:set_first_obj_offset() {
	/* With 24 bits available, we can support offsets into 16 MiB pages. */
	BUILD_BUG_ON(PAGE_SIZE > 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;
	zpdesc->first_obj_offset |= offset & FIRST_OBJ_PAGE_TYPE_MASK;
}

When init_zspage() sets an initial object offset at or above 16 MiB, the
offset wraps modulo 16 MiB, causing find_alloced_obj() during compaction to
scan from the wrong offset inside an object payload.

> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 4a6ec43f418b2..2d83fcc8610da 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c

[ ... ]

> @@ -295,27 +343,28 @@ struct zs_pool {
>  
>  static inline void zpdesc_inc_zone_page_state(struct zpdesc *zpdesc)
>  {
> -	inc_zone_page_state(zpdesc_page(zpdesc), NR_ZSPAGES);
> +	__zone_stat_add_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
>  }
>  
>  static inline void zpdesc_dec_zone_page_state(struct zpdesc *zpdesc)
>  {
> -	dec_zone_page_state(zpdesc_page(zpdesc), NR_ZSPAGES);
> +	__zone_stat_sub_folio(zpdesc_folio(zpdesc), NR_ZSPAGES);
>  }

[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.

[ ... ]

> @@ -609,7 +667,8 @@ static int zs_stats_size_show(struct seq_file *s, void *v)
>  
>  		spin_lock(&class->lock);
>  
> -		seq_printf(s, " %5u %5u ", i, class->size);
> +		seq_printf(s, " %5u %5u %5u", i, class->size,
> +			   class_size_to_zs_order(class->size));

[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.

[ ... ]

> @@ -1189,16 +1257,17 @@ static void obj_write(struct size_class *class, struct zspage *zspage,
>  		      unsigned long obj, const void *src, size_t size)
>  {
>  	struct zpdesc *zpdesc;
> +	unsigned long page_size = class_size_to_zs_size(class->size);
>  	unsigned long off;
>  	unsigned int obj_idx;
>  
>  	obj_to_location(obj, &zpdesc, &obj_idx);
> -	off = offset_in_page(class->size * obj_idx);
> +	off = (class->size * obj_idx) & (page_size - 1);
>  
>  	if (!ZsHugePage(zspage))
>  		off += ZS_HANDLE_SIZE;
>  
> -	if (off + size <= PAGE_SIZE) {
> +	if (off + size <= page_size) {
>  		/* this object is contained entirely within a page */
>  		void *dst = kmap_local_zpdesc(zpdesc);
>  
> @@ -1208,7 +1277,7 @@ static void obj_write(struct size_class *class, struct zspage *zspage,
>  		/* this object spans two pages */
>  		size_t sizes[2];
>  
> -		sizes[0] = PAGE_SIZE - off;
> +		sizes[0] = page_size - off;
>  		sizes[1] = size - sizes[0];
>  
>  		memcpy_to_page(zpdesc_page(zpdesc), off, src, sizes[0]);

[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).

> @@ -1230,9 +1299,9 @@ static void obj_write(struct size_class *class, struct zspage *zspage,
>   *
>   * Return: the size (in bytes) of the first huge zsmalloc &size_class.
>   */
> -size_t zs_huge_class_size(struct zs_pool *pool)
> +size_t zs_huge_class_size(struct zs_pool *pool, enum zsmalloc_type type)
>  {
> -	return huge_class_size;
> +	return huge_class_size[type];
>  }
>  EXPORT_SYMBOL_GPL(zs_huge_class_size);

[Severity: Low]
The kernel-doc comment above zs_huge_class_size() is missing an @type entry
for the new parameter.

[ ... ]

> @@ -1365,13 +1437,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;
>  	zspage = get_zspage(f_zpdesc);

[Severity: High]
Does zs_rebind_handle() also need to mask the object offset with
page_size - 1 instead of offset_in_page()?

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);
    ...
}

When a non-huge multi-page object sits at an offset >= PAGE_SIZE within its
zpdesc, zs_rebind_handle() overwrites 8 bytes in the first subpage and leaves
the object's own header pointing to the old src handle, so later compaction
updates src instead of dst.

[ ... ]

> @@ -1615,13 +1691,13 @@ static struct zs_handle *find_alloced_obj(struct size_class *class,
>  	int index = *obj_idx;
>  	struct zs_handle *handle = NULL;
>  	void *addr = kmap_local_zpdesc(zpdesc);
> +	unsigned long page_size = class_size_to_zs_size(class->size);
>  
>  	offset = get_first_obj_offset(zpdesc);
>  	offset += class->size * index;
>  
> -	while (offset < PAGE_SIZE) {
> -		handle = obj_allocated(zpdesc, addr + offset);
> -		if (handle)
> +	while (offset < page_size) {
> +		if (obj_allocated(zpdesc, addr + offset, &handle))
>  			break;

[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.

[ ... ]

> @@ -2215,16 +2292,32 @@ struct zs_pool *zs_create_pool(const char *name)
>  	 */
>  	for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
>  		int size;
> -		int pages_per_zspage;
> +		int zpdescs_per_zspage;
>  		int objs_per_zspage;
>  		struct size_class *class;
>  		int fullness;
> +		int idx = ZSMALLOC_TYPE_BASEPAGE;
> +#ifdef CONFIG_ZSMALLOC_MULTI_PAGES
> +		int order = 0;
> +#endif
>  
> -		size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
> +		if (i < ZS_PAGE_SIZE_CLASSES)
> +			size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
> +#ifdef CONFIG_ZSMALLOC_MULTI_PAGES
> +		if (i >= ZS_PAGE_SIZE_CLASSES)
> +			size = PAGE_SIZE + (i - ZS_PAGE_SIZE_CLASSES) *
> +			       ZS_MULTI_PAGES_SIZE_CLASS_DELTA;
> +#endif
>  		if (size > ZS_MAX_ALLOC_SIZE)
>  			size = ZS_MAX_ALLOC_SIZE;

[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, turning it into a non-huge multi-page class while class
ZS_PAGE_SIZE_CLASSES starts at PAGE_SIZE and is unreachable from
get_size_class_index().  Storing an incompressible PAGE_SIZE page then lands
in a non-huge class with an 8-byte handle prefix, corrupting incompressible
page reads in zram and triggering length warnings in zswap.

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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115024455.1465726-1-zhaonanzhe@xiaomi.com?part=1


  reply	other threads:[~2026-11-16  2:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15  2:44 [RFC PATCH v4 0/4] mTHP-friendly compression in zsmalloc and zram based on multi-pages Nanzhe Zhao
2026-11-15  2:44 ` [RFC PATCH v4 1/4] mm: zsmalloc: support objects compressed based on multiple pages Nanzhe Zhao
2026-11-16  2:46   ` sashiko-bot [this message]
2026-11-15  2:44 ` [RFC PATCH v4 2/4] zram: support compression at the granularity of multi-pages Nanzhe Zhao
2026-11-16  2:46   ` sashiko-bot
2026-11-15  2:44 ` [RFC PATCH v4 3/4] zram: backend_zstd: use multi-page size for zstd parameter setup Nanzhe Zhao
2026-11-15  2:44 ` [RFC PATCH v4 4/4] mm: swap: allow direct reclaim for synchronous multi-page swapin Nanzhe Zhao
2026-11-16  2:46   ` 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-216088@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox