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: 10885 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1103021wru; Tue, 25 Oct 2022 09:36:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7vZK5UvnJdlmp+17uDLsX0CDbr6xwUXrl1PtjxB5/WuVSUIMzpyDFN6etl5WNL0USnnWhs X-Received: by 2002:a17:907:7244:b0:78d:cedc:7a9e with SMTP id ds4-20020a170907724400b0078dcedc7a9emr32780884ejc.600.1666715760989; Tue, 25 Oct 2022 09:36:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666715760; cv=none; d=google.com; s=arc-20160816; b=AHI+YDB3yNw4K98SNPOHR89QwoRo6qA4eFUyTvORcVJ+1yRBxU7SpJd7+bmILDDu9f 5ZW9Q9vPjPvSrA9TCsoo3dJwqPaANI58Wc3s8jBbPf8KBVNcszgYIh4LMm9EDNwGoaZM LX1MikuiOrZXAzWaVJomL7hsOdvmP+B8t4eqt1lMaB5SZTlk9Vyhn0Qn1ZlZBUgRNkW0 +t+GSFahawnXpLpKBKjAI6d9dGG1nDKTJdxJzr9Ds1zzJuJ5YTK1fTLp/UEeB1a75T58 qrU2CawF9DYrukaUd8X5W96C2ZfYGf7K5WVDu9U/gDvLe/YYUAbf0f3o/2n6K0r27Lni if3g== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw22-20020a170906479600b0078d3b4510b5si3876876ejc.854.2022.10.25.09.35.35; Tue, 25 Oct 2022 09:36:00 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747611168332273596?= X-GMAIL-MSGID: =?utf-8?q?1747678145519324106?= 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);