From nobody Tue Apr 7 00:28:03 2026 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 E712BC433F5 for ; Sun, 9 Oct 2022 09:07:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230029AbiJIJHe (ORCPT ); Sun, 9 Oct 2022 05:07:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbiJIJHa (ORCPT ); Sun, 9 Oct 2022 05:07:30 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 909301CFC1 for ; Sun, 9 Oct 2022 02:07:29 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id h13so7007349pfr.7 for ; Sun, 09 Oct 2022 02:07:29 -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 :message-id:reply-to; bh=RN0/9ftrQEX77iPPbChQJHYVjybhF/OgEdl9wxmnkf4=; b=JcG2gmo5pwbfQLQPoKGWMTnTpl9CgkQm68UB33vFvfR7qUVhLuqLtjAhCEI2NIIup2 eFxWjDtN9iIRrqh7+cEwliC0/H/hPH/9hzWjUD2M8nNY8yI9K6i5Fj0Vu4w/96cS4d4Z ZuXMW9EqXdjazBY+itAc8OQrOxJfvMSgiMpc8= 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=RN0/9ftrQEX77iPPbChQJHYVjybhF/OgEdl9wxmnkf4=; b=KAPAHEvygFw/5fMS2NlKNaFhAQWZmsuER3I1xW14fd5x7pK7d94oL7Wht+meZ7W/CD fWqy0LDMyw8UvRyTlngX5+5zkicYnJzvGc8zlFVOnqDTCGfURES0/+QaOeSo9I1Ko7jl QA0ooVWrRB9UZlBucdtX6QawPeJael0LvDySzLFsXxKkiGWtk0Hlp9pXUBgTWUY/JLfI WOsdNe4UjmZHtMILKqa7iNUZVD0T6+OrBC4mQQrDydBL2QWIZ1EJthJqrvWsfT++vEWe 8XMT43N9E+E3hHcXo9csKPKVabPx5+BN1phoTJv48X+yvnQTcRdPIarR05USRAqE7dl0 jAaw== X-Gm-Message-State: ACrzQf2Zn+4oYMamSwNwbv0Kq12KvOkgghwyaV/+HjqHgc+1kptynGj4 mEl7tjUzG2jmAurehX1Qo5Kuwg== X-Google-Smtp-Source: AMsMyM7JrIj1aW/9GlPBhc2nLzfzKzg2w767zaqTiIHAkqgWrsX62xV1nGN/ulotXfisKwM2KTi0Ug== X-Received: by 2002:a63:1e56:0:b0:462:970:e0de with SMTP id p22-20020a631e56000000b004620970e0demr721488pgm.90.1665306448995; Sun, 09 Oct 2022 02:07:28 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:28 -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: [PATCHv3 1/8] zram: Preparation for multi-zcomp support Date: Sun, 9 Oct 2022 18:07:13 +0900 Message-Id: <20221009090720.1040633-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" The patch turns compression streams and compressor algorithm name struct zram members into arrays, so that we can have multiple compression streams support (in the next patches). The patch uses a rather explicit API for compressor selection: - Get primary (default) compression stream zcomp_stream_get(zram->comps[ZRAM_PRIMARY_ZCOMP]) - Get secondary compression stream zcomp_stream_get(zram->comps[ZRAM_SECONDARY_ZCOMP]) We use similar API for compression streams put(). At this point we always have just one compression stream, since CONFIG_ZRAM_MULTI_COMP is not yet defined. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zcomp.c | 6 +-- drivers/block/zram/zcomp.h | 2 +- drivers/block/zram/zram_drv.c | 87 ++++++++++++++++++++++++----------- drivers/block/zram/zram_drv.h | 14 +++++- 4 files changed, 77 insertions(+), 32 deletions(-) diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 0916de952e09..55af4efd7983 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -206,7 +206,7 @@ void zcomp_destroy(struct zcomp *comp) * case of allocation error, or any other error potentially * returned by zcomp_init(). */ -struct zcomp *zcomp_create(const char *compress) +struct zcomp *zcomp_create(const char *alg) { struct zcomp *comp; int error; @@ -216,14 +216,14 @@ struct zcomp *zcomp_create(const char *compress) * is not loaded yet. We must do it here, otherwise we are about to * call /sbin/modprobe under CPU hot-plug lock. */ - if (!zcomp_available_algorithm(compress)) + if (!zcomp_available_algorithm(alg)) return ERR_PTR(-EINVAL); =20 comp =3D kzalloc(sizeof(struct zcomp), GFP_KERNEL); if (!comp) return ERR_PTR(-ENOMEM); =20 - comp->name =3D compress; + comp->name =3D alg; error =3D zcomp_init(comp); if (error) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 40f6420f4b2e..cdefdef93da8 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -27,7 +27,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *n= ode); ssize_t zcomp_available_show(const char *comp, char *buf); bool zcomp_available_algorithm(const char *comp); =20 -struct zcomp *zcomp_create(const char *comp); +struct zcomp *zcomp_create(const char *alg); void zcomp_destroy(struct zcomp *comp); =20 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp); diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 966aab902d19..770ea3489eb6 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1007,36 +1007,53 @@ static ssize_t comp_algorithm_show(struct device *d= ev, struct zram *zram =3D dev_to_zram(dev); =20 down_read(&zram->init_lock); - sz =3D zcomp_available_show(zram->compressor, buf); + sz =3D zcomp_available_show(zram->comp_algs[ZRAM_PRIMARY_ZCOMP], buf); up_read(&zram->init_lock); =20 return sz; } =20 +static void comp_algorithm_set(struct zram *zram, u32 idx, const char *alg) +{ + /* Do not kfree() algs that we didn't allocate, IOW the default ones */ + if (zram->comp_algs[idx] !=3D default_compressor) + kfree(zram->comp_algs[idx]); + zram->comp_algs[idx] =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[ARRAY_SIZE(zram->compressor)]; + char *compressor; size_t sz; =20 - strscpy(compressor, buf, sizeof(compressor)); + sz =3D strlen(buf); + if (sz >=3D CRYPTO_MAX_ALG_NAME) + return -E2BIG; + + compressor =3D kstrdup(buf, GFP_KERNEL); + if (!compressor) + return -ENOMEM; + /* ignore trailing newline */ - sz =3D strlen(compressor); if (sz > 0 && compressor[sz - 1] =3D=3D '\n') compressor[sz - 1] =3D 0x00; =20 - if (!zcomp_available_algorithm(compressor)) + if (!zcomp_available_algorithm(compressor)) { + kfree(compressor); return -EINVAL; + } =20 down_write(&zram->init_lock); if (init_done(zram)) { up_write(&zram->init_lock); + kfree(compressor); pr_info("Can't change algorithm for initialized device\n"); return -EBUSY; } =20 - strcpy(zram->compressor, compressor); + comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, compressor); up_write(&zram->init_lock); return len; } @@ -1284,7 +1301,7 @@ static int __zram_bvec_read(struct zram *zram, struct= page *page, u32 index, size =3D zram_get_obj_size(zram, index); =20 if (size !=3D PAGE_SIZE) - zstrm =3D zcomp_stream_get(zram->comp); + zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_ZCOMP]); =20 src =3D zs_map_object(zram->mem_pool, handle, ZS_MM_RO); if (size =3D=3D PAGE_SIZE) { @@ -1296,7 +1313,7 @@ static int __zram_bvec_read(struct zram *zram, struct= page *page, u32 index, dst =3D kmap_atomic(page); ret =3D zcomp_decompress(zstrm, src, size, dst); kunmap_atomic(dst); - zcomp_stream_put(zram->comp); + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); } zs_unmap_object(zram->mem_pool, handle); zram_slot_unlock(zram, index); @@ -1363,13 +1380,13 @@ static int __zram_bvec_write(struct zram *zram, str= uct bio_vec *bvec, kunmap_atomic(mem); =20 compress_again: - zstrm =3D zcomp_stream_get(zram->comp); + zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_ZCOMP]); src =3D kmap_atomic(page); ret =3D zcomp_compress(zstrm, src, &comp_len); kunmap_atomic(src); =20 if (unlikely(ret)) { - zcomp_stream_put(zram->comp); + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); pr_err("Compression failed! err=3D%d\n", ret); zs_free(zram->mem_pool, handle); return ret; @@ -1397,7 +1414,7 @@ static int __zram_bvec_write(struct zram *zram, struc= t bio_vec *bvec, __GFP_HIGHMEM | __GFP_MOVABLE); if (IS_ERR((void *)handle)) { - zcomp_stream_put(zram->comp); + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); atomic64_inc(&zram->stats.writestall); handle =3D zs_malloc(zram->mem_pool, comp_len, GFP_NOIO | __GFP_HIGHMEM | @@ -1414,14 +1431,14 @@ static int __zram_bvec_write(struct zram *zram, str= uct bio_vec *bvec, * zstrm buffer back. It is necessary that the dereferencing * of the zstrm variable below occurs correctly. */ - zstrm =3D zcomp_stream_get(zram->comp); + zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_ZCOMP]); } =20 alloced_pages =3D zs_get_total_pages(zram->mem_pool); update_used_max(zram, alloced_pages); =20 if (zram->limit_pages && alloced_pages > zram->limit_pages) { - zcomp_stream_put(zram->comp); + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); zs_free(zram->mem_pool, handle); return -ENOMEM; } @@ -1435,7 +1452,7 @@ static int __zram_bvec_write(struct zram *zram, struc= t bio_vec *bvec, if (comp_len =3D=3D PAGE_SIZE) kunmap_atomic(src); =20 - zcomp_stream_put(zram->comp); + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); zs_unmap_object(zram->mem_pool, handle); atomic64_add(comp_len, &zram->stats.compr_data_size); out: @@ -1710,6 +1727,20 @@ static int zram_rw_page(struct block_device *bdev, s= ector_t sector, return ret; } =20 +static void zram_destroy_comps(struct zram *zram) +{ + u32 idx; + + for (idx =3D 0; idx < ZRAM_MAX_ZCOMPS; idx++) { + struct zcomp *comp =3D zram->comps[idx]; + + zram->comps[idx] =3D NULL; + if (IS_ERR_OR_NULL(comp)) + continue; + zcomp_destroy(comp); + } +} + static void zram_reset_device(struct zram *zram) { down_write(&zram->init_lock); @@ -1727,11 +1758,11 @@ static void zram_reset_device(struct zram *zram) /* I/O operation under all of CPU are done so let's free */ zram_meta_free(zram, zram->disksize); zram->disksize =3D 0; + zram_destroy_comps(zram); memset(&zram->stats, 0, sizeof(zram->stats)); - zcomp_destroy(zram->comp); - zram->comp =3D NULL; reset_bdev(zram); =20 + comp_algorithm_set(zram, ZRAM_PRIMARY_ZCOMP, default_compressor); up_write(&zram->init_lock); } =20 @@ -1742,6 +1773,7 @@ static ssize_t disksize_store(struct device *dev, struct zcomp *comp; struct zram *zram =3D dev_to_zram(dev); int err; + u32 idx; =20 disksize =3D memparse(buf, NULL); if (!disksize) @@ -1760,22 +1792,25 @@ static ssize_t disksize_store(struct device *dev, goto out_unlock; } =20 - comp =3D zcomp_create(zram->compressor); - if (IS_ERR(comp)) { - pr_err("Cannot initialise %s compressing backend\n", - zram->compressor); - err =3D PTR_ERR(comp); - goto out_free_meta; - } + for (idx =3D 0; idx < ZRAM_MAX_ZCOMPS; idx++) { + comp =3D zcomp_create(zram->comp_algs[idx]); + if (IS_ERR(comp)) { + pr_err("Cannot initialise %s compressing backend\n", + zram->comp_algs[idx]); + err =3D PTR_ERR(comp); + goto out_free_comps; + } =20 - zram->comp =3D comp; + zram->comps[idx] =3D comp; + } zram->disksize =3D disksize; set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT); up_write(&zram->init_lock); =20 return len; =20 -out_free_meta: +out_free_comps: + zram_destroy_comps(zram); zram_meta_free(zram, disksize); out_unlock: up_write(&zram->init_lock); @@ -1962,7 +1997,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; =20 - strscpy(zram->compressor, default_compressor, sizeof(zram->compressor)); + zram->comp_algs[ZRAM_PRIMARY_ZCOMP] =3D default_compressor; =20 zram_debugfs_register(zram); pr_info("Added device: %s\n", zram->disk->disk_name); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index a2bda53020fd..4044ddbb2326 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -89,10 +89,20 @@ struct zram_stats { #endif }; =20 +#ifdef CONFIG_ZRAM_MULTI_COMP +#define ZRAM_PRIMARY_ZCOMP 0 +#define ZRAM_SECONDARY_ZCOMP 1 +#define ZRAM_MAX_ZCOMPS 2 +#else +#define ZRAM_PRIMARY_ZCOMP 0 +#define ZRAM_SECONDARY_ZCOMP 0 +#define ZRAM_MAX_ZCOMPS 1 +#endif + struct zram { struct zram_table_entry *table; struct zs_pool *mem_pool; - struct zcomp *comp; + struct zcomp *comps[ZRAM_MAX_ZCOMPS]; struct gendisk *disk; /* Prevent concurrent execution of device init */ struct rw_semaphore init_lock; @@ -107,7 +117,7 @@ struct zram { * we can store in a disk. */ u64 disksize; /* bytes */ - char compressor[CRYPTO_MAX_ALG_NAME]; + const char *comp_algs[ZRAM_MAX_ZCOMPS]; /* * zram is claimed so open request will be failed */ --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 D589EC4332F for ; Sun, 9 Oct 2022 09:07:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229989AbiJIJHl (ORCPT ); Sun, 9 Oct 2022 05:07:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230021AbiJIJHd (ORCPT ); Sun, 9 Oct 2022 05:07:33 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2852723390 for ; Sun, 9 Oct 2022 02:07:32 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id i6so8465202pfb.2 for ; Sun, 09 Oct 2022 02:07:32 -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 :message-id:reply-to; bh=sxrqiw0cbhfXN4+OelLD9+srPRQcNc3GlNvIAj2f+tU=; b=oe5qxBC1H4qUilQPWPy/moxJ4w1ln70cLpRc/Wk8jkmt3TBM5d7VJNyft5SpWN8+Hr V+SK/9WSNTNYqY8WLXByi/u6h7gRuxPzVFGafKEyR+Y8/tLdczZTPqNCD93xhD5PTkaY XKLkVqBfhhtAFrvw7K6Y4FSE3vUclNw6hsVzk= 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=sxrqiw0cbhfXN4+OelLD9+srPRQcNc3GlNvIAj2f+tU=; b=g0aHrsQ8Sx2+UH2uUrcdJWBbqNrYMD4sZ6G0EOBfpVk+YwCa0bUXiq+5/EoMKSKAYA BX/ScaDybuRseS9KkygbrK4g24TGDfJL3RpMbKz2S86iENoOe1R7kzNGUYZJGArAd6De 8re994jQLC6KJ+olegbuA6IVDmjaer8PfPE+LKuJFqz0wscaxEqwVpLNkEMJJuWeirbU sCVIqcppX+y4L7TDqkKbkI0hz/Ux34cvFOJNmDSJaembkzWtmL4Nmln5IZMYh9AY/Ml4 1jpHr+Dh/GlPtDAvI0DGrZWdJmyGydV9oTQHlDGtBkVu5H5X/XiJJLM6ua29Bc8/E4i8 j39Q== X-Gm-Message-State: ACrzQf15bv9pb7BvY77pPwPB24bzcsb6kgpJHPIfFEkKTGJAFeG1WqiK X6uoTuAHAh3sk5jUjMpJeIFbQA== X-Google-Smtp-Source: AMsMyM6C8IGN2Dj12wvsCAFdbafvyBhEfZJPCUlA+MTMr1Fg8ofJtLAKdkOUiWQsDUoA0tspOCcD7g== X-Received: by 2002:a63:b4d:0:b0:454:d8b4:285 with SMTP id a13-20020a630b4d000000b00454d8b40285mr11989075pgl.410.1665306451631; Sun, 09 Oct 2022 02:07:31 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:31 -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: [PATCHv3 2/8] zram: Add recompression algorithm sysfs knob Date: Sun, 9 Oct 2022 18:07:14 +0900 Message-Id: <20221009090720.1040633-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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. 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 770ea3489eb6..a8ef3c0c3dae 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); =20 static int zram_major; -static const char *default_compressor =3D CONFIG_ZRAM_DEF_COMP; +static const char *default_comp_algs[ZRAM_MAX_ZCOMPS] =3D { + CONFIG_ZRAM_DEF_COMP, +#ifdef CONFIG_ZRAM_MULTI_COMP + "zstd", +#endif +}; =20 /* Module params (documentation at end) */ static unsigned int num_devices =3D 1; @@ -1000,31 +1005,37 @@ 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 idx, const char *alg) { - size_t sz; - struct zram *zram =3D dev_to_zram(dev); + bool default_alg =3D false; + int i; =20 - down_read(&zram->init_lock); - sz =3D 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 =3D 0; i < ZRAM_MAX_ZCOMPS; i++) { + if (zram->comp_algs[idx] =3D=3D default_comp_algs[i]) { + default_alg =3D true; + break; + } + } =20 - return sz; + if (!default_alg) + kfree(zram->comp_algs[idx]); + zram->comp_algs[idx] =3D alg; } =20 -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] !=3D default_compressor) - kfree(zram->comp_algs[idx]); - zram->comp_algs[idx] =3D alg; + ssize_t sz; + + down_read(&zram->init_lock); + sz =3D zcomp_available_show(zram->comp_algs[idx], buf); + up_read(&zram->init_lock); + + return sz; } =20 -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 =3D dev_to_zram(dev); char *compressor; size_t sz; =20 @@ -1053,11 +1064,55 @@ static ssize_t comp_algorithm_store(struct device *= dev, return -EBUSY; } =20 - 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 =3D 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 =3D dev_to_zram(dev); + int ret; + + ret =3D __comp_algorithm_store(zram, ZRAM_PRIMARY_ZCOMP, 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); + + 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 =3D dev_to_zram(dev); + int ret; + + ret =3D __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) { @@ -1762,7 +1817,11 @@ static void zram_reset_device(struct zram *zram) memset(&zram->stats, 0, sizeof(zram->stats)); reset_bdev(zram); =20 - 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); } =20 @@ -1895,6 +1954,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, @@ -1918,6 +1980,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 @@ -1997,7 +2062,11 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; =20 - zram->comp_algs[ZRAM_PRIMARY_ZCOMP] =3D default_compressor; + zram->comp_algs[ZRAM_PRIMARY_ZCOMP] =3D + default_comp_algs[ZRAM_PRIMARY_ZCOMP]; + if (IS_ENABLED(CONFIG_ZRAM_MULTI_COMP)) + zram->comp_algs[ZRAM_SECONDARY_ZCOMP] =3D + default_comp_algs[ZRAM_SECONDARY_ZCOMP]; =20 zram_debugfs_register(zram); pr_info("Added device: %s\n", zram->disk->disk_name); --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 21453C433FE for ; Sun, 9 Oct 2022 09:07:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229490AbiJIJHs (ORCPT ); Sun, 9 Oct 2022 05:07:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230033AbiJIJHf (ORCPT ); Sun, 9 Oct 2022 05:07:35 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 808A131214 for ; Sun, 9 Oct 2022 02:07:34 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id r18so8058191pgr.12 for ; Sun, 09 Oct 2022 02:07:34 -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 :message-id:reply-to; bh=NpDlcwWZSRK+GLE9jeOjGHO/GXqOD7ElWxbYUhdOrA4=; b=m/pPO/9xut56toEBoPrxZZRoR2qK004hf4UOkxTjnrozUXN7IvLCcvwk7FCrdEbtmv UcUpkQjZZdv77JkrcOn3KRTs/KPJaMi8J2NEQtT0vQZnwdZa00RA9gESYlF10wu7FW3W CsuaSVfjOrbsPVaazJib/NqDbLehCZx6rr5/A= 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=NpDlcwWZSRK+GLE9jeOjGHO/GXqOD7ElWxbYUhdOrA4=; b=erL2V4CAH8u56JydQ/4UH1Fo245kcnLYK3hodUPZJYJGCenkSSRHRPYH5uW4NMVq3s MjjaFWAAm2Rj4iRmjlwxOlSSgxGivCPZ2GJiW4bl3pfOcKglJk7OHxcWXH8QCWIu8f7H kd+DdpiV9mMxLtol246tKWbdR0Z4zCdzqeoAm62wAI36nLSxYXVY1ycXdhG6QunkvSpq HIk1xgZ5Adlg3N0+WpUxRuy6UCNrMY4p9P5SrBrcOExyVJ6hEGkqckUapLqS6mVYyy/9 d+paaejQ/QxN+xMXVByoUBQ84iBikjz6FGxRAS8Cdnw05cYq+kIHufCY/+856W48H+Tq mrhA== X-Gm-Message-State: ACrzQf0t3iq28vSZferZjM+nSFTw6iQmmYjpVXTb4ore8NSkUB+n+VPV J8VB0rDmdz1a5rST8MHL7C0pHg== X-Google-Smtp-Source: AMsMyM5O6XJLSh+H0wcf7xO6063i9Fd43x32Eg2ntgvjKRu11pYf1BZfHPExCSvDcr2/A0Gi6aZQfg== X-Received: by 2002:a63:1944:0:b0:455:bea0:97ee with SMTP id 4-20020a631944000000b00455bea097eemr12190033pgz.258.1665306454057; Sun, 09 Oct 2022 02:07:34 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:33 -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: [PATCHv3 3/8] zram: Factor out WB and non-WB zram read functions Date: Sun, 9 Oct 2022 18:07:15 +0900 Message-Id: <20221009090720.1040633-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" We will use non-WB variant in ZRAM page recompression path. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 73 ++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index a8ef3c0c3dae..94c62d7ea818 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1314,8 +1314,30 @@ static void zram_free_page(struct zram *zram, size_t= index) ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB)); } =20 -static int __zram_bvec_read(struct zram *zram, struct page *page, u32 inde= x, - struct bio *bio, bool partial_io) +/* + * Reads a page from the writeback devices. Corresponding ZRAM slot + * should be unlocked. + */ +static int zram_read_from_writeback(struct zram *zram, struct page *page, + u32 index, struct bio *bio, + bool partial_io) +{ + struct bio_vec bvec; + + bvec.bv_page =3D page; + bvec.bv_len =3D PAGE_SIZE; + bvec.bv_offset =3D 0; + return read_from_bdev(zram, &bvec, + zram_get_element(zram, index), + bio, partial_io); +} + +/* + * Reads (decompresses if needed) a page from zspool (zsmalloc). + * Corresponding ZRAM slot should be locked. + */ +static int zram_read_from_zspool(struct zram *zram, struct page *page, + u32 index) { struct zcomp_strm *zstrm; unsigned long handle; @@ -1323,23 +1345,6 @@ static int __zram_bvec_read(struct zram *zram, struc= t page *page, u32 index, void *src, *dst; int ret; =20 - zram_slot_lock(zram, index); - if (zram_test_flag(zram, index, ZRAM_WB)) { - struct bio_vec bvec; - - zram_slot_unlock(zram, index); - /* A null bio means rw_page was used, we must fallback to bio */ - if (!bio) - return -EOPNOTSUPP; - - bvec.bv_page =3D page; - bvec.bv_len =3D PAGE_SIZE; - bvec.bv_offset =3D 0; - return read_from_bdev(zram, &bvec, - zram_get_element(zram, index), - bio, partial_io); - } - handle =3D zram_get_handle(zram, index); if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) { unsigned long value; @@ -1349,7 +1354,6 @@ static int __zram_bvec_read(struct zram *zram, struct= page *page, u32 index, mem =3D kmap_atomic(page); zram_fill_page(mem, PAGE_SIZE, value); kunmap_atomic(mem); - zram_slot_unlock(zram, index); return 0; } =20 @@ -1371,17 +1375,40 @@ static int __zram_bvec_read(struct zram *zram, stru= ct page *page, u32 index, zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); } zs_unmap_object(zram->mem_pool, handle); - zram_slot_unlock(zram, index); + return ret; +} + +static int __zram_bvec_read(struct zram *zram, struct page *page, u32 inde= x, + struct bio *bio, bool partial_io) +{ + int ret; + + zram_slot_lock(zram, index); + if (!zram_test_flag(zram, index, ZRAM_WB)) { + /* Slot should be locked through out the function call */ + ret =3D zram_read_from_zspool(zram, page, index); + zram_slot_unlock(zram, index); + } else { + /* Slot should be unlocked before the function call */ + zram_slot_unlock(zram, index); + + /* A null bio means rw_page was used, we must fallback to bio */ + if (!bio) + return -EOPNOTSUPP; + + ret =3D zram_read_from_writeback(zram, page, index, bio, + partial_io); + } =20 /* Should NEVER happen. Return bio error if it does. */ - if (WARN_ON(ret)) + if (WARN_ON(ret < 0)) pr_err("Decompression failed! err=3D%d, page=3D%u\n", ret, index); =20 return ret; } =20 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, - u32 index, int offset, struct bio *bio) + u32 index, int offset, struct bio *bio) { int ret; struct page *page; --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 0A866C433FE for ; Sun, 9 Oct 2022 09:08:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230166AbiJIJI3 (ORCPT ); Sun, 9 Oct 2022 05:08:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbiJIJIB (ORCPT ); Sun, 9 Oct 2022 05:08:01 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B37AB31DC8 for ; Sun, 9 Oct 2022 02:07:48 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 78so8052804pgb.13 for ; Sun, 09 Oct 2022 02:07:48 -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 :message-id:reply-to; bh=Di6UYGr5o/GSN0ajTLtFClQB+xSRVwulRo8S0QrCcKI=; b=YhyVT+feaZTJaqgGyJt76SZs2ZcBpZEPkuaHpuvXaOtZOmphmqZgRaqvn3DYi0upEy t7yOIzNZs2gRP6fJS3kskvr4gH1ZoYrzP7/REgxqXRLm3i0Mufst4j2XcMHo7KeKIEop n/7EanSxuX44lUiLBTUef0T6xqzl8TmCaeIvc= 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=Di6UYGr5o/GSN0ajTLtFClQB+xSRVwulRo8S0QrCcKI=; b=zZueOjiU3WBBsUhwKz850sMNxQB4E6ygp57xnajYLcvQ97j27eOptTQshOwO1U6J+k JthwWMlebvpLz48jBME6VqwjTOZOKR6TO8ea9vPZ05rykIFLm2Eb/ypdgnlHu8Fj2hvP iQEexzl9EUjr3DLBb6VGHWRDNLzBD7xPAHr/Io1qNFAbIVCvEm8XhdqbfTwlYGJjT+vU B8vZ3tSRPv0FQOIRhX8qY6r0P8TzXEMpSu47EHQFpYk9p7En4Ck2G8pqfe9xuWUi2jR4 KBXzwIp5ONV1Hix8sDdnkNZoTVpciN1qPSwwqTgkz6kpWXWS+RIVa1fv0JYWIxXfklNL 6KFw== X-Gm-Message-State: ACrzQf1ncO62xWe03szIhynqMNlaxNuvMw0LNDM4BAwLwLRVpG55MaeV 0j4bcdzsWrjZLCbf4sdPjx/aWIun0BFpUw== X-Google-Smtp-Source: AMsMyM7YWoa+daGBBBz9uSUN9qJ47XLGsV80REC7vAB/6UsA5DqmTTpqUmhteFDCgPBWGb9qrzm1Yw== X-Received: by 2002:a17:902:bd44:b0:17f:fc22:3a74 with SMTP id b4-20020a170902bd4400b0017ffc223a74mr11276401plx.161.1665306456519; Sun, 09 Oct 2022 02:07:36 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:36 -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: [PATCHv3 4/8] zram: Introduce recompress sysfs knob Date: Sun, 9 Oct 2022 18:07:16 +0900 Message-Id: <20221009090720.1040633-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" Allow zram to recompress (using secondary compression streams) pages. We support three modes: 1) IDLE pages recompression is activated by `idle` mode echo idle > /sys/block/zram0/recompress 2) Since there may be many idle pages user-space may pass a size watermark value and we will recompress IDLE pages only of equal or greater size: echo 888 > /sys/block/zram0/recompress 3) HUGE pages recompression is activated by `huge` mode echo huge > /sys/block/zram0/recompress 4) HUGE_IDLE pages recompression is activated by `huge_idle` mode echo huge_idle > /sys/block/zram0/recompress Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 12 +++ drivers/block/zram/zram_drv.c | 194 +++++++++++++++++++++++++++++++++- drivers/block/zram/zram_drv.h | 2 + 3 files changed, 205 insertions(+), 3 deletions(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index d4100b0c083e..7febf36e93eb 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -78,3 +78,15 @@ config ZRAM_MEMORY_TRACKING /sys/kernel/debug/zram/zramX/block_state. =20 See Documentation/admin-guide/blockdev/zram.rst for more information. + +config ZRAM_MULTI_COMP + bool "Enable multiple per-CPU compression streams" + depends on ZRAM + help + This will enable per-CPU multi-compression streams, so that ZRAM + can re-compress IDLE/huge pages, using a potentially slower but + more effective compression algorithm. Note, that IDLE page support + requires ZRAM_MEMORY_TRACKING. + + echo TIMEOUT > /sys/block/zramX/idle + echo SIZE > /sys/block/zramX/recompress diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 94c62d7ea818..12368e773f3d 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1282,6 +1282,12 @@ static void zram_free_page(struct zram *zram, size_t= index) atomic64_dec(&zram->stats.huge_pages); } =20 + if (zram_test_flag(zram, index, ZRAM_RECOMP)) + zram_clear_flag(zram, index, ZRAM_RECOMP); + + if (zram_test_flag(zram, index, ZRAM_RECOMP_SKIP)) + zram_clear_flag(zram, index, ZRAM_RECOMP_SKIP); + if (zram_test_flag(zram, index, ZRAM_WB)) { zram_clear_flag(zram, index, ZRAM_WB); free_block_bdev(zram, zram_get_element(zram, index)); @@ -1343,6 +1349,7 @@ static int zram_read_from_zspool(struct zram *zram, s= truct page *page, unsigned long handle; unsigned int size; void *src, *dst; + u32 idx; int ret; =20 handle =3D zram_get_handle(zram, index); @@ -1359,8 +1366,13 @@ static int zram_read_from_zspool(struct zram *zram, = struct page *page, =20 size =3D zram_get_obj_size(zram, index); =20 - if (size !=3D PAGE_SIZE) - zstrm =3D zcomp_stream_get(zram->comps[ZRAM_PRIMARY_ZCOMP]); + if (size !=3D PAGE_SIZE) { + idx =3D ZRAM_PRIMARY_ZCOMP; + if (zram_test_flag(zram, index, ZRAM_RECOMP)) + idx =3D ZRAM_SECONDARY_ZCOMP; + + zstrm =3D zcomp_stream_get(zram->comps[idx]); + } =20 src =3D zs_map_object(zram->mem_pool, handle, ZS_MM_RO); if (size =3D=3D PAGE_SIZE) { @@ -1372,7 +1384,7 @@ static int zram_read_from_zspool(struct zram *zram, s= truct page *page, dst =3D kmap_atomic(page); ret =3D zcomp_decompress(zstrm, src, size, dst); kunmap_atomic(dst); - zcomp_stream_put(zram->comps[ZRAM_PRIMARY_ZCOMP]); + zcomp_stream_put(zram->comps[idx]); } zs_unmap_object(zram->mem_pool, handle); return ret; @@ -1603,6 +1615,180 @@ static int zram_bvec_write(struct zram *zram, struc= t bio_vec *bvec, return ret; } =20 +#ifdef CONFIG_ZRAM_MULTI_COMP +/* + * This function will decompress (unless it's ZRAM_HUGE) the page and then + * attempt to compress it using secondary compression algorithm (which is + * potentially more effective). + * + * Corresponding ZRAM slot should be locked. + */ +static int zram_recompress(struct zram *zram, + u32 index, + struct page *page, + int size_watermark) +{ + unsigned long handle_prev; + unsigned long handle_next; + unsigned int comp_len_next; + unsigned int comp_len_prev; + struct zcomp_strm *zstrm; + void *src, *dst; + int ret; + + handle_prev =3D zram_get_handle(zram, index); + if (!handle_prev) + return -EINVAL; + + comp_len_prev =3D zram_get_obj_size(zram, index); + /* + * Do not recompress objects that are already "small enough". + */ + if (comp_len_prev < size_watermark) + return 0; + + ret =3D zram_read_from_zspool(zram, page, index); + if (ret) + return ret; + + zstrm =3D zcomp_stream_get(zram->comps[ZRAM_SECONDARY_ZCOMP]); + src =3D kmap_atomic(page); + ret =3D zcomp_compress(zstrm, src, &comp_len_next); + kunmap_atomic(src); + + /* + * 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 + * don't attemp to re-compress it again (RECOMP_SKIP). + */ + if (comp_len_next >=3D huge_class_size || + comp_len_next >=3D comp_len_prev || + ret) { + zram_set_flag(zram, index, ZRAM_RECOMP_SKIP); + zram_clear_flag(zram, index, ZRAM_IDLE); + zcomp_stream_put(zram->comps[ZRAM_SECONDARY_ZCOMP]); + return ret; + } + + /* + * No direct reclaim (slow path) for handle allocation and no + * re-compression attempt (unlike in __zram_bvec_write()) since + * we already stored that object in zsmalloc. If we cannot alloc + * memory then me bail out. + */ + handle_next =3D zs_malloc(zram->mem_pool, comp_len_next, + __GFP_KSWAPD_RECLAIM | + __GFP_NOWARN | + __GFP_HIGHMEM | + __GFP_MOVABLE); + if (IS_ERR((void *)handle_next)) { + zcomp_stream_put(zram->comps[ZRAM_SECONDARY_ZCOMP]); + return -ENOMEM; + } + + dst =3D zs_map_object(zram->mem_pool, handle_next, ZS_MM_WO); + memcpy(dst, zstrm->buffer, comp_len_next); + zcomp_stream_put(zram->comps[ZRAM_SECONDARY_ZCOMP]); + + zs_unmap_object(zram->mem_pool, handle_next); + + zram_free_page(zram, index); + zram_set_handle(zram, index, handle_next); + zram_set_obj_size(zram, index, comp_len_next); + + zram_set_flag(zram, index, ZRAM_RECOMP); + atomic64_add(comp_len_next, &zram->stats.compr_data_size); + atomic64_inc(&zram->stats.pages_stored); + + return 0; +} + +#define RECOMPRESS_IDLE (1 << 0) +#define RECOMPRESS_HUGE (1 << 1) + +static ssize_t recompress_store(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct zram *zram =3D dev_to_zram(dev); + unsigned long nr_pages =3D zram->disksize >> PAGE_SHIFT; + unsigned long index; + struct page *page; + ssize_t ret =3D 0; + int mode, size_watermark =3D 0; + + if (sysfs_streq(buf, "idle")) { + mode =3D RECOMPRESS_IDLE; + } else if (sysfs_streq(buf, "huge")) { + mode =3D RECOMPRESS_HUGE; + } else if (sysfs_streq(buf, "huge_idle")) { + mode =3D RECOMPRESS_IDLE | RECOMPRESS_HUGE; + } else { + /* + * We will re-compress only idle objects equal or greater + * in size than watermark. + */ + ret =3D kstrtoint(buf, 10, &size_watermark); + if (ret) + return ret; + mode =3D RECOMPRESS_IDLE; + } + + if (size_watermark > PAGE_SIZE) + return ret; + + down_read(&zram->init_lock); + if (!init_done(zram)) { + ret =3D -EINVAL; + goto release_init_lock; + } + + page =3D alloc_page(GFP_KERNEL); + if (!page) { + ret =3D -ENOMEM; + goto release_init_lock; + } + + for (index =3D 0; index < nr_pages; index++) { + zram_slot_lock(zram, index); + + if (!zram_allocated(zram, index)) + goto next; + + if (mode & RECOMPRESS_IDLE && + !zram_test_flag(zram, index, ZRAM_IDLE)) + goto next; + + if (mode & RECOMPRESS_HUGE && + !zram_test_flag(zram, index, ZRAM_HUGE)) + goto next; + + if (zram_test_flag(zram, index, ZRAM_WB) || + zram_test_flag(zram, index, ZRAM_UNDER_WB) || + zram_test_flag(zram, index, ZRAM_SAME) || + zram_test_flag(zram, index, ZRAM_RECOMP) || + zram_test_flag(zram, index, ZRAM_RECOMP_SKIP)) + goto next; + + ret =3D zram_recompress(zram, index, page, size_watermark); +next: + zram_slot_unlock(zram, index); + if (ret) + break; + + cond_resched(); + } + + ret =3D len; + __free_page(page); + +release_init_lock: + up_read(&zram->init_lock); + return ret; +} +#endif + /* * zram_bio_discard - handler on discard request * @index: physical block index in PAGE_SIZE units @@ -1983,6 +2169,7 @@ static DEVICE_ATTR_RW(writeback_limit_enable); #endif #ifdef CONFIG_ZRAM_MULTI_COMP static DEVICE_ATTR_RW(recomp_algorithm); +static DEVICE_ATTR_WO(recompress); #endif =20 static struct attribute *zram_disk_attrs[] =3D { @@ -2009,6 +2196,7 @@ static struct attribute *zram_disk_attrs[] =3D { &dev_attr_debug_stat.attr, #ifdef CONFIG_ZRAM_MULTI_COMP &dev_attr_recomp_algorithm.attr, + &dev_attr_recompress.attr, #endif NULL, }; diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 4044ddbb2326..09b9ceb5dfa3 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -49,6 +49,8 @@ enum zram_pageflags { ZRAM_UNDER_WB, /* page is under writeback */ ZRAM_HUGE, /* Incompressible page */ ZRAM_IDLE, /* not accessed page since last idle marking */ + ZRAM_RECOMP, /* page was recompressed */ + ZRAM_RECOMP_SKIP, /* secondary algorithm cannot compress this page */ =20 __NR_ZRAM_PAGEFLAGS, }; --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 5E754C433FE for ; Sun, 9 Oct 2022 09:08:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230097AbiJIJIB (ORCPT ); Sun, 9 Oct 2022 05:08:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229810AbiJIJHr (ORCPT ); Sun, 9 Oct 2022 05:07:47 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E594131236 for ; Sun, 9 Oct 2022 02:07:39 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d10so8439312pfh.6 for ; Sun, 09 Oct 2022 02:07:39 -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 :message-id:reply-to; bh=OqM9mWTVfbPsXUXKnKUtd3WdYMNua/MWINqYRXykEKs=; b=RKbrhKqsTOg+IHSg3cwCrTY4ZG/+LpdneXqRPTBTZgcz5VO3HHb+9AxHDxUZ3Jb2gQ W9+FolOVoU5RPYyTNGMx8TF2g/qhCoAGnRJ1xhcEuqFdZ+9luHiMWlVruTmUYFSKhdIu PYKfmU+qpJwUkcvfrhZk58dMiWNXxIm3cM5kU= 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=OqM9mWTVfbPsXUXKnKUtd3WdYMNua/MWINqYRXykEKs=; b=nDAAu3H4tDUJ1/3cUX/3wygXs6b6VJ6xm44Nhjx2YSjBv9BBbl33QZRyk4FscoQFMU jP5hG4n3YdmsgGHGwdDq+yVdmupg2vum6/CIpQ106mZaY6b2cF+uhbKavneDyrn3ROgF TOkxgHx25AF2+000egX5DkhOYQ9brPwyhAQhRB7xxTLfD/c4OiihK6fyepaUnxT74ZB2 K8RIHB0HjlztpOBqgechZr/A0hLTOwZPi9613+IwOTtowsLjJG6DsKcl5cnZtrIPx0y3 gz1T7vuqv/NCZbLDtItGtf0tC8jHS0TG4RrUUmD89L7BFWikYWQlsf+XfxYBheKL/ob5 kDvg== X-Gm-Message-State: ACrzQf0VurS29ZbiVOAMRHAP6e7hNJxJ8W/m40XuO8dRmj8nMGdUqtCj eriI4UiIfzmHXL7PBbAyuZpoFQ== X-Google-Smtp-Source: AMsMyM67guaiwP/QYUJO3S8X8Xt6xiCerRRailhF+6FA6YUQCxdLbbWRbAwLSAGadIpcgCrGmDFfEA== X-Received: by 2002:a63:90ca:0:b0:458:f28d:917e with SMTP id a193-20020a6390ca000000b00458f28d917emr11934116pge.201.1665306458975; Sun, 09 Oct 2022 02:07:38 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:38 -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: [PATCHv3 5/8] documentation: Add recompression documentation Date: Sun, 9 Oct 2022 18:07:17 +0900 Message-Id: <20221009090720.1040633-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" Document user-space visible device attributes that are enabled by ZRAM_MULTI_COMP. Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 55 +++++++++++++++++++++ drivers/block/zram/zram_drv.c | 7 +-- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/ad= min-guide/blockdev/zram.rst index c73b16930449..88957fcb6ad7 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -401,6 +401,61 @@ budget in next setting is user's job. If admin wants to measure writeback count in a certain period, they could know it via /sys/block/zram0/bd_stat's 3rd column. =20 +recompression +------------- + +With CONFIG_ZRAM_MULTI_COMP, zram can recompress idle/huge pages using +alternative (secondary) compression algorithm. The basic idea is that +alternative compression algorithm can provide better compression ratio +at a price of (potentially) slower compression/decompression speeds. +Alternative compression algorithm can, for example, be more successful +compressing huge pages (those that default algorithm failed to compress). +Another application is idle pages recompression - pages that are cold and +sit in the memory can be recompressed using more effective algorithm and, +hence, reduce zsmalloc memory usage. + +With CONFIG_ZRAM_MULTI_COMP, zram will setup two compression algorithms +per-CPU: primary and secondary ones. Primary zram compressor is explained +in "3) Select compression algorithm", the secondary algorithm is configured +in a similar way, using recomp_algorithm device attribute: + +Examples:: + + #show supported recompression algorithms + cat /sys/block/zramX/recomp_algorithm + zstd [lzo] + + #select zstd recompression algorithm + echo zstd > /sys/block/zramX/recomp_algorithm + +Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress, +which controls recompression: + +Examples:: + + #IDLE pages recompression is activated by `idle` mode + echo idle > /sys/block/zramX/recompress + + #HUGE pages recompression is activated by `huge` mode + echo huge > /sys/block/zram0/recompress + + #HUGE_IDLE pages recompression is activated by `huge_idle` mode + echo huge_idle > /sys/block/zramX/recompress + +The number of idle pages can be significant, so user-space can pass a size +watermark value to the recompress knob, to filter out idle pages for +recompression: zram will recompress only idle pages of equal or greater +size::: + + #recompress idle pages larger than 3000 bytes + echo 3000 > /sys/block/zramX/recompress + + #recompress idle pages larger than 2000 bytes + echo 2000 > /sys/block/zramX/recompress + +Recompression is mostly focused on idle pages (except for huge pages +recompression), so it works better in conjunction with memory tracking. + memory tracking =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 12368e773f3d..143c33bd6783 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1623,9 +1623,7 @@ static int zram_bvec_write(struct zram *zram, struct = bio_vec *bvec, * * Corresponding ZRAM slot should be locked. */ -static int zram_recompress(struct zram *zram, - u32 index, - struct page *page, +static int zram_recompress(struct zram *zram, u32 index, struct page *page, int size_watermark) { unsigned long handle_prev; @@ -1708,8 +1706,7 @@ static int zram_recompress(struct zram *zram, =20 static ssize_t recompress_store(struct device *dev, struct device_attribute *attr, - const char *buf, - size_t len) + const char *buf, size_t len) { struct zram *zram =3D dev_to_zram(dev); unsigned long nr_pages =3D zram->disksize >> PAGE_SHIFT; --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 6D405C433FE for ; Sun, 9 Oct 2022 09:08:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230050AbiJIJIG (ORCPT ); Sun, 9 Oct 2022 05:08:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230035AbiJIJHz (ORCPT ); Sun, 9 Oct 2022 05:07:55 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 173B131346 for ; Sun, 9 Oct 2022 02:07:41 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id x6so8034539pll.11 for ; Sun, 09 Oct 2022 02:07:41 -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 :message-id:reply-to; bh=4cZ+puyQMSIB+n8E0d97j6XHSOdtq59YpKkyki1VJU8=; b=EgKwRSilPm7p4CO1u8/fIzwBKtIVXSWTmIsbJOKy4/04UduO4fKhE+2bWFSK6PWTy7 MUs1CmVg66IhdIP7LBiQ3/atcqWn26lPXRjdKFSnnEXRcmOc+F9INsSAneuaKX1eiKEB 7yEG45dA0ijTgitSeK72Gyqzf24vDXZ6hjyLY= 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=4cZ+puyQMSIB+n8E0d97j6XHSOdtq59YpKkyki1VJU8=; b=VkTA0F/pm6fzfJt55GuLAFkuzgKtLPJBEC44npohhPPt+QCE2RqIPrNViCSNQsBZgh sLxWynGARCaT0GJl4LsY8q9r0IoPFNj0Xz6ODxaYVmZgfyW4cIF4m/qzP+oZsAIrBj6H lW5ZpxRtA5xecushkETxV1efJ6mTJo89p2VKCMGRyJHQZFvVlG5GDgdAAcoiXrkqP6gA 022Z0J6fdCOog4ZNRYXHneOp2yEVuJQvbTBxqvgz+X0c12cO16QEhRPi2Ez9Rl7Lq2qs 7J3xZjDD+cO8Qrvo2upU95sE61wUUaNo0t2LDqygrW3UZ8vtT1bPL1+2CgJyu3RmLCkE JW1A== X-Gm-Message-State: ACrzQf1VRXAnsgqKUKIw19sqgr3V+8XXAJgs+VkYA0isjmPM51NaQnu4 Kvn4HyWTKNOaNwL5+7A1b/V/GnlFvQHanw== X-Google-Smtp-Source: AMsMyM6wBamnMbzu7YlLUOWaIL8i996NZuN+u1yUYf1GbwAo3RpqMwn47tLv84VDFkbJMphI+eG9ag== X-Received: by 2002:a17:902:ec83:b0:17c:afb3:d1ec with SMTP id x3-20020a170902ec8300b0017cafb3d1ecmr13185807plg.172.1665306461306; Sun, 09 Oct 2022 02:07:41 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:41 -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: [PATCHv3 6/8] zram: Add recompression algorithm choice to Kconfig Date: Sun, 9 Oct 2022 18:07:18 +0900 Message-Id: <20221009090720.1040633-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" Make (secondary) recompression algorithm selectable just like we do it for the (primary) default one. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/Kconfig | 40 +++++++++++++++++++++++++++++++++++ drivers/block/zram/zram_drv.c | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 7febf36e93eb..442885ac72ca 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -90,3 +90,43 @@ config ZRAM_MULTI_COMP =20 echo TIMEOUT > /sys/block/zramX/idle echo SIZE > /sys/block/zramX/recompress + +choice + prompt "Default zram recompression algorithm" + default ZRAM_DEF_RECOMP_ZSTD + depends on ZRAM && ZRAM_MULTI_COMP + +config ZRAM_DEF_RECOMP_LZORLE + bool "lzo-rle" + depends on CRYPTO_LZO + +config ZRAM_DEF_RECOMP_ZSTD + bool "zstd" + depends on CRYPTO_ZSTD + +config ZRAM_DEF_RECOMP_LZ4 + bool "lz4" + depends on CRYPTO_LZ4 + +config ZRAM_DEF_RECOMP_LZO + bool "lzo" + depends on CRYPTO_LZO + +config ZRAM_DEF_RECOMP_LZ4HC + bool "lz4hc" + depends on CRYPTO_LZ4HC + +config ZRAM_DEF_RECOMP_842 + bool "842" + depends on CRYPTO_842 + +endchoice + +config ZRAM_DEF_RECOMP + string + default "lzo-rle" if ZRAM_DEF_RECOMP_LZORLE + default "zstd" if ZRAM_DEF_RECOMP_ZSTD + default "lz4" if ZRAM_DEF_RECOMP_LZ4 + default "lzo" if ZRAM_DEF_RECOMP_LZO + default "lz4hc" if ZRAM_DEF_RECOMP_LZ4HC + default "842" if ZRAM_DEF_RECOMP_842 diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 143c33bd6783..7285a85b4054 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -44,7 +44,7 @@ static int zram_major; static const char *default_comp_algs[ZRAM_MAX_ZCOMPS] =3D { CONFIG_ZRAM_DEF_COMP, #ifdef CONFIG_ZRAM_MULTI_COMP - "zstd", + CONFIG_ZRAM_DEF_RECOMP, #endif }; =20 --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 138BBC433FE for ; Sun, 9 Oct 2022 09:08:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230082AbiJIJIN (ORCPT ); Sun, 9 Oct 2022 05:08:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230058AbiJIJH5 (ORCPT ); Sun, 9 Oct 2022 05:07:57 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B31D931358 for ; Sun, 9 Oct 2022 02:07:44 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id b2so8041736plc.7 for ; Sun, 09 Oct 2022 02:07:44 -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 :message-id:reply-to; bh=5N+idb9Meu8bPgxV55r58/HUuOBKJs8gG9ElyKqtXIc=; b=Y9iTT0+zRaa4UXFiqtUyNe+GZMNhwGfWFvD2qw8EcyhN30/JkANHqh6exb6Tvly/D2 ozVLLGVefoQfieaWnBXFVKNSFSvVNfgcfNpFVTyp0JuWTvz3d6lhQytCAr0PB0S/K1Gr xjkMHUwZuSW9W4NSn0+pi1i4T6+rRDN/i8bH4= 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=5N+idb9Meu8bPgxV55r58/HUuOBKJs8gG9ElyKqtXIc=; b=3JPDCnL/u1v0vqyuCDsoDTYol3PrARL020rWlhGRFM3wI7VNkQ70arYR8kevJH6c87 LgAmhtVdnl+SXGGBNe+7J8sz7xMkTQ38e/Rv9V+i266/1BmytTudfXrzHoKMeAlOkK6Q aDrBE86SJELtJdmvscnwNeslHYaF48Jb730NtdFqX8x1gySEo27O4nCLAsgkGJhp5HEt fBtxTyfZ2wZz+B9TMxh6HTOY8GEiQ9gypG13ZAPs2jTSffwrsb4RKndjbAE9nt2Ml/te 0K6XFeH1UtMPTi1Bkglmeaj+RqFjD5YVK9PYvb7Gx956LdaZ2ntmzWyILu5cKBtpqD54 BVTw== X-Gm-Message-State: ACrzQf0Q4+HeVJH07fw2W0FHHeNAr+lVU98T/WTOtubC7PF7lKgcFsZG zu/C+s3/VGVt9Bn++quZzmzWOA== X-Google-Smtp-Source: AMsMyM4wsiBlX1NkUhAfcaouRhXX8PAS3oIjVVHLWLI0PncP5Tf5uAod8RAj/q7gd5Nan0SlLp2qHg== X-Received: by 2002:a17:902:ec8d:b0:178:2914:b5a0 with SMTP id x13-20020a170902ec8d00b001782914b5a0mr13430611plg.17.1665306463751; Sun, 09 Oct 2022 02:07:43 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:43 -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: [PATCHv3 7/8] zram: Add recompress flag to read_block_state() Date: Sun, 9 Oct 2022 18:07:19 +0900 Message-Id: <20221009090720.1040633-8-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" Add a new flag to zram block state that shows if the page was recompressed (using alternative compression algorithm). Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 9 ++++++--- drivers/block/zram/zram_drv.c | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/ad= min-guide/blockdev/zram.rst index 88957fcb6ad7..70a3d0243b45 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -466,9 +466,10 @@ pages of the process with*pagemap. If you enable the feature, you could see block state via /sys/kernel/debug/zram/zram0/block_state". The output is as follows:: =20 - 300 75.033841 .wh. - 301 63.806904 s... - 302 63.806919 ..hi + 300 75.033841 .wh.. + 301 63.806904 s.... + 302 63.806919 ..hi. + 303 62.801919 ....r =20 First column zram's block index. @@ -485,6 +486,8 @@ Third column huge page i: idle page + r: + recompressed page (secondary compression algorithm) =20 First line of above example says 300th block is accessed at 75.033841sec and the block's state is huge so it is written back to the backing diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 7285a85b4054..34bb21691cee 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -925,13 +925,14 @@ static ssize_t read_block_state(struct file *file, ch= ar __user *buf, =20 ts =3D ktime_to_timespec64(zram->table[index].ac_time); copied =3D snprintf(kbuf + written, count, - "%12zd %12lld.%06lu %c%c%c%c\n", + "%12zd %12lld.%06lu %c%c%c%c%c\n", index, (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC, zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.', zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.', zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.', - zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.'); + zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.', + zram_test_flag(zram, index, ZRAM_RECOMP) ? 'r' : '.'); =20 if (count <=3D copied) { zram_slot_unlock(zram, index); --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Tue Apr 7 00:28:03 2026 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 E7E37C433FE for ; Sun, 9 Oct 2022 09:08:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230138AbiJIJIR (ORCPT ); Sun, 9 Oct 2022 05:08:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbiJIJH6 (ORCPT ); Sun, 9 Oct 2022 05:07:58 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB23A31377 for ; Sun, 9 Oct 2022 02:07:46 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id i6so8465444pfb.2 for ; Sun, 09 Oct 2022 02:07:46 -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 :message-id:reply-to; bh=yuRbPyGZZsBm+5IGqHamjUR/zxD3kPUYp8Ab64laoNc=; b=oOmLwkPegZkRA22IheBP7jpj4rmN4FNV+6EnSr8jIB4n5RzMMUBb94GoepCq8HsBWc wToGz+fobeVepMARste1oDCCsKjfC5vJ3MkWqQukaYD4qtrGqrEZq3tH6F+DfrHOdfHb kPbYtXBxq2sTBcE0H0EKh7MhwrpKsuyWl3O88= 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=yuRbPyGZZsBm+5IGqHamjUR/zxD3kPUYp8Ab64laoNc=; b=uyGTJX449Xqs9ajK0+U6e7Irt0yBXrGTzP6q6DBXQryFG07OsgXuCBiORKgcJ4qP7B Fsqb3RPF1CAobSmz7m2hpQ31Sx5BrH9uxkH67LCbXsiTvlaOBAPQn3UWuCTaP1lhjvkl /r8g3QXeJYxEdOVWrcOfh2pfEelNIj7YUeeMYgDok3fNfi0ImxP5QRbMyelA4oUy2TNm l00Z1SMQTvCF4aezYipWGBj/zAPCpJzXvBA8ZasJQQpCynJxQoKxeb0wNuGSvFEhv0Rz K20DXKQqflQquNMuZxT8GhZThGzIM9uqQNnkZmxlrEM5KwIfpa5SJ6kR9cBrstMFuhqH msEw== X-Gm-Message-State: ACrzQf2DDSDbtautVKVHnV6BBAx63KIKPxdKfGl6FjD2XICfPGNPH0Bk T/zFwCA8dyd4/csIil5w+AEWOknT/G59wg== X-Google-Smtp-Source: AMsMyM7emD3Idi1m55hr8/6YiYa0jBoU5n5s7t3oFzxQwBHDrOMjbsnZwwXmfSQt4BFw/i9VJmJBOQ== X-Received: by 2002:a63:6905:0:b0:43c:d4:eef4 with SMTP id e5-20020a636905000000b0043c00d4eef4mr11578195pgc.126.1665306466139; Sun, 09 Oct 2022 02:07:46 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:8517:d51e:5fe9:9be7]) by smtp.gmail.com with ESMTPSA id q194-20020a632acb000000b00439d071c110sm4335148pgq.43.2022.10.09.02.07.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Oct 2022 02:07:45 -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: [PATCHv3 8/8] zram: correct typos Date: Sun, 9 Oct 2022 18:07:20 +0900 Message-Id: <20221009090720.1040633-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221009090720.1040633-1-senozhatsky@chromium.org> References: <20221009090720.1040633-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" Trivial comment typos fixes. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 34bb21691cee..ecbc5963b5b8 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -759,7 +759,7 @@ static ssize_t writeback_store(struct device *dev, zram_slot_unlock(zram, index); /* * Return last IO error unless every IO were - * not suceeded. + * not succeeded. */ ret =3D err; continue; @@ -1658,7 +1658,7 @@ static int zram_recompress(struct zram *zram, u32 ind= ex, struct page *page, /* * 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 - * don't attemp to re-compress it again (RECOMP_SKIP). + * don't attempt to re-compress it again (RECOMP_SKIP). */ if (comp_len_next >=3D huge_class_size || comp_len_next >=3D comp_len_prev || --=20 2.38.0.rc1.362.ged0d419d3c-goog