Re: [RFC PATCH v4 0/2] mm/zsmalloc: defer object free to per-cpu batches

From: Sergey Senozhatsky

Date: Thu Nov 19 2026 - 20:44:48 EST


On (26/11/19 14:35), Sergey Senozhatsky wrote:
> On (26/11/09 18:59), Hongfeng Chen wrote:
> > On Fri, Oct 23, 2026 at 11:19 AM Nhat Pham <nphamcs@xxxxxxxxx> wrote:
> > > Is there any more juice we can squeeze from zsmalloc in that case?
> > >
> > > It would be nice if you can perf zsmalloc freeing. And yeah, maybe we
> > > can't optimize any further, but let's eliminate that first before we
> > > rely on offloading.
> >
> > Here is the perf breakdown of the free path:
> >
> > zram:
> >
> > __swap_cluster_free_entries 90.28%
> > zram_slot_free_notify 86.01%
> > slot_free 74.15%
> > zs_free 56.01%
> > __free_frozen_pages 19.85%
> > kmem_cache_free 10.15%
> > _raw_spin_lock 5.53%
> > fix_fullness_group 3.31%
> > dec_zone_page_state 2.43%
> > obj_free 2.17%
> > trylock_zspage 2.12%
>
> We removed one of kmem_cache_free() from zs_free() [1].
>
> [1] https://lore.kernel.org/linux-kernel/20261110054836.1515030-1-senozhatsky@xxxxxxxxxxxx

I suppose we can batch some operations in zs_free(), something like
this:

---

mm/zsmalloc.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 4a6ec43f418b..e0083962e4d3 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -43,6 +43,7 @@
#include <linux/debugfs.h>
#include <linux/zsmalloc.h>
#include <linux/fs.h>
+#include <linux/folio_batch.h>
#include <linux/workqueue.h>
#include "zpdesc.h"

@@ -891,21 +892,39 @@ static int trylock_zspage(struct zspage *zspage)
static inline void __free_zspage_lockless(struct zspage *zspage)
{
struct zpdesc *zpdesc, *next;
+ struct folio_batch fbatch;
+ struct zone *zone = NULL;
+ int nr = 0;

VM_BUG_ON(get_zspage_inuse(zspage));
VM_BUG_ON(zspage->fullness != ZS_INUSE_RATIO_0);
+ BUILD_BUG_ON(ZS_MAX_PAGES_PER_ZSPAGE > FOLIO_BATCH_SIZE);

+ folio_batch_init(&fbatch);
next = zpdesc = get_first_zpdesc(zspage);
do {
VM_BUG_ON_PAGE(!zpdesc_is_locked(zpdesc), zpdesc_page(zpdesc));
next = get_next_zpdesc(zpdesc);
reset_zpdesc(zpdesc);
zpdesc_unlock(zpdesc);
- zpdesc_dec_zone_page_state(zpdesc);
- zpdesc_put(zpdesc);
+
+ /* NR_ZSPAGES is per zone: one update per run of same-zone pages */
+ if (zpdesc_zone(zpdesc) != zone) {
+ if (nr)
+ mod_zone_page_state(zone, NR_ZSPAGES, -nr);
+ zone = zpdesc_zone(zpdesc);
+ nr = 0;
+ }
+ nr++;
+
+ /* Free all pages of the zspage in one go, see folios_put() */
+ folio_batch_add(&fbatch, zpdesc_folio(zpdesc));
zpdesc = next;
} while (zpdesc != NULL);

+ mod_zone_page_state(zone, NR_ZSPAGES, -nr);
+ folios_put(&fbatch);
+
cache_free_zspage(zspage);
}

--
2.56.1.750.gf70dc682cf-goog