From nobody Mon Sep 15 22:28:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48B1FC433FE for ; Wed, 9 Nov 2022 11:51:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230355AbiKILvI (ORCPT ); Wed, 9 Nov 2022 06:51:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230281AbiKILvB (ORCPT ); Wed, 9 Nov 2022 06:51:01 -0500 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FFAA2CDD3 for ; Wed, 9 Nov 2022 03:51:00 -0800 (PST) Received: by mail-pl1-x634.google.com with SMTP id l2so16831295pld.13 for ; Wed, 09 Nov 2022 03:51:00 -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=QV6BWdO7MKeO8Ww84dIxpPU86fV+Px/mVnSr9bBFmMlJBepXBKhio2JFUUioZ30dGe ABrtGf2QxQI/3dAr8+9StByq+lOeIGIZmNErNJT7RRiDyQCFRwYq1z/E0TO+NmkwLQ+W cf8YvHvrbUmRP9fXhhczbdEZZCqspkxf79cVfXscJJUdKfisqys2jgltj5slH+SJaVTv z+u424S2ADZfHcDmdtvbv9C6jZUwy0WviCAKIMOBPKtDQt5iZ/GgbwLjPeUq6R6Pdr6k WpGFUKt8utfBHPeHZFxcIMBHBnVRkL5fDNZ+YJRKEgkgc/yL1d2BSkNeR7/vDHckhUTF 7gjQ== X-Gm-Message-State: ACrzQf1n05vlxGvYz4m9EJYqAsfuKRdVo7SnTL5OurIqeroBHKZakt8D qbcc56KZMl2i1LZlfp45hEHdcQ== 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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=3Dzstd priority=3D1" > /sys/block/zramX/recomp_algorithm echo "algo=3Ddeflate priority=3D2" > /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=3Dzstd" > /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; } =20 -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 *al= g) { - size_t sz; - struct zram *zram =3D dev_to_zram(dev); + /* Do not free statically defined compression algorithms */ + if (zram->comp_algs[prio] !=3D default_compressor) + kfree(zram->comp_algs[prio]); + + zram->comp_algs[prio] =3D alg; +} + +static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *bu= f) +{ + ssize_t sz; =20 down_read(&zram->init_lock); - sz =3D zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_COMP], buf); + sz =3D zcomp_available_show(zram->comp_algs[prio], buf); up_read(&zram->init_lock); =20 return sz; } =20 -static void comp_algorithm_set(struct zram *zram, u32 prio, const char *al= g) +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] !=3D default_compressor) - kfree(zram->comp_algs[prio]); - zram->comp_algs[prio] =3D alg; -} - -static ssize_t comp_algorithm_store(struct device *dev, - struct device_attribute *attr, const char *buf, size_t len) -{ - struct zram *zram =3D dev_to_zram(dev); char *compressor; size_t sz; =20 @@ -1053,11 +1050,94 @@ static ssize_t comp_algorithm_store(struct device *= dev, return -EBUSY; } =20 - 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 =3D 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 =3D dev_to_zram(dev); + int ret; + + ret =3D __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf); + return ret ? ret : len; } =20 +#ifdef CONFIG_ZRAM_MULTI_COMP +static ssize_t recomp_algorithm_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct zram *zram =3D dev_to_zram(dev); + ssize_t sz =3D 0; + u32 prio; + + for (prio =3D ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) { + if (!zram->comp_algs[prio]) + continue; + + sz +=3D scnprintf(buf + sz, PAGE_SIZE - sz - 2, "#%d: ", prio); + sz +=3D __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 =3D dev_to_zram(dev); + int prio =3D ZRAM_SECONDARY_COMP; + char *args, *param, *val; + char *alg =3D NULL; + int ret; + + args =3D skip_spaces(buf); + while (*args) { + args =3D next_arg(args, ¶m, &val); + + if (!*val) + return -EINVAL; + + if (!strcmp(param, "algo")) { + alg =3D val; + continue; + } + + if (!strcmp(param, "priority")) { + ret =3D kstrtoint(val, 10, &prio); + if (ret) + return ret; + continue; + } + } + + if (!alg) + return -EINVAL; + + if (prio < ZRAM_SECONDARY_COMP || prio >=3D ZRAM_MAX_COMPS) + return -EINVAL; + + ret =3D __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 =20 static struct attribute *zram_disk_attrs[] =3D { &dev_attr_disksize.attr, @@ -1921,6 +2004,9 @@ static struct attribute *zram_disk_attrs[] =3D { &dev_attr_bd_stat.attr, #endif &dev_attr_debug_stat.attr, +#ifdef CONFIG_ZRAM_MULTI_COMP + &dev_attr_recomp_algorithm.attr, +#endif NULL, }; =20 @@ -2000,7 +2086,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; =20 - zram->comp_algs[ZRAM_PRIMARY_COMP] =3D default_compressor; + comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); =20 zram_debugfs_register(zram); pr_info("Added device: %s\n", zram->disk->disk_name); --=20 2.38.1.431.g37b22c650d-goog