[RFC PATCH 2/4] zsmalloc: introduce lockless zs_class_can_compact()
From: Sergey Senozhatsky
Date: Thu Dec 03 2026 - 05:14:38 EST
__zs_compact() always takes pool->lock for writing and class->lock
before checking if a class can be compacted, which can be expensive
and slow if the locks are contended.
Introduce an optimistic lockless fast path and check
zs_class_can_compact() locklessly at the start of __zs_compact()
before taking the locks; once locked, the loop checks it again
with exact counts.
x86_64 QEMU/KVM (8 vCPUs, LZ4 zram, 64 MiB across 64 size-class
buckets, medians across interleaved boots):
before after
No-op compact (2000 calls) 9.50 us 6.01 us (-36.8%)
Partial frag (4/64 buckets, 50 rounds) 11.75 us 8.68 us (-26.1%)
Contended compact + 4 I/O threads 10.27 us 6.40 us (-37.7%)
Contended concurrent I/O p99 4.57 us 4.40 us (-3.7%)
Suggested-by: Nhat Pham <nphamcs@xxxxxxxxx>
Suggested-by: Chris Wiseman <chriswiseman@xxxxxxxxxx>
Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
mm/zsmalloc.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index e4a359fcfaa8..6e4caf264330 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -535,18 +535,27 @@ static struct size_class *lookup_size_class(struct zs_pool *pool, size_t size)
static inline void class_stat_add(struct size_class *class, int type,
unsigned long cnt)
{
- class->stats.objs[type] += cnt;
+ WRITE_ONCE(class->stats.objs[type], class->stats.objs[type] + cnt);
}
static inline void class_stat_sub(struct size_class *class, int type,
unsigned long cnt)
{
- class->stats.objs[type] -= cnt;
+ WRITE_ONCE(class->stats.objs[type], class->stats.objs[type] - cnt);
}
static inline unsigned long class_stat_read(struct size_class *class, int type)
{
- return class->stats.objs[type];
+ return READ_ONCE(class->stats.objs[type]);
+}
+
+static inline bool zs_class_can_compact(struct size_class *class)
+{
+ unsigned long obj_allocated = class_stat_read(class, ZS_OBJS_ALLOCATED);
+ unsigned long obj_used = class_stat_read(class, ZS_OBJS_INUSE);
+
+ return obj_allocated > obj_used &&
+ obj_allocated - obj_used >= class->objs_per_zspage;
}
static void __init zs_stat_init(void)
@@ -1999,6 +2008,9 @@ static unsigned long zs_can_compact(struct size_class *class)
return 0;
obj_wasted = obj_allocated - obj_used;
+ if (obj_wasted < class->objs_per_zspage)
+ return 0;
+
obj_wasted /= class->objs_per_zspage;
return obj_wasted * class->pages_per_zspage;
@@ -2011,6 +2023,14 @@ static unsigned long __zs_compact(struct zs_pool *pool,
struct zspage *dst_zspage = NULL;
unsigned long pages_freed = 0;
+ /*
+ * Fast lockless check to avoid taking pool->lock and class->lock
+ * for classes that have no compactable pages. The exact counts
+ * are re-checked under locks below.
+ */
+ if (!zs_class_can_compact(class))
+ return 0;
+
/*
* Protect against races between zpage migration and zs_free() /
* zs_rebind_handle() (only when ZS_OBJ_CLASS_BITS does not apply),
@@ -2018,7 +2038,7 @@ static unsigned long __zs_compact(struct zs_pool *pool,
*/
write_lock(&pool->lock);
spin_lock(&class->lock);
- while (zs_can_compact(class)) {
+ while (zs_class_can_compact(class)) {
int fg;
if (!dst_zspage) {
--
2.56.1.987.g2ec0ab4260-goog