From patchwork Wed Nov 9 11:50:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13037456 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 3CC5BC43219 for ; Wed, 9 Nov 2022 11:51:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4194B8E0002; Wed, 9 Nov 2022 06:51:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CA1B6B0078; Wed, 9 Nov 2022 06:51:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21CBF8E0002; Wed, 9 Nov 2022 06:51:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 121A56B0075 for ; Wed, 9 Nov 2022 06:51:44 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D3D8940655 for ; Wed, 9 Nov 2022 11:51:38 +0000 (UTC) X-FDA: 80113739076.29.492F71C Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf28.hostedemail.com (Postfix) with ESMTP id 5828CC0020 for ; Wed, 9 Nov 2022 11:51:30 +0000 (UTC) Received: by mail-pg1-f182.google.com with SMTP id h193so15991530pgc.10 for ; Wed, 09 Nov 2022 03:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ISzeUQbltZw2zOA4vUeGiMYh+5K7aaZl+AlK2yoDkbI=; b=abhQ1Lg35Ucp+NagZfYKvx4GbY0NqG+/hyXy+da3FyfoeakVv8aDDXACjC5rRaYMlE E3LixsEmNHw/j0vp8f/7HSXIgTj/qjh5U/zqtfM2h03rzuq8Scn+ixTQLqjY4CV5PBYE 62QyHJ431/vUwvGW0FiMJTSIr0gK1BLKAlU3Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ISzeUQbltZw2zOA4vUeGiMYh+5K7aaZl+AlK2yoDkbI=; b=12zQdiNvr6AkqxOaz8fKEGiaHuJfZgy47G6hbIHyhO3qM+Vt2F/IVquoiK16gt/IRo ImoGi7Ni1/J2Imi5GTrcr+D9zFLE6V29U1loRm555VZjPmQ7I5yJNZmHLDyWU7ZbB2uW wTXvCIQsc9LPDKFQNCAhBWYTibOzLxoqRcCVWS1H7TdOzBUE7TRUe7pXE7e83w0f5nj9 bzdzlqjxIRVEoAM7TuBcAMvvEUV/NiII2EhOETIKngY4yJyIGiiNPXkztoLG3yR/maqL GbLAnX0R09rGvWU7ILsLV0h8qMOmhrEOMrGAfUZsyfRUA+G8w7X7DKzchjyOS/V+ChCA S7Ow== X-Gm-Message-State: ACrzQf0/GwmRbz4MyLlNOI6qc1l78vB+DNd9tW9Fex5tP6LtQn7mXj4B L/q2UoafT9x8TEb0Vz3ZmLsXOA== X-Google-Smtp-Source: AMsMyM5bs/os1qCUXnfE9uDJ3/tdq0rmqwrB+hDi9koev9Tj6IMDU384APFO34LPFle2GUW6mVXbfw== X-Received: by 2002:a63:f047:0:b0:46e:b1d4:2a68 with SMTP id s7-20020a63f047000000b0046eb1d42a68mr51073818pgj.496.1667994675340; Wed, 09 Nov 2022 03:51:15 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:61f1:44d3:3f47:2cdb]) by smtp.gmail.com with ESMTPSA id f5-20020a655505000000b00434760ee36asm7292311pgr.16.2022.11.09.03.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 03:51:15 -0800 (PST) From: Sergey Senozhatsky To: Minchan Kim , Andrew Morton Cc: Nitin Gupta , Suleiman Souhlal , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Sergey Senozhatsky , Alexey Romanov Subject: [PATCHv5 08/13] zram: add size class equals check into recompression Date: Wed, 9 Nov 2022 20:50:42 +0900 Message-Id: <20221109115047.2921851-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog In-Reply-To: <20221109115047.2921851-1-senozhatsky@chromium.org> References: <20221109115047.2921851-1-senozhatsky@chromium.org> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667994693; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ISzeUQbltZw2zOA4vUeGiMYh+5K7aaZl+AlK2yoDkbI=; b=BOqWmkP9cOLZIATUgdP2jQm9DI/Jzxz64TRET+/mM7lhuQcUiFd6YqaMQKX2cJIGhULpZo Y7vHbM7g2YJRd1Lg2Pi6l8fBii6rE8SrzbZiCcDenypD6ztQAujh2UdlM+1vBjbj8WZXS3 cR/tk2+sPEuKSl+kyVtDLqQwwqYACqY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=abhQ1Lg3; spf=pass (imf28.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.182 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667994693; a=rsa-sha256; cv=none; b=4wZQTTbT51T5v+lf5hZiPmRogwMS4l0AjxFDffTR4yyQBa4bqjiFKI6mYJzYF+xxlXAIRU 5uRx01wFk/JEbsbqNJJIfOdaxO2qzntg0dMsUCe329rz431oSlb9nUjsLF4SchSszv7llh wET1UlRqKa8KAFfIaTOSRi78phmwtRs= X-Rspamd-Queue-Id: 5828CC0020 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=abhQ1Lg3; spf=pass (imf28.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.182 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Rspamd-Server: rspam10 X-Rspam-User: X-Stat-Signature: n7ag6jde8tnackj7rtc68skaihchqxyf X-HE-Tag: 1667994690-225263 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: From: Alexey Romanov 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. [senozhatsky: rebased and fixed conflicts] Signed-off-by: Alexey Romanov Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 11 ++++++++++- include/linux/zsmalloc.h | 2 ++ mm/zsmalloc.c | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 9561569273fe..383d967ef4c7 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1675,6 +1675,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, unsigned long handle_new; unsigned int comp_len_old; unsigned int comp_len_new; + unsigned int class_index_old; + unsigned int class_index_new; void *src, *dst; int ret; @@ -1693,6 +1695,7 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, if (ret) return ret; + class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old); /* * Iterate the secondary comp algorithms list (in order of priority) * and try to recompress the page. @@ -1718,9 +1721,13 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, return ret; } + class_index_new = zs_lookup_class_index(zram->mem_pool, + comp_len_new); + /* Continue until we make progress */ if (comp_len_new >= huge_class_size || comp_len_new >= comp_len_old || + class_index_new >= class_index_old || (threshold && comp_len_new >= threshold)) { zcomp_stream_put(zram->comps[prio]); continue; @@ -1743,7 +1750,9 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, * that would save memory, mark the object as incompressible so that * we will not try to compress it again. */ - if (comp_len_new >= huge_class_size || comp_len_new >= comp_len_old) { + if (comp_len_new >= huge_class_size || + comp_len_new >= comp_len_old || + class_index_new >= class_index_old) { zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE); return 0; } diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index 2a430e713ce5..a48cd0ffe57d 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -55,5 +55,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 d03941cace2c..065744b7e9d8 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1205,6 +1205,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(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);