From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 743CF606A32F for ; Sun, 6 Dec 2026 02:34:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hl5EpDxk Received: by kanga.kvack.org (Postfix) id 97D906B05B2; Sat, 5 Dec 2026 21:34:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 92DBE6B05B3; Sat, 5 Dec 2026 21:34:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 844516B05B4; Sat, 5 Dec 2026 21:34:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5BF766B05B2 for ; Sat, 5 Dec 2026 21:34:48 -0500 (EST) Received: from smtpin29.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C683612071E for ; Sun, 6 Dec 2026 02:34:46 +0000 (UTC) X-FDA: 85511990172.29.B035CAB Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf27.hostedemail.com (Postfix) with ESMTP id 2C09740009 for ; Sun, 6 Dec 2026 02:34:45 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=hl5EpDxk; spf=pass (imf27.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796524485; b=kxs++OQ452+fqXT/h9EP4gwCM10PpYxbodA/9C8mDpkom2lAVBz/5ewGcEXLHfkBpOqHEp OEYxG4F03fKC/UcSeENn5uld/fA1VyCNXktUYvcWMZzbjARUfHZAih9CW+o+30j14n7IST md31klmnA4VqP0pUHtStMvRDpHG3fNw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796524485; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7WPic/39Y1AVl3CRRASkzg0/K4v0Q4tpvPh1EDVgZEQ=; b=DIovUEG/6vVW+19n5KOJjb+NSxFk0n+o8+j8I+Osd+PU3onEI3XUEGHw/TFAo+TBSunn81 bHxZCO0nXHNtgRo83APXIWg9xe6yWIO4zQq6NfjIkm7QpBMqM3if3HEMfXhe3JmyQ79uRz vQupZZopbXYFAcSMtP8V0VyO7dFhRH0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=hl5EpDxk; spf=pass (imf27.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 93F0E61232; Sun, 6 Dec 2026 02:34:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DB441F0092B; Sun, 6 Dec 2026 02:34:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796524484; bh=7WPic/39Y1AVl3CRRASkzg0/K4v0Q4tpvPh1EDVgZEQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=hl5EpDxkUlF1pqzKm5okjFpD0IZNWcyU9X/A9bCuj4PpfhBUfdjTjo8elnvsIqSO3 WfMu1XDCkxkcAwmZwwFH2bK9oPiJUfUI3XvVmZAEawfRdhCzcOVLsgQZGVXM/XAL4E 36smcQQBpVFyqgtYXM1btEsGQ7hD/Y52YeFC7ky+kESeY1lApS35bul296Go2hLJcD zm1swQ+2raiSBezyzWJ4w7+yEBCQN0sKddtCc0fmC+Pu6aoFkcby12DxSDL3hRNoVZ UMBK/VPtIaWOPHkwvzruSZe26zi8VGlFIS1sb4jsQb2QJjfYxHqxv4dzI76tfymB+h xwpZYnyhC2XWw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RESEND RFC PATCH v5 1/4] mm: zsmalloc: support objects compressed based on multiple pages Reply-To: sashiko-reviews@lists.linux.dev To: "Nanzhe Zhao" , "Andrew Morton" Cc: linux-mm@kvack.org, "Jens Axboe" , "Sergey Senozhatsky" , "Minchan Kim" , "Kairui Song" , "Johannes Weiner" , "Nhat Pham" , "Usama Arif" , "Yosry Ahmed" , "Chengming Zhou" , "Chris Li" , "David Hildenbrand" , "Ying Huang" , "Suren Baghdasaryan" , "Ryan Roberts" , "Kanchana P Sridhar" , "Wajdi K Feghali" , "Bala Seshasayee" , "Nick Terrell" , "Barry Song" , "Tangquan Zheng" , "Bo Zhang" In-Reply-To: <20261205023024.3718932-2-zhaonanzhe@xiaomi.com> References: <20261205023024.3718932-1-zhaonanzhe@xiaomi.com> <20261205023024.3718932-2-zhaonanzhe@xiaomi.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 06 Dec 2026 02:34:43 +0000 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2C09740009 X-Rspam-User: X-Stat-Signature: w61itdhj4a34e6rdckg47q3x41eerziu X-Spam-Status: No, score=-11.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796524485-685079 X-HE-Meta: U2FsdGVkX185L168LqLAx/JI0T8q0UoutnZTmOMRbh3stR0AHt7jt3dKQuSwNWox4PP1cXbHLSI2+scNDsIaFjAYDyULi7+BuxcmMi4KCNQaKPRxIvkwARrfyVGA346KTAD0sk9MXlGgW3yNm59Qt00sLpU6pqkLUW/B/aW5A17qXUs9oMJccDfsFttwHla5VIyYqbDF45HYmls73CqqvA4bQIEht+SmDeaGQOEbYzIIaptUhkCrijO0qCS2Wj72V8699P9HzlSxn7SAO09vREbv0NfIvKWIz94jJhFLKOJeHd/cnQWNmsdhYolMzKYgLGoi0xTTBKF4Hj+x7xBElo/pEWX0jPuKOZPswHnnqko7hM4fQL06MM63tBuIMjm5om04fosUXAWABTjapeTSKVhYwMQc5Gw0lGhYq/KX0LcmyU6sENZkVbfVJlL5S0o105v3fZqtQ4meuGkStj0asHG1MijGXxtAe/ALRkY8/PCqK5YdkBA81db5MbyJGGNmgb+FkuA4v2dKBeHAv38GGXp81ik0tgJrLqrk9it0k5ZdBtOo6AWdVLKJjVe/akHOyKFoUodeXIBGO8Ja4WbJGLLKUxaHal7jUY8YNs0OrceT+Y9q+8YFFtk05pimujHM9+Xt5ZXoNkymf7PY6L08CXBLpDudEb3AHJgz2l50PCANNMRkEHEjgK1ELFamsK/ZUZKNea36a5Ch4/GOl0XRbtKMBz+PCP2WPR8iRcGRHnpel02feDI8AoAQNoa57l90h/yfnCXHOIJtrFLX5hSxXDiMiDZ2oypWFTN++MmftDOieRliNq0oaKWOCqy+0qFUvtlzdc3zbnsjJJNN1jJZnMT2DR24lchyGyUISZXEMb6AtyMOKAL7N7u+ry/9ErsSRQ08YJ1+H1UNUnAKzshMQNqFG74m9X6xcDmJl0MzvDcgPI7LoL1pqTXgS9iWF4DRDzCU6hg2jmxyZyvB5lw uuEw+s49 z3ciOcCpkNUjB+6FcyZ708RLaX1gSH62dXBpTPUbq6LvCGlYx+prfy9myltHMAHEUW8oqoktcpGdslf8HD54/e+vxp5JBlzowRVpw4IpGxf6kFq8wDA+khDwwulBxFpX01lP4JetJ1jGYSYoEOFMjUKeQz725jNvBGc2DUemdDLwtyGFbk0ywgc5zEJXRKTlfmxIt7q1M6m2g/hIdsY+cq7PqZ+xLwfTBo2z3/aLzGlPr3x0w6+j/uot9EbWNNxxG1M2fsZoc/F/GHZWqFjOtEWnuLfsXl22M5+OxmYEkYfzb1AJrTm28+k19o2/US0Ri1uv9CQEiB42XizJ2Hj8x+5TdVFadrKhHUQcvo+NLH3lhEA/MmQTeu2PI3NjyuR4A5i43DVUhmvBYbyRk3dETn+8rrQjSGBHHgpYBjzljehoUPjdizLYJbDNgrUSl9y3k6uwB7uh7ThHIomY2DJtfulTyKjLs2cuSuIlVafJ4167tO0ZWR4BNBDHgHOjNj0ytEENwwMSUgxnQxAfhq0ZZr3vrCIBDoXf5uhhqbwCw88tsd1FivBaRsPFOBmZMzz6sDfS64pg645e7GQDOjcjdd9oun4q6DAJQUl+lhLeTuOW5G0zOzCSRPKtkP35asrDQ5O5SLpJpaZqL+pvlZWPj86CCXs3yM9VvwfI9OoEFmwkKGSG3Whh0Pu3gZwxmMAAXSFf5fwcbQtXN23G3kiFU6SuORTpL9+ZnBmOv5X2N1aob4YglN1fWrRrcqWty57ROhUDFtKdt+zJWnzA769778Mtmn7Ga0f1e8TYzslY8GORQFnx1SC6qYrfj77RqQbj9R1U6SpanL8MmMfWBjSoaVfnlEQ== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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 objec= t can span 0-order > page boundaries. The code refers to these linked pages as a single entity > called zspage. > =20 > -For simplicity, zsmalloc can only allocate objects of size up to PAGE_SI= ZE > -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, failu= re > -is returned (see zs_store). > +By default, zsmalloc allocates objects of size up to PAGE_SIZE, since th= is > +satisfies the requirements of single-page compression (in the worst case, > +a page is incompressible and is thus stored "as-is", i.e. in uncompresse= d 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 speci= fies > +the allocation order for multi-page objects, currently supported up to o= rder 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 @@ > =20 > #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 =3D 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=3D16, 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=3D8, 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 =3D PAGE_SIZE >> CLASS_BITS (and a static_assert that ZS_SIZE_CLASSES <=3D (1U << (CLASS_BITS + 1))) would avoid both regressions. [ ... ] > @@ -1154,12 +1221,13 @@ void zs_obj_read_sg_begin(struct zs_pool *pool, c= onst struct zs_handle *handle, > read_unlock(&pool->lock); > =20 > class =3D zspage_class(pool, zspage); > - off =3D offset_in_page(class->size * obj_idx); > + page_size =3D class_size_to_zs_size(class->size); > + off =3D (class->size * obj_idx) & (page_size - 1); > =20 > if (!ZsHugePage(zspage)) > off +=3D ZS_HANDLE_SIZE; > =20 > - if (off + mem_len <=3D PAGE_SIZE) { > + if (off + mem_len <=3D 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, con= st struct zs_handle *handle, > size_t sizes[2]; > =20 > /* this object spans two pages */ > - sizes[0] =3D PAGE_SIZE - off; > + sizes[0] =3D page_size - off; > sizes[1] =3D mem_len - sizes[0]; > =20 > 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 =3D 8 gets ZS_MIN_ALLOC_SIZE =3D 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 =3D=3D 4096 =3D=3D page_size and sizes[0] =3D 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 =3D get_next_zpdesc(zpdesc) and resetting off =3D 0 when off =3D=3D page_size after adding ZS_HANDLE_SIZE avoids emitting the empty entry. [ ... ] > @@ -1376,13 +1454,15 @@ static void obj_free(int class_size, unsigned lon= g 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; > =20 > =20 > obj_to_location(obj, &f_zpdesc, &f_objidx); > - f_offset =3D offset_in_page(class_size * f_objidx); > + page_size =3D class_size_to_zs_size(class_size); > + page_mask =3D ~(page_size - 1); > + f_offset =3D (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 =3D offset_in_page(class->size * obj_idx); vaddr =3D kmap_local_zpdesc(zpdesc); link =3D (struct link_free *)(vaddr + off); link->handle =3D (unsigned long)dst | OBJ_ALLOCATED_TAG; kunmap_local(vaddr); } ... } For a non-huge multi-page object at an in-zpdesc offset >=3D PAGE_SIZE (for example order 2, class->size =3D 4160, obj_idx =3D 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 *p= ool, > fg =3D putback_zspage(class, src_zspage); > if (fg =3D=3D ZS_INUSE_RATIO_0) { > free_zspage(pool, class, src_zspage); > - pages_freed +=3D class->pages_per_zspage; > + pages_freed +=3D class_pages_per_zspage(class); > } > src_zspage =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205023024.3718= 932-1-zhaonanzhe@xiaomi.com?part=3D1