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 B63E1606A31A for ; Wed, 2 Dec 2026 04:39:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=jD/aikkO Received: by kanga.kvack.org (Postfix) id EE7CA6B0604; Tue, 1 Dec 2026 23:39:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC2CC6B0624; Tue, 1 Dec 2026 23:39:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD4916B0625; Tue, 1 Dec 2026 23:39:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A96566B0604 for ; Tue, 1 Dec 2026 23:39:24 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2E16D1C0F49 for ; Wed, 2 Dec 2026 04:39:24 +0000 (UTC) X-FDA: 85497789048.12.120CF6E Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by imf02.hostedemail.com (Postfix) with ESMTP id 6EE7480009 for ; Wed, 2 Dec 2026 04:39:22 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b="jD/aikkO"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3-aAPawoKCNgD8HB4I6B8HAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jenhaochen.bounces.google.com designates 209.85.167.199 as permitted sender) smtp.mailfrom=3-aAPawoKCNgD8HB4I6B8HAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jenhaochen.bounces.google.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796186362; b=FpSMAMEyFS7O+JwvHNTxJ+3j5nMn3JglPtxvkMwzAnn4gQ+QRq3Jrliy4pQPTvxv0ZN98s 5M2ZQ9mKLTW3+z5iHV5HlEzoUTztCABszHP3+vxyVQkP/2vqoZsfhdGtsoAXuTBOS6xKoP 6VPQIArvAw8RYJOxRt70yiOqTToTjZg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796186362; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hKdTwLX+z/XCuiyq8+z2O5H4wxcwOmx5rROUzzLaZAg=; b=e+DgsXJw+7KpcE2RL6/Xqa2BiiwVD+qVYHGTYV6di+ReNnaG3+gKjYw/K9tJKvtEmX1LU6 4jjqN+wCt7mxD/K+d/z/qONeF3piAZQJdJSWaKzJScZF1SiQB809o+JKEhUSyApWhfAf6d 4czUFyplalawrTmOfMaMZQJaBVpsAbg= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b="jD/aikkO"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3-aAPawoKCNgD8HB4I6B8HAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jenhaochen.bounces.google.com designates 209.85.167.199 as permitted sender) smtp.mailfrom=3-aAPawoKCNgD8HB4I6B8HAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--jenhaochen.bounces.google.com Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-57eac2a0602so4420731b6e.2 for ; Tue, 01 Dec 2026 20:39:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796186361; x=1796791161; darn=kvack.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=hKdTwLX+z/XCuiyq8+z2O5H4wxcwOmx5rROUzzLaZAg=; b=jD/aikkOe14fccWXa2qejBwpriNQWU0koiz36Rwu1auqkTS3cFa0K2DU7Y3vjcQL8p kvxDB+1028xL8aEYPYo9XyNwnV9mqwYq0UtTB5ZbcM3U79ZwqlUo4ZZQo4pHKxPi9tZl BjRymIfPY1wgyj9PDorY3Xy0OlMlCKj9KdNmYN/LY2Vk4OHpGw8LRNwLWqZ1bVxYUPQe 9d1pJQudK/DU5JLSqLKedOI6U/EB8pM5VWgqtpVMcbQhB5ooe9kD9oBfqDNXAQjLa4UE t2PdOZKxhIxeYqDGqSMHEpyFvVTQmNLi0pawrpJPqBiiMVzraJAsgoKkgeSMwfn1c3Ok GY3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796186361; x=1796791161; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hKdTwLX+z/XCuiyq8+z2O5H4wxcwOmx5rROUzzLaZAg=; b=Yjrti24iQTmG32oTTMUxO2Bu0LLvQkyGNCgKy10NrqjKLgSvOshFM6g8vQxZpo93j3 IkEImZO5RToQM9UuXz2u/0qRPxu88OUVX8faV4yPXtj2e72vCyPIeCYY5dTzQg+8seDg 3hhCz0W5bo2KCyECRoxfxwh6LRCjxmoDl17TcCH3mftRkINUs5/WUn6KgxOEscqbuOd+ 4v6lJMEs8HSzVxgEa3mAK/gulC7VgcI0lwaEKI9j5zDoUfIgHiXsziHWlZqTkQiMUu2L rl5jIqhV8f7xA0kIvTvvqUWem3zIP15KJahw/Z0jRwa6i9nsCbkrKSmmjhsG/2jpJTPN BD1g== X-Gm-Message-State: APuU4Z9qzWquJcqp18eZgm5eyKpinQFbpSolyOlVQoZ+hjo9eQ0yRMWT hxmgzbxsEVQrlGe5h4V5/HGp7BgAWlGbhjlUGgj8yXSW5B6Kt9PQhMAdg3APHXxM2ycLDG0txWh 73OgnH9t5WKzcdeZvUGR9Eg== X-Received: from iobn6-n2.prod.google.com ([2002:a05:6602:c6c6:20b0:9e8:de6c:a5a3]) (user=jenhaochen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:10d2:b0:4e5:c2b4:33a3 with SMTP id 5614622812f47-57a0be9d008mr45408679b6e.45.1796186361347; Tue, 01 Dec 2026 20:39:21 -0800 (PST) Date: Wed, 2 Dec 2026 04:39:11 +0000 In-Reply-To: <20261202043911.2556266-1-jenhaochen@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261202043911.2556266-1-jenhaochen@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202043911.2556266-3-jenhaochen@google.com> Subject: [PATCH v2 2/2] mm/zsmalloc: pass payload location to obj_write() From: Jenhao Chen To: Sergey Senozhatsky , Minchan Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Brian Geffon , minchan@google.com, liumartin@google.com, benwei@google.com Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: w1aurdzd8goj6e7t4icgsucso7yrqgdi X-Rspamd-Queue-Id: 6EE7480009 X-Rspamd-Server: rspam02 X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-HE-Tag: 1796186362-311450 X-HE-Meta: U2FsdGVkX1+3mYuOOF/tecT8R0YS89j7m1hkPQq5waSZ2VzpVU4pON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CVrwxXd/EBC1nFUrpQ2hUnVLk8Hy2OlO75NFuDNMiXvsKX4uo7NiYPx2ZQI04KfXdGysaMSLHxg0ZTiw1zsCdyM14nmvEe/qods0bRSO11YFqQu9xwPseERzD6BxO0RqY5/YtplK0Wv/v4mTUkZD1CPWe3iSoEMJabTxKW5gAgWhU7Am+Exbcpp9VKENuqhojPQ9Km1kKQAktbR7frfi2MUDV5WnT/VkoQV/9MsU25irGftVN+hRVNKtgU/Bv2A/UgJ9+LJFMknBwweihBfNZ80fEDnAZEZWfBQ1THv1cEOXQzoOeQkzlBrtETXNAkzS55phLSLa0+y4aE7hQdVv6Mj3ID/sBH3xnrbNxsMlQ27Smhm/GQt2CNo0KlrJDCg216ds3Xb0Ai5RYTaoMGiKFqGCPDb2N0dB7/kdLkQbX+UP6Ma+cU69NMYSrFxgDRhLXG99zbJFAU5w++yL7pGxhmH7nrfVwxpiMUHr3UKraoNoJtMdc2DtxXuKlocMmR9fm5oozAztvuUNFbvGEJizrtXwYJk7cDBJgFsAVffDL48tfTX+lK+kVGkhg5efRQD8hWUpptxhuUXI2GggVf/x3aJe5oY32tvYzTN/i/65YK/wU4G+GxwwlQRtIYY0pkTVMo+Umj0ColZ8i294jbMxEqWTW3OwoRJHsRl3MhGO3kDCfQsQNjbGg6zd9xEuQW5izOL+kP07l5IP3YHcjaIaC 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 obj_malloc() already walks to m_zpdesc, computes m_offset, and checks ZsHugePage(zspage) before encoding (m_zpdesc, obj_idx, zspage->class) into obj; it also re-derives size_class from pool->size_class[] even though every caller already has it. obj_write() then immediately decodes obj via obj_to_location(), recomputes class->size * obj_idx, and re-checks ZsHugePage(zspage). Both helpers are also left out of line by Clang 22 and GCC 15 (obj_write.isra.0), adding two calls per store (one inside class->lock). Pass size_class and an optional struct obj_loc *loc (zpdesc and payload offset past ZS_HANDLE_SIZE for non-huge zspages) to obj_malloc(), pass (loc.zpdesc, loc.off) directly to obj_write(), and mark both helpers __always_inline. Add VM_WARN_ON_ONCE(class->index != zspage->class) to guard the caller-supplied class. The location handoff and __always_inline are paired because passing &loc out of line spills loc to the stack (+1.7 insns/pair on Clang -O2), while forcing __always_inline without struct obj_loc fails to CSE across the lock calls on GCC 15 (+3.7% cycles, +139..243 B extra .text). With __always_inline, @loc is a compile-time constant at every call site (scalarized in zs_store()/zs_store_slow() and folded away when NULL in migrate_zspage()). Locking is unchanged: obj_write() already decoded the local obj returned by obj_malloc(). On the fast path the location stays stable under class->lock until zspage_read_lock() takes over; on the slow path the zspage is unpublished until insert_zspage() and SetZsPageMovable() run after obj_write(). In migrate_zspage(), src_zspage and dst_zspage come from the same class in __zs_compact(). mm/zsmalloc.o sizes (delta vs patch 1; trades modest net .text growth for a faster zs_store()): Build zs_store() net .text bloat-o-meter arm64 Clang 22 -O2 940 -> 1,360 B +712 B +1.96% arm64 Clang 22 AFDO 940 -> 1,360 B +728 B +2.03% x86_64 Clang 22 -O2 622 -> 993 B +396 B +2.66% x86_64 Clang 22 AFDO 850 -> 1,200 B +268 B +1.83% x86_64 GCC 15 -O2 540 -> 885 B +281 B +2.10% Steady-state zs_store() + zs_free() benchmark (delta vs patch 1; same setup, 4K pages, chain size 8, CONFIG_DEBUG_VM=n; arm64 insns via QEMU, x86_64 native on AMD EPYC 7B13, n=15 interleaved runs without retpoline thunks): Build insns/pair pair cyc zs_store cyc pool grow arm64 Clang 22 -O2 -5.4% - - - arm64 Clang 22 AFDO -5.4% - - - x86_64 Clang 22 -O2 -5.8% -5.1% -6.4% -3.7% x86_64 Clang 22 AFDO -6.0% -4.0% -5.4% -5.4% x86_64 GCC 15 -O2 -3.3% neutral neutral -2.4% Signed-off-by: Jenhao Chen --- mm/zsmalloc.c | 66 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 5f4e589..3e660a0 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -261,6 +261,17 @@ struct link_free { }; }; +/* + * Payload location handed from obj_malloc() to obj_write(). @off is past + * the inline handle for !ZsHugePage() zspages and may equal PAGE_SIZE, in + * which case the payload starts at offset 0 of get_next_zpdesc(@zpdesc). + * Valid only while the zspage is pinned or unpublished. + */ +struct obj_loc { + struct zpdesc *zpdesc; + unsigned long off; +}; + static struct kmem_cache *zspage_cachep; struct zs_pool { @@ -1190,19 +1201,10 @@ void zs_obj_read_sg_end(struct zs_pool *pool, const struct zs_handle *handle) } EXPORT_SYMBOL_GPL(zs_obj_read_sg_end); -static void obj_write(struct size_class *class, struct zspage *zspage, - unsigned long obj, const void *src, size_t size) +/* Always inlined so (zpdesc, off) stay in registers across zs_store(). */ +static __always_inline void obj_write(struct zpdesc *zpdesc, unsigned long off, + const void *src, size_t size) { - struct zpdesc *zpdesc; - unsigned long off; - unsigned int obj_idx; - - obj_to_location(obj, &zpdesc, &obj_idx); - off = offset_in_page(class->size * obj_idx); - - if (!ZsHugePage(zspage)) - off += ZS_HANDLE_SIZE; - if (off + size <= PAGE_SIZE) { /* this object is contained entirely within a page */ void *dst = kmap_local_zpdesc(zpdesc); @@ -1241,19 +1243,20 @@ size_t zs_huge_class_size(struct zs_pool *pool) } EXPORT_SYMBOL_GPL(zs_huge_class_size); -static unsigned long obj_malloc(struct zs_pool *pool, - struct zspage *zspage, struct zs_handle *handle) +/* Always inlined so @loc folds at compile time (NULL in migrate_zspage()). */ +static __always_inline unsigned long obj_malloc(struct size_class *class, + struct zspage *zspage, + struct zs_handle *handle, + struct obj_loc *loc) { int i, nr_zpdesc, offset; unsigned long obj; struct link_free *link; - struct size_class *class; - struct zpdesc *m_zpdesc; - unsigned long m_offset; + unsigned long m_offset, payload_off; void *vaddr; - class = pool->size_class[zspage->class]; + VM_WARN_ON_ONCE(class->index != zspage->class); obj = get_freeobj(zspage); offset = obj * class->size; @@ -1267,12 +1270,15 @@ static unsigned long obj_malloc(struct zs_pool *pool, vaddr = kmap_local_zpdesc(m_zpdesc); link = (struct link_free *)vaddr + m_offset / sizeof(*link); set_freeobj(zspage, link->next >> OBJ_TAG_BITS); - if (likely(!ZsHugePage(zspage))) + if (likely(!ZsHugePage(zspage))) { /* record handle in the header of allocated chunk */ link->handle = (unsigned long)handle | OBJ_ALLOCATED_TAG; - else + payload_off = m_offset + ZS_HANDLE_SIZE; + } else { zspage->first_zpdesc->handle = (unsigned long)handle | OBJ_ALLOCATED_TAG; + payload_off = m_offset; + } kunmap_local(vaddr); mod_zspage_inuse(zspage, 1); @@ -1280,6 +1286,11 @@ static unsigned long obj_malloc(struct zs_pool *pool, obj = location_to_obj(m_zpdesc, obj, zspage->class); record_obj(handle, obj); + if (loc) { + loc->zpdesc = m_zpdesc; + loc->off = payload_off; + } + return obj; } @@ -1295,7 +1306,7 @@ static noinline int zs_store_slow(struct zs_pool *pool, { struct size_class *class = lookup_size_class(pool, size); struct zspage *zspage; - unsigned long obj; + struct obj_loc loc; int newfg; zspage = alloc_zspage(pool, class, gfp, nid); @@ -1307,8 +1318,8 @@ static noinline int zs_store_slow(struct zs_pool *pool, * can allocate the object and copy data before taking class->lock * and without zspage_read_lock(). */ - obj = obj_malloc(pool, zspage, handle); - obj_write(class, zspage, obj, src, size); + obj_malloc(class, zspage, handle, &loc); + obj_write(loc.zpdesc, loc.off, src, size); spin_lock(&class->lock); newfg = get_fullness_group(class, zspage); @@ -1345,7 +1356,6 @@ int zs_store(struct zs_pool *pool, struct zs_handle *handle, const void *src, size_t size, gfp_t gfp, const int nid) { struct size_class *class; - unsigned long obj; struct zspage *zspage; if (WARN_ON_ONCE(!valid_unallocated_handle(handle))) @@ -1363,14 +1373,16 @@ int zs_store(struct zs_pool *pool, struct zs_handle *handle, const void *src, spin_lock(&class->lock); zspage = find_get_zspage(class); if (likely(zspage)) { - obj = obj_malloc(pool, zspage, handle); + struct obj_loc loc; + + obj_malloc(class, zspage, handle, &loc); /* Now move the zspage to another fullness group, if required */ fix_fullness_group(class, zspage); class_stat_add(class, ZS_OBJS_INUSE, 1); zspage_read_lock(zspage); spin_unlock(&class->lock); - obj_write(class, zspage, obj, src, size); + obj_write(loc.zpdesc, loc.off, src, size); zspage_read_unlock(zspage); return 0; } @@ -1676,7 +1688,7 @@ static void migrate_zspage(struct zs_pool *pool, struct zspage *src_zspage, } used_obj = handle_to_obj(handle); - free_obj = obj_malloc(pool, dst_zspage, handle); + free_obj = obj_malloc(class, dst_zspage, handle, NULL); zs_object_copy(class, free_obj, used_obj); obj_idx++; obj_free(class->size, used_obj); -- 2.56.1.911.g883d8bcb20-goog