From patchwork Tue Oct 25 16:26:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Romanov X-Patchwork-Id: 13019518 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B5D3C04A95 for ; Tue, 25 Oct 2022 16:26:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 787448E0002; Tue, 25 Oct 2022 12:26:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7102D8E0001; Tue, 25 Oct 2022 12:26:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B1008E0002; Tue, 25 Oct 2022 12:26:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4764D8E0001 for ; Tue, 25 Oct 2022 12:26:54 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 03393804C1 for ; Tue, 25 Oct 2022 16:26:53 +0000 (UTC) X-FDA: 80060000748.20.BBCA842 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by imf18.hostedemail.com (Postfix) with ESMTP id 4C8251C0042 for ; Tue, 25 Oct 2022 16:26:51 +0000 (UTC) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 278005FD03; Tue, 25 Oct 2022 19:26:49 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1666715209; bh=zkSUQe1qaY4QG+yHFWp5XX2esm9RY0bLyeMYknhjNYE=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=B8Wajy+C6AhIBkK6thNlQUr02wfHD/+K0NKnWCT+SMEshwd1xNJycaESOpg9Je5wj 1q3X/5nH+4K5keszWCpMV2x088NbD2Z97LuTgY3/6uDWsWIEoRDe10GARcScVBlyYr 8KBL3LSNOLy1sL8IivB57Aw0CuUGqGOjWjD4nWc9oyUcGPYBAsOuXKBMoE5zLqzizE VKZL3t9jTEn+H4jv5lKLQKcVWgt4dK6Mr2J3hjeQJT2s6Va35My2O82YaLFYPvMCot gDu2p96wHzpfKWasElGvqHNZC3Ma33MCtpzfVpWYkywyrANeWUegchOhPONA+qSnT/ ung4QmCoIVC/Q== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 25 Oct 2022 19:26:47 +0300 (MSK) From: Alexey Romanov To: , , , CC: , , , Alexey Romanov Subject: [PATCH v2] zram: add size class equals check into recompression Date: Tue, 25 Oct 2022 19:26:38 +0300 Message-ID: <20221025162638.7853-1-avromanov@sberdevices.ru> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Originating-IP: [172.16.1.6] X-ClientProxiedBy: S-MS-EXCH02.sberdevices.ru (172.16.1.5) To S-MS-EXCH01.sberdevices.ru (172.16.1.4) X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/10/25 10:18:00 #20514748 X-KSMG-AntiVirus-Status: Clean, skipped ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666715212; a=rsa-sha256; cv=none; b=4ep6RUyAeoZW1onr4/ldPefY1mWNGdsxoCVNAQp2iWt+RjnAZQb9uLxGvDrjXJsxtiw9aF Q9PXViHpe2dw4dcXg+mFNMmjn74wVc2fwMtBOzD7Bj29886j/HjuGgpqiWk3e/S1P2qqnD R8vPA5GO2TF6XWI1LieMMyQusm/o3X0= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=B8Wajy+C; spf=pass (imf18.hostedemail.com: domain of AVRomanov@sberdevices.ru designates 45.89.227.171 as permitted sender) smtp.mailfrom=AVRomanov@sberdevices.ru; dmarc=pass (policy=quarantine) header.from=sberdevices.ru ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666715212; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=zkSUQe1qaY4QG+yHFWp5XX2esm9RY0bLyeMYknhjNYE=; b=MJETFU5r4KPD07KDiub9XTGwRPKQoBf5mDVqIGK80gYpKm02YJSiCgX1JxF3IonAnn36sY ur1IpJReCbTS10w6TU0DOurAr12Cfa9Ady4bmIZqHPgwNq485zAZYhtDZpBAk4xA9EBjD3 X9zJGerzJjiGCNx/LhB6KyHQSyh1/98= X-Rspam-User: X-Rspamd-Queue-Id: 4C8251C0042 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=B8Wajy+C; spf=pass (imf18.hostedemail.com: domain of AVRomanov@sberdevices.ru designates 45.89.227.171 as permitted sender) smtp.mailfrom=AVRomanov@sberdevices.ru; dmarc=pass (policy=quarantine) header.from=sberdevices.ru X-Stat-Signature: zatzsjd6kyachio7f465s8u1r7ws9irp X-Rspamd-Server: rspam03 X-HE-Tag: 1666715210-663543 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: It makes no sense for us to recompress the object if it will be in the same size class. We anyway don't get any memory gain. But, at the same time, we get a CPU time overhead when inserting this object into zspage and decompressing it afterwards. Signed-off-by: Alexey Romanov --- drivers/block/zram/zram_drv.c | 5 +++++ include/linux/zsmalloc.h | 2 ++ mm/zsmalloc.c | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 517dae4ff21c..5dc51120fce4 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1671,6 +1671,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, unsigned long handle_next; unsigned int comp_len_next; unsigned int comp_len_prev; + unsigned int class_index_prev; + unsigned int class_index_next; struct zcomp_strm *zstrm; void *src, *dst; int ret; @@ -1695,6 +1697,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, ret = zcomp_compress(zstrm, src, &comp_len_next); kunmap_atomic(src); + class_index_prev = zs_lookup_class_index(zram->mem_pool, comp_len_prev); + class_index_next = zs_lookup_class_index(zram->mem_pool, comp_len_next); /* * Either a compression error or we failed to compressed the object * in a way that will save us memory. Mark the object so that we @@ -1702,6 +1706,7 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, */ if (comp_len_next >= huge_class_size || comp_len_next >= comp_len_prev || + class_index_next >= class_index_prev || ret) { zram_set_flag(zram, index, ZRAM_RECOMP_SKIP); zram_clear_flag(zram, index, ZRAM_IDLE); diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index 4a92c5e186ad..d8eb5802e6c2 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -68,5 +68,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle); unsigned long zs_get_total_pages(struct zs_pool *pool); unsigned long zs_compact(struct zs_pool *pool); +unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size); + void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats); #endif diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 5c21f3f4cb29..f18cf19a92d9 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1209,6 +1209,27 @@ static bool zspage_full(struct size_class *class, struct zspage *zspage) return get_zspage_inuse(zspage) == class->objs_per_zspage; } +/** + * zs_lookup_class_index() - Returns index of the zsmalloc &size_class + * that hold objects of the provided size. + * @pool: zsmalloc pool to use + * @size: object size + * + * Context: Any context. + * + * Return: the index of the zsmalloc &size_class that hold objects of the + * provided size. + */ +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(pool, size)]; + + return class->index; +} +EXPORT_SYMBOL_GPL(zs_lookup_class_index); + unsigned long zs_get_total_pages(struct zs_pool *pool) { return atomic_long_read(&pool->pages_allocated);