From nobody Mon Apr 6 19:40:30 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 B12CCC433FE for ; Wed, 5 Oct 2022 02:40:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229681AbiJECkb (ORCPT ); Tue, 4 Oct 2022 22:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiJECk0 (ORCPT ); Tue, 4 Oct 2022 22:40:26 -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 761994B486 for ; Tue, 4 Oct 2022 19:40:25 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id a26so5714602pfg.7 for ; Tue, 04 Oct 2022 19:40:25 -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=RN0/9ftrQEX77iPPbChQJHYVjybhF/OgEdl9wxmnkf4=; b=nQlfnrsaj0ac1FJKgPoHcP1gtXNfui8pyXYvooL/6jB5mikJ/wUfpeEjTZY7CcRr35 Fi2LEDpz/3Z0Pu026JAgJlVtjef1nCofyor/7Rb3gAjcenmdVTfXe3vvAxAf4dpeGlyg inaHhUIY5EG3pU5Hs04f+Ui8ibSv6LWk3YH3Q= 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=RN0/9ftrQEX77iPPbChQJHYVjybhF/OgEdl9wxmnkf4=; b=xG9/JOH2xkjaaS0eTew6ICIbCbtQNfJxAl2g5itWSy8kcvB74d14FoGNKh1bt7aB35 qnEfvrOyJteaSUyIRuO6g7F0gdgI+g49WAIoYJDyEVBEocc1wJX7F6oxguDfAnwVhT4J 1DoXSmDRWVM/meZzm3VSPz7FVO1iKgvIr7NYg4D8k3lvBQi+fXz97oeg9ZnINxm+nVrM DIIQvk/s1dNrcw9hXaKybfbnXpnAFrKoVnhkipyFXbXxf6O11whsmj8F3mEaGeQJaF5/ fkEf9lsgCKd9F3le585swmrjbuhQr0Wtqm5GE72uX5LFrpBAPyBbPMQ6ftJUS2SZCtlS jzmA== X-Gm-Message-State: ACrzQf2l/hNO/AaegNph6TPyTGSrhnDBUk7mAtiMvJ+nqhmkRXZQWhjg mcq78kWc8Lix7wchXqepujVGfQ== X-Google-Smtp-Source: AMsMyM6a01BazGwgDzIKWc7xLCFsB4UevKtw/MTbCyGaQz/G72GLvZa3xsSvhW4Ar/31g2sfusl3sQ== X-Received: by 2002:a63:c1f:0:b0:41a:9b73:a89e with SMTP id b31-20020a630c1f000000b0041a9b73a89emr25503591pgl.342.1664937624880; Tue, 04 Oct 2022 19:40:24 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:a9d9:6396:ee12:8be8]) by smtp.gmail.com with ESMTPSA id a14-20020aa794ae000000b00561e010a309sm1673579pfl.19.2022.10.04.19.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 19:40:24 -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: [PATCHv2 1/8] zram: Preparation for multi-zcomp support Date: Wed, 5 Oct 2022 11:40:07 +0900 Message-Id: <20221005024014.22914-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221005024014.22914-1-senozhatsky@chromium.org> References: <20221005024014.22914-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