From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: Longlong Xia <xialonglong@kylinos.cn>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Minchan Kim <minchan@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.6.y 9/9] zsmalloc: account for handle size in class lookup
Date: Fri, 4 Sep 2026 16:32:17 -0400 [thread overview]
Message-ID: <20260904203217.1548415-9-sashal@kernel.org> (raw)
In-Reply-To: <20260904203217.1548415-1-sashal@kernel.org>
From: Longlong Xia <xialonglong@kylinos.cn>
[ Upstream commit f7bf5cd5b5f2b13fe2361860880c4e214c08b440 ]
zs_lookup_class_index() lets zram recompression decide whether a newly
compressed object would use a smaller size class. It currently classifies
the payload size directly, while zs_malloc() adds ZS_HANDLE_SIZE before
selecting the class.
This makes lookup disagree with allocation near size-class boundaries.
With 4 KiB pages, CONFIG_ZSMALLOC_CHAIN_SIZE=8, and 64-bit handles, a
1025-to-1024-byte recompression appears to move from class 64 to class 62
although both allocations use class 64. Conversely, a 1049-to-1025-byte
recompression appears to stay in class 64 although the allocations move
from class 65 to class 64.
As a result, zram can accept replacements with no allocation benefit or
reject ones that would save memory, potentially marking the object
incompressible.
Factor size-class selection into lookup_size_class(), account for the
handle there, and use the helper for both lookup and allocation.
Link: https://lore.kernel.org/20260809115518.3791787-1-xialonglong2025@163.com
Fixes: 7c2af309abd2 ("zram: add size class equals check into recompression")
Assisted-by: Codex:gpt-5.6-sol
Signed-off-by: Longlong Xia <xialonglong@kylinos.cn>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
mm/zsmalloc.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index b63864f5fa398..dde7162701f2a 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -498,6 +498,11 @@ static int get_size_class_index(int size)
return min_t(int, ZS_SIZE_CLASSES - 1, idx);
}
+static struct size_class *lookup_size_class(struct zs_pool *pool, size_t size)
+{
+ return pool->size_class[get_size_class_index(size + ZS_HANDLE_SIZE)];
+}
+
static inline void class_stat_add(struct size_class *class, int type,
unsigned long cnt)
{
@@ -1140,7 +1145,7 @@ unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
{
struct size_class *class;
- class = pool->size_class[get_size_class_index(size)];
+ class = lookup_size_class(pool, size);
return class->index;
}
@@ -1349,9 +1354,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
if (!handle)
return (unsigned long)ERR_PTR(-ENOMEM);
- /* extra space in chunk to keep the handle */
- size += ZS_HANDLE_SIZE;
- class = pool->size_class[get_size_class_index(size)];
+ class = lookup_size_class(pool, size);
/* class->lock effectively protects the zpage migration */
spin_lock(&class->lock);
--
2.53.0
prev parent reply other threads:[~2026-09-04 20:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 13:01 FAILED: patch "[PATCH] zsmalloc: account for handle size in class lookup" failed to apply to 6.6-stable tree gregkh
2026-09-04 20:32 ` [PATCH 6.6.y 1/9] mm/zsmalloc: remove migrate_write_lock_nested() Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 2/9] mm/zsmalloc: remove unused zspage->isolated Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 3/9] mm/zsmalloc: remove set_zspage_mapping() Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 4/9] mm/zsmalloc: remove_zspage() don't need fullness parameter Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 5/9] mm/zsmalloc: remove get_zspage_mapping() Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 6/9] mm/zsmalloc: change back to per-size_class lock Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 7/9] mm/zsmalloc: clarify class per-fullness zspage counts Sasha Levin
2026-09-04 20:32 ` [PATCH 6.6.y 8/9] zsmalloc: rename class stat mutators Sasha Levin
2026-09-04 20:32 ` Sasha Levin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260904203217.1548415-9-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=minchan@kernel.org \
--cc=senozhatsky@chromium.org \
--cc=stable@vger.kernel.org \
--cc=xialonglong@kylinos.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox