From patchwork Mon Sep 5 08:23:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12965769 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 39567C6FA89 for ; Mon, 5 Sep 2022 08:23:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C8A19801C5; Mon, 5 Sep 2022 04:23:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C39E48D0050; Mon, 5 Sep 2022 04:23:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADA13801C5; Mon, 5 Sep 2022 04:23:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9F8CB8D0050 for ; Mon, 5 Sep 2022 04:23:36 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 645E8121881 for ; Mon, 5 Sep 2022 08:23:36 +0000 (UTC) X-FDA: 79877342832.29.B47D912 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf23.hostedemail.com (Postfix) with ESMTP id 17FFD140077 for ; Mon, 5 Sep 2022 08:23:35 +0000 (UTC) Received: by mail-pj1-f49.google.com with SMTP id x1-20020a17090ab00100b001fda21bbc90so11477572pjq.3 for ; Mon, 05 Sep 2022 01:23:35 -0700 (PDT) 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; bh=osBqiJaYCcdSOMrgUz97U3jA0MdVQ300bRjV6yXUVIc=; b=Dj8l0GVOcH4FsUVnxL4tY09OuUF7plVKGT36GD2zseXqHCVAoWzqC2wyYGRnwKkNVu iBgy2FIh5hALHQRL0BZwivTaMCDPYseK+lZDWWS5Jzyy5wgDArZezc3vpw08Z1BCcI+N 9D4avvixlHeN3+YHVLR5hRRZkQnMwy742gYmU= 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; bh=osBqiJaYCcdSOMrgUz97U3jA0MdVQ300bRjV6yXUVIc=; b=vwLRuB2j4p8pNYJ7wTQ9BE2/EnAgvtfZ1cMwX5G9Yk1hbZSP8mTjmdXpkSmD0uHjgx WzmLtRDEgPMvWeTrwsaeVxyZYw79M2Jfs70VJw2CamLhKQ1CrGUNDo2UeIZPX6kcGuWO gvKgsnGEVCV595+wIWVJe6bpaawL2ugbqPZCqa87O89ByqMrnuSlkHeAvXa26i/VoMKG 3WSAkQyQGS4N5tzqXkq2N73LL4wT/pELjjPz9eSzEzMRbi6p3evyoQfacLczXEEWhcAg O+xzA31bcCzCQOn+Uw+yUoF1EM/ESOdo2CFGJOr1D6Vu/XQ3mwjsrdvgee2zydQwvR31 DP5w== X-Gm-Message-State: ACgBeo2aQzVdz+wnwPr1rci0xHS7mvjl/qGlonQ2oOEzfrGiCZLG5U0O 3CygVKqGDAUvV/WTuII8a2tsrw== X-Google-Smtp-Source: AA6agR7g8dFTB0VArIVTxGYq1Z4Rv7IOKVS5+1nZdI6tf1siXXlu2O6JpvShJE42fy847+SkNmM3Zw== X-Received: by 2002:a17:90b:4f45:b0:1f5:6554:d555 with SMTP id pj5-20020a17090b4f4500b001f56554d555mr18729225pjb.226.1662366215186; Mon, 05 Sep 2022 01:23:35 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:71e2:8444:42d9:4bb0]) by smtp.gmail.com with ESMTPSA id p14-20020a170902e74e00b001641b2d61d4sm6807093plf.30.2022.09.05.01.23.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 01:23:34 -0700 (PDT) From: Sergey Senozhatsky To: Minchan Kim , Andrew Morton Cc: Nitin Gupta , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Sergey Senozhatsky Subject: [PATCH RFC 2/7] zram: Add recompression algorithm sysfs knob Date: Mon, 5 Sep 2022 17:23:18 +0900 Message-Id: <20220905082323.2742399-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220905082323.2742399-1-senozhatsky@chromium.org> References: <20220905082323.2742399-1-senozhatsky@chromium.org> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Dj8l0GVO; spf=pass (imf23.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.49 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=1662366216; a=rsa-sha256; cv=none; b=nU4ar7SOuO4W7mLKexYrPiqhfWcEIudhzOFbnXTmotK5urN7MQppxKMJWdKq3mK75SSJ8G YQYm9CoPGWzBQHjxpwnq+S8DJfUWAR00Jwgc4iwwz4OMQW59CgyfEf0SRhyqr0EZhQxTPF /V23xCCrFtr9HLtvFQ/2coyCkQmEH7Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662366216; 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=osBqiJaYCcdSOMrgUz97U3jA0MdVQ300bRjV6yXUVIc=; b=limL1E+YpCP389+GI+umNxJF2h9Uu9SF088iP1ny7qr79YXrw5ZX14hkKvg4LH74khwDfo J4Uj/LM5lznywHeuvMZZdoNbMn4W9Tls9xhl4lkkyZAMv94FK9PYpSfVx95Wv1LbKCm1Eq bCpvBn/BhXslDlAkPAfi9DtEUwEn9HE= X-Rspamd-Server: rspam02 X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Dj8l0GVO; spf=pass (imf23.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.49 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Stat-Signature: qkfmoi56k7y69f6wueo8h9qabpxycij6 X-Rspamd-Queue-Id: 17FFD140077 X-HE-Tag: 1662366215-556701 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: Introduce recomp_algorithm sysfs knob that controls secondary algorithm selection used for recompression. This device attribute works in a similar way with comp_algorithm attribute. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 111 +++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 21 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 4ad1daa1283e..694c8c426cb2 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -41,7 +41,12 @@ static DEFINE_IDR(zram_index_idr); static DEFINE_MUTEX(zram_index_mutex); static int zram_major; -static const char *default_compressor = CONFIG_ZRAM_DEF_COMP; +static const char *default_comp_algs[ZRAM_MAX_ZCOMPS] = { + CONFIG_ZRAM_DEF_COMP, +#ifdef CONFIG_ZRAM_MULTI_COMP + "zstd", +#endif +}; /* Module params (documentation at end) */ static unsigned int num_devices = 1; @@ -1011,31 +1016,37 @@ static ssize_t max_comp_streams_store(struct device *dev, return len; } -static ssize_t comp_algorithm_show(struct device *dev, - struct device_attribute *attr, char *buf) +static void comp_algorithm_set(struct zram *zram, u32 idx, const char *alg) { - size_t sz; - struct zram *zram = dev_to_zram(dev); + bool default_alg = false; + int i; - down_read(&zram->init_lock); - sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_ZCOMP], buf); - up_read(&zram->init_lock); + /* Do not kfree() algs that we didn't allocate, IOW the default ones */ + for (i = 0; i < ZRAM_MAX_ZCOMPS; i++) { + if (zram->comp_algs[idx] == default_comp_algs[i]) { + default_alg = true; + break; + } + } - return sz; + if (!default_alg) + kfree(zram->comp_algs[idx]); + zram->comp_algs[idx] = alg; } -static void comp_algorithm_set(struct zram *zram, u32 idx, const char *alg) +static ssize_t __comp_algorithm_show(struct zram *zram, u32 idx, char *buf) { - /* Do not kfree() algs that we didn't allocate, IOW the default ones */ - if (zram->comp_algs[idx] != default_compressor) - kfree(zram->comp_algs[idx]); - zram->comp_algs[idx] = alg; + ssize_t sz; + + down_read(&zram->init_lock); + sz = zcomp_available_show(zram->comp_algs[idx], buf); + up_read(&zram->init_lock); + + return sz; } -static ssize_t comp_algorithm_store(struct device *dev, - struct device_attribute *attr, const char *buf, size_t len) +static int __comp_algorithm_store(struct zram *zram, u32 idx, const char *buf) { - struct zram *zram = dev_to_zram(dev); char *compressor; size_t sz; @@ -1064,11 +1075,55 @@ static ssize_t comp_algorithm_store(struct device *dev, return -EBUSY; } - comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, compressor); + comp_algorithm_set(zram, idx, compressor); up_write(&zram->init_lock); - return len; + return 0; +} + +static ssize_t comp_algorithm_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct zram *zram = dev_to_zram(dev); + + return __comp_algorithm_show(zram, ZRAM_PRIMARY_ZCOMP, buf); +} + +static ssize_t comp_algorithm_store(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct zram *zram = dev_to_zram(dev); + int ret; + + ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_ZCOMP, buf); + return ret ? ret : len; } +#ifdef CONFIG_ZRAM_MULTI_COMP +static ssize_t recomp_algorithm_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct zram *zram = dev_to_zram(dev); + + return __comp_algorithm_show(zram, ZRAM_SECONDARY_ZCOMP, buf); +} + +static ssize_t recomp_algorithm_store(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct zram *zram = dev_to_zram(dev); + int ret; + + ret = __comp_algorithm_store(zram, ZRAM_SECONDARY_ZCOMP, buf); + return ret ? ret : len; +} +#endif + static ssize_t compact_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { @@ -1770,7 +1825,11 @@ static void zram_reset_device(struct zram *zram) memset(&zram->stats, 0, sizeof(zram->stats)); reset_bdev(zram); - comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, default_compressor); + comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, + default_comp_algs[ZRAM_PRIMARY_ZCOMP]); + if (IS_ENABLED(CONFIG_ZRAM_MULTI_COMP)) + comp_algorithm_set(zram, ZRAM_SECONDARY_ZCOMP, + default_comp_algs[ZRAM_SECONDARY_ZCOMP]); up_write(&zram->init_lock); } @@ -1912,6 +1971,9 @@ static DEVICE_ATTR_WO(writeback); static DEVICE_ATTR_RW(writeback_limit); static DEVICE_ATTR_RW(writeback_limit_enable); #endif +#ifdef CONFIG_ZRAM_MULTI_COMP +static DEVICE_ATTR_RW(recomp_algorithm); +#endif static struct attribute *zram_disk_attrs[] = { &dev_attr_disksize.attr, @@ -1935,6 +1997,9 @@ static struct attribute *zram_disk_attrs[] = { &dev_attr_bd_stat.attr, #endif &dev_attr_debug_stat.attr, +#ifdef CONFIG_ZRAM_MULTI_COMP + &dev_attr_recomp_algorithm.attr, +#endif NULL, }; @@ -2014,7 +2079,11 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; - zram->comp_algs[ZRAM_PRIMARY_ZCOMP] = default_compressor; + zram->comp_algs[ZRAM_PRIMARY_ZCOMP] = + default_comp_algs[ZRAM_PRIMARY_ZCOMP]; + if (IS_ENABLED(CONFIG_ZRAM_MULTI_COMP)) + zram->comp_algs[ZRAM_SECONDARY_ZCOMP] = + default_comp_algs[ZRAM_SECONDARY_ZCOMP]; zram_debugfs_register(zram); pr_info("Added device: %s\n", zram->disk->disk_name);