Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] mm/zsmalloc: optimize zs_store() fast path
@ 2026-12-02  4:39 Jenhao Chen
  2026-12-02  4:39 ` [PATCH v2 1/2] mm/zsmalloc: outline slow path in zs_store() Jenhao Chen
  2026-12-02  4:39 ` [PATCH v2 2/2] mm/zsmalloc: pass payload location to obj_write() Jenhao Chen
  0 siblings, 2 replies; 3+ messages in thread
From: Jenhao Chen @ 2026-12-02  4:39 UTC (permalink / raw)
  To: Sergey Senozhatsky, Minchan Kim, Andrew Morton
  Cc: linux-mm, linux-kernel, llvm, Johannes Weiner, Yosry Ahmed,
	Nhat Pham, Chengming Zhou, Brian Geffon, minchan, liumartin,
	benwei

This two-patch series optimizes the zs_store() fast path on top of the
stabilized zsmalloc changes in mm-new / next-20261201 (mm tree commit
5973b750e388):

1. Outline zs_store()'s new-zspage allocation path into a 6-arg noinline
   zs_store_slow(), matching free_page_chain().  This keeps alloc_zspage()
   and its zpdescs[] array off the fast path, enables sibling tail calls
   on x86_64, and shrinks zs_store() by 19-73%.

2. Pass size_class and a payload struct obj_loc directly from obj_malloc()
   to obj_write() and mark both __always_inline, eliminating the
   location_to_obj() -> obj_to_location() / class->size * obj_idx /
   ZsHugePage() round-trip and two out-of-line calls per store.

Cumulative results (base -> patch 2; 4K pages, chain size 8):
- arm64  Clang 22 -O2:  -5.2% insns/pair, net .text +858 B
- arm64  Clang 22 AFDO: -6.4% insns/pair, zs_store -56%, net .text +930 B
- x86_64 Clang 22 -O2:  -5.9% insns/pair, -6.0% pair / -7.3% zs_store()
  cycles, -5.0% pool growth, net .text +430 B
- x86_64 Clang 22 AFDO: -6.9% insns/pair, -4.7% pair / -6.9% zs_store()
  cycles, -4.3% pool growth, zs_store -45%, net .text +446 B
- x86_64 GCC 15   -O2:  -4.1% insns/pair, zs_store() cycles neutral,
  -3.1% pool growth, zs_store -56%, net .text +372 B

Verified byte-for-byte read-back via zs_obj_read_sg_begin()/end() across
1,024 regular objects (384..3136 B, including page-spanning slots) plus
4096 B huge objects, and clean builds across default, CONFIG_COMPACTION=n,
and CONFIG_DEBUG_VM=y.

Changes in v2:
- Rebased onto next-20261201 (Sergey's object alloc API redesign and
  Wenchao's ZS_OBJ_CLASS_BITS lockless zs_free()).
- Split into 2 patches separating slow-path outlining from the payload
  location handoff and inlining.
- Outlined the full slow path into a 6-arg noinline zs_store_slow()
  instead of only marking alloc_zspage() noinline.
- Passed size_class and struct obj_loc directly to obj_write() instead of
  relying on compiler CSE across lock calls.
- Dropped __always_inline on obj_free(): on next-20261201, Clang 22
  already inlines obj_free(), while forcing it inline on GCC 15 spills
  handle across _raw_spin_lock and regresses zs_free() (~+5 cycles).
- Added bloat-o-meter / net .text deltas and cold pool growth numbers.
- Link to v1: https://lore.kernel.org/r/20261030121700.260975-1-jenhaochen@google.com

Jenhao Chen (2):
  mm/zsmalloc: outline slow path in zs_store()
  mm/zsmalloc: pass payload location to obj_write()

 mm/zsmalloc.c | 126 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 77 insertions(+), 49 deletions(-)

-- 
2.56.1.911.g883d8bcb20-goog



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] mm/zsmalloc: outline slow path in zs_store()
  2026-12-02  4:39 [PATCH v2 0/2] mm/zsmalloc: optimize zs_store() fast path Jenhao Chen
@ 2026-12-02  4:39 ` Jenhao Chen
  2026-12-02  4:39 ` [PATCH v2 2/2] mm/zsmalloc: pass payload location to obj_write() Jenhao Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Jenhao Chen @ 2026-12-02  4:39 UTC (permalink / raw)
  To: Sergey Senozhatsky, Minchan Kim, Andrew Morton
  Cc: linux-mm, linux-kernel, llvm, Johannes Weiner, Yosry Ahmed,
	Nhat Pham, Chengming Zhou, Brian Geffon, minchan, liumartin,
	benwei

Because alloc_zspage() is static with a single caller, GCC 15 (-O2) and
Clang 22 (with CONFIG_AUTOFDO_CLANG) inline it into zs_store().  That
pulls alloc_zspage()'s zpdescs[ZS_MAX_PAGES_PER_ZSPAGE] array (64-128 B
on 64-bit) and its zero-fill under CONFIG_INIT_STACK_ALL_ZERO into
zs_store()'s stack frame and inflates the fast-path body up to 3 KB.

Move the new-zspage allocation path into a static noinline helper
zs_store_slow(), following the same pattern as free_page_chain().  Keep
zs_store()'s 6-argument signature (re-deriving size_class inside the slow
path) so x86_64 emits a register-only sibling tail call (jmp) without
spilling nid to the stack.

mm/zsmalloc.o sizes (4K pages, chain size 8):

  Build                   zs_store()          net .text   bloat-o-meter
  arm64  Clang 22 -O2     1,156 ->   940 B    +146 B      +0.71%
  arm64  Clang 22 AFDO    3,060 ->   940 B    +202 B      +0.54%
  x86_64 Clang 22 -O2       784 ->   622 B     +34 B      +0.24%
  x86_64 Clang 22 AFDO    2,187 ->   850 B    +178 B      +1.21%
  x86_64 GCC 15   -O2     1,993 ->   540 B     +91 B      +0.33%

Steady-state zs_store() + zs_free() benchmark (8 classes, 384..3136 B,
1,024 live objects, 4K pages; arm64 insns via QEMU, x86_64 native on AMD
EPYC 7B13, n=15 interleaved runs):

  Build                   insns/pair    pair cycles   cold pool growth
  arm64  Clang 22 -O2     +0.2%         -             -
  arm64  Clang 22 AFDO    -1.1%         -             -
  x86_64 Clang 22 -O2     -0.1%         -1.0%         -1.4%
  x86_64 Clang 22 AFDO    -1.0%         -0.7%         +1.3% (noise)
  x86_64 GCC 15   -O2     -0.7%         +0.3% (noise) -0.7%

Signed-off-by: Jenhao Chen <jenhaochen@google.com>
---
 mm/zsmalloc.c | 66 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 25 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index a1b6161..5f4e589 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1283,6 +1283,46 @@ static unsigned long obj_malloc(struct zs_pool *pool,
 	return obj;
 }
 
+/*
+ * Allocate a new zspage and store into it.  Not inlined (see
+ * free_page_chain()), so alloc_zspage()'s zpdescs[] array and slow-path
+ * register pressure stay off the zs_store() fast path.  Matches
+ * zs_store()'s 6-argument signature to allow sibling tail calls on x86_64.
+ */
+static noinline int zs_store_slow(struct zs_pool *pool,
+				  struct zs_handle *handle, const void *src,
+				  size_t size, gfp_t gfp, const int nid)
+{
+	struct size_class *class = lookup_size_class(pool, size);
+	struct zspage *zspage;
+	unsigned long obj;
+	int newfg;
+
+	zspage = alloc_zspage(pool, class, gfp, nid);
+	if (!zspage)
+		return -ENOMEM;
+
+	/*
+	 * New zspage is not in class->fullness_list or movable yet, so we
+	 * 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);
+
+	spin_lock(&class->lock);
+	newfg = get_fullness_group(class, zspage);
+	insert_zspage(class, zspage, newfg);
+	atomic_long_add(class->pages_per_zspage, &pool->pages_allocated);
+	class_stat_add(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage);
+	class_stat_add(class, ZS_OBJS_INUSE, 1);
+
+	/* We completely set up zspage so mark them as movable */
+	SetZsPageMovable(pool, zspage);
+	spin_unlock(&class->lock);
+
+	return 0;
+}
 
 /**
  * zs_store() - Allocate block of given size from pool and copy data into it.
@@ -1306,7 +1346,6 @@ int zs_store(struct zs_pool *pool, struct zs_handle *handle, const void *src,
 {
 	struct size_class *class;
 	unsigned long obj;
-	int newfg;
 	struct zspage *zspage;
 
 	if (WARN_ON_ONCE(!valid_unallocated_handle(handle)))
@@ -1338,30 +1377,7 @@ int zs_store(struct zs_pool *pool, struct zs_handle *handle, const void *src,
 
 	spin_unlock(&class->lock);
 
-	zspage = alloc_zspage(pool, class, gfp, nid);
-	if (!zspage)
-		return -ENOMEM;
-
-	/*
-	 * New zspage is not in class->fullness_list or movable yet, so we
-	 * 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);
-
-	spin_lock(&class->lock);
-	newfg = get_fullness_group(class, zspage);
-	insert_zspage(class, zspage, newfg);
-	atomic_long_add(class->pages_per_zspage, &pool->pages_allocated);
-	class_stat_add(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage);
-	class_stat_add(class, ZS_OBJS_INUSE, 1);
-
-	/* We completely set up zspage so mark them as movable */
-	SetZsPageMovable(pool, zspage);
-	spin_unlock(&class->lock);
-
-	return 0;
+	return zs_store_slow(pool, handle, src, size, gfp, nid);
 }
 EXPORT_SYMBOL_GPL(zs_store);
 
-- 
2.56.1.911.g883d8bcb20-goog



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] mm/zsmalloc: pass payload location to obj_write()
  2026-12-02  4:39 [PATCH v2 0/2] mm/zsmalloc: optimize zs_store() fast path Jenhao Chen
  2026-12-02  4:39 ` [PATCH v2 1/2] mm/zsmalloc: outline slow path in zs_store() Jenhao Chen
@ 2026-12-02  4:39 ` Jenhao Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Jenhao Chen @ 2026-12-02  4:39 UTC (permalink / raw)
  To: Sergey Senozhatsky, Minchan Kim, Andrew Morton
  Cc: linux-mm, linux-kernel, llvm, Johannes Weiner, Yosry Ahmed,
	Nhat Pham, Chengming Zhou, Brian Geffon, minchan, liumartin,
	benwei

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 <jenhaochen@google.com>
---
 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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  4:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  4:39 [PATCH v2 0/2] mm/zsmalloc: optimize zs_store() fast path Jenhao Chen
2026-12-02  4:39 ` [PATCH v2 1/2] mm/zsmalloc: outline slow path in zs_store() Jenhao Chen
2026-12-02  4:39 ` [PATCH v2 2/2] mm/zsmalloc: pass payload location to obj_write() Jenhao Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox