Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp8735470rwi; Tue, 25 Oct 2022 10:08:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7wnJyZzgEz9wqyulR3Pjy9j7NUUl52kI+3gZru+av/zUXFsvEepWvoZet/Y9JlWzLX7g9+ X-Received: by 2002:a17:907:2cd9:b0:78d:9f4c:9cff with SMTP id hg25-20020a1709072cd900b0078d9f4c9cffmr33190254ejc.345.1666717711245; Tue, 25 Oct 2022 10:08:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666717711; cv=none; d=google.com; s=arc-20160816; b=xnpioyQyTnqSrnlIXhpPmVrrixSLlnzPM0msDgLa9NW/uIBYNmhZObB79maO/R3uLM YSlHYoleTr/wFfQNIEueZAahVbzqXlNW+aifZDgNEJJz5riQG6tGBQZJuO4J5aiNyovL g29f3EkgzQVBYVQrO/Nfh8W6t3U5KO1537LyD4iP7PIDzUgvHDBQ89NIzf368VJUppyP G2Ksc3mHNrYwurNbSnFEIExJ5w4erE2xgwSeuBoprZVSXsdnDDL/i8HE0uGLvbzi7JaT XRe9CHwF2GshafTLjw8oncEuLiasc/k/q1E6QkcX2ozDSFglVCq77QEfLtW8+bfuizPH mk+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=zkSUQe1qaY4QG+yHFWp5XX2esm9RY0bLyeMYknhjNYE=; b=jRd1Eyto7g156p0FlfQCNppDArMsj2AmHsKzZoktYKDb95vSKjEKMmQodBvbWlWich KvGg7yoNhkaa3JptUtDClCt/XDlRWNWZdeVf5O1YXuM1BnthtPKt/bqtdG+WTTs1OdPu Bhyexo6fAoWlFYZ83SZ+K8QR+gjFzmZdHyFavcikOrWLmXULPGDJpsfFcmbDjdExDWGC xh7+9m36dH9KWoem4kkcDnZktbjAYshB2NEeaqoBj9vF+Z9Rc0BkpgpF10Gtl5PKT/RM T14Na/T5U3Jt0792CrgEHYuqmCZCDtFOs6MG6OqIftumEMKFaq+bsW0Uw0zQ7ccHRjhn cPsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=B8Wajy+C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w22-20020a056402269600b0045cc36d6d8fsi2655134edd.336.2022.10.25.10.08.05; Tue, 25 Oct 2022 10:08:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=B8Wajy+C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230499AbiJYQ1C (ORCPT + 99 others); Tue, 25 Oct 2022 12:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231853AbiJYQ06 (ORCPT ); Tue, 25 Oct 2022 12:26:58 -0400 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0D7A170B6B for ; Tue, 25 Oct 2022 09:26:51 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.25.1