From patchwork Wed Nov 9 11:50:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13037455 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 150DCC433FE for ; Wed, 9 Nov 2022 11:51:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8AB646B0074; Wed, 9 Nov 2022 06:51:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85C3F8E0001; Wed, 9 Nov 2022 06:51:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 723F56B0078; Wed, 9 Nov 2022 06:51:42 -0500 (EST) 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 648426B0074 for ; Wed, 9 Nov 2022 06:51:42 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1E3C5C06B3 for ; Wed, 9 Nov 2022 11:51:42 +0000 (UTC) X-FDA: 80113739244.08.09C6C5C Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf03.hostedemail.com (Postfix) with ESMTP id BA5C020012 for ; Wed, 9 Nov 2022 11:51:36 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id m14-20020a17090a3f8e00b00212dab39bcdso1640224pjc.0 for ; Wed, 09 Nov 2022 03:51:36 -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=2STvSSncO0NE+Pc4XM/M24vXge8hgNvkyOrZLLo09CU=; b=Gnj8YxkZmSXisqpa9f9O26JNP3w9Jr92llhfl96tANf3bFA1UUpYeanXW/x1XBpd1T ReKNHsqAsLWVqB7RONpDvI1p9U74iuIlieuE7HOpICpJzJKH1kBktLXfHl103bUf82Lr eDAqyc0zNwuD4OrOAil0SbC8M8KIN6gRnOxnc= 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=2STvSSncO0NE+Pc4XM/M24vXge8hgNvkyOrZLLo09CU=; b=3AmwHBmQoGd+eSfSwSEJnKsqT+HIVRkpOdoiAnl6RwT0FEw57rdQVS7mpFdEp8sedZ UfZqfMA7gSn/bww5TwW+GQUwSESwz0FNn6puqPopYP/SfpJUGQTKX/HsgPoGw1U0r/uU RKLWaGRBJTLBEyQ/ibOSDGZ6XVJ9sLb0R3PrGQ141uNxpFqsyLyGTEh3sc/x2s+ZxHNs +vC4oNSgOxXUd9lqcTYwksMooK5rYhAEE+ifFD+39/eI1qEIdwY/WAanaUdhm+4W9mBa lsmWYwPyNtEQcrvrgx2+bq/RpFEwlO3Bhwv1jPUuiPvW1Nvtb+lnrvB3RZheoUP9z15a L9Ew== X-Gm-Message-State: ACrzQf0TigQIYmG3AwptBO3TCvNONqeKgb9z0URJ18gox2OQEy0+eaYN U2FLShOzxJ05gSLFeW8PWu8Z197mH70YBg== X-Google-Smtp-Source: AMsMyM6xPKvI1rFKl5nOfDTS3HUFzInmPELN0OJL4VTq5t1OcpxDaMsalY0qlRJ2qAgEQis7z+sUuA== X-Received: by 2002:a17:90b:3113:b0:213:82ad:9bc7 with SMTP id gc19-20020a17090b311300b0021382ad9bc7mr71655635pjb.131.1667994659864; Wed, 09 Nov 2022 03:50:59 -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.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 03:50:59 -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 Subject: [PATCHv5 02/13] zram: Add recompression algorithm sysfs knob Date: Wed, 9 Nov 2022 20:50:36 +0900 Message-Id: <20221109115047.2921851-3-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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667994696; a=rsa-sha256; cv=none; b=EwlgdxpR+SrqcKgp+CnQ40SK5brvcWGubvMiuAvTWda9Vb3p0/wjMC7dClBzqaf8ojb2Q4 dzUxD1wzeY+xQ1PFDqjUDE6KouhT2bAfi63TbCESdLTxMq+oRvQy6gqTTLRc7PsGCEN5A7 u69wK3lBWEbS8uhLX+Dz4iRMTrPtS3c= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Gnj8YxkZ; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf03.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.47 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667994696; 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=2STvSSncO0NE+Pc4XM/M24vXge8hgNvkyOrZLLo09CU=; b=VFMo9DNsSm5gyR80jDTSuCbt7rQfcb6l2WNuGA0o+N1oh09vkIPsoMjJb+roCLKPoyzElt eM06Q95INy2F1wW3AZtWXwzIV/Ji4n/OqbWAM21lqyzWb9TqZz1wHzCyBEdr1wWP599ZA9 zL0fvnw1t3OabpGllSUJryFRmz8Fkxk= X-Rspam-User: X-Stat-Signature: xu6bi18p9uok3q6xdpimr9re8orc3k8h X-Rspamd-Queue-Id: BA5C020012 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Gnj8YxkZ; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf03.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.47 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org X-Rspamd-Server: rspam03 X-HE-Tag: 1667994696-999124 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. We will support up to 3 secondary compression algorithms which are sorted in order of their priority. To select an algorithm user has to provide its name and priority: echo "algo=zstd priority=1" > /sys/block/zramX/recomp_algorithm echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm During recompression zram iterates through the list of registered secondary algorithms in order of their priorities. We also have a short version for cases when there is only one secondary compression algorithm: echo "algo=zstd" > /sys/block/zramX/recomp_algorithm This will register zstd as the secondary algorithm with priority 1. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 124 ++++++++++++++++++++++++++++------ 1 file changed, 105 insertions(+), 19 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 5371ed63c785..56026a6deb70 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1000,31 +1000,28 @@ 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 prio, const char *alg) { - size_t sz; - struct zram *zram = dev_to_zram(dev); + /* Do not free statically defined compression algorithms */ + if (zram->comp_algs[prio] != default_compressor) + kfree(zram->comp_algs[prio]); + + zram->comp_algs[prio] = alg; +} + +static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *buf) +{ + ssize_t sz; down_read(&zram->init_lock); - sz = zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_COMP], buf); + sz = zcomp_available_show(zram->comp_algs[prio], buf); up_read(&zram->init_lock); return sz; } -static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg) +static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf) { - /* Do not kfree() algs that we didn't allocate, IOW the default ones */ - if (zram->comp_algs[prio] != default_compressor) - kfree(zram->comp_algs[prio]); - zram->comp_algs[prio] = alg; -} - -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); char *compressor; size_t sz; @@ -1053,11 +1050,94 @@ static ssize_t comp_algorithm_store(struct device *dev, return -EBUSY; } - comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, compressor); + comp_algorithm_set(zram, prio, 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_COMP, 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_COMP, 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); + ssize_t sz = 0; + u32 prio; + + for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) { + if (!zram->comp_algs[prio]) + continue; + + sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, "#%d: ", prio); + sz += __comp_algorithm_show(zram, prio, buf + sz); + } + + return sz; +} + +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 prio = ZRAM_SECONDARY_COMP; + char *args, *param, *val; + char *alg = NULL; + int ret; + + args = skip_spaces(buf); + while (*args) { + args = next_arg(args, ¶m, &val); + + if (!*val) + return -EINVAL; + + if (!strcmp(param, "algo")) { + alg = val; + continue; + } + + if (!strcmp(param, "priority")) { + ret = kstrtoint(val, 10, &prio); + if (ret) + return ret; + continue; + } + } + + if (!alg) + return -EINVAL; + + if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS) + return -EINVAL; + + ret = __comp_algorithm_store(zram, prio, alg); + return ret ? ret : len; +} +#endif + static ssize_t compact_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { @@ -1898,6 +1978,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, @@ -1921,6 +2004,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, }; @@ -2000,7 +2086,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; - zram->comp_algs[ZRAM_PRIMARY_COMP] = default_compressor; + comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); zram_debugfs_register(zram); pr_info("Added device: %s\n", zram->disk->disk_name);