From patchwork Mon May 6 07:58:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13655046 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DD90142E9D for ; Mon, 6 May 2024 07:59:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982351; cv=none; b=iNREu2O4HzgkIqDx2+dLRUa4ri06I3q+58+hD1oFPcW8x3avyvQbGtjaeuFwRIayk47HtvFKlEiGF4NdUs2YQ4vfqEO8V9plg8ym4hya1G/vlfmrEVE6pr4RKvWa5YFrenu+fHeFYEg8HMrIVasZfFmuaAxS/a/M7IOd1wYJwSg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982351; c=relaxed/simple; bh=2Vyk+Ag37u4kDGLIMiSings1Oonk1CeUx8x+t48thY8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qfMtTkDlY/N04BSgeVByZQMD1eLT1302yhd2Px8jaqYTiiUwPbFmsb2ougHLS6D7xp2Ub1wIFsl5+uGY0Uaoyd67IoqY6ordsspmEABCeFbjwvWRNwELA7AZstebvyabW9aQPlOtdUf+IwO0VGQRh6HTpBo6gVnCHgPa/2VLqDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Zw2Lam0O; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Zw2Lam0O" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6f44dd41a5cso1631141b3a.0 for ; Mon, 06 May 2024 00:59:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1714982349; x=1715587149; darn=vger.kernel.org; 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=pVh6NYo/4zssb8icrCllJJuSBkrSwxoCvV3IlQAP7Ko=; b=Zw2Lam0Os1nG0TuPs2vIV5KPyjJOuUkRK/Jue8vHkRxUEgAV5eCpo/hfrlooipCg7t TOLLCZhhre75c7xA5oJbaFRsZcSVEElz279C1JTX4DVuKp2vP0CAB4Ay9ThF+3w8WfkZ UzWu2ZrBmxk5i/Zv6CO98u56XZF0QhWIAjjC8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714982349; x=1715587149; 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=pVh6NYo/4zssb8icrCllJJuSBkrSwxoCvV3IlQAP7Ko=; b=nuuO/U6kkYZO9GYds19haUS4z9Hn+GSQWT/e4Jx7flKe8E9EE0i1JPLHybWqQ8mmc7 e4bl9dtxuVu51CPq3/TIcfA5TcFONFMfO7yAimUW1l55cuhTt9GIQ/W1XDRE/97/AB7a 1Tckj2MAQyRBZPSwtuKBFQ9eT8squo2xAoBMKmf+ruwRStUaOgNO77/NujwsuBhG34qq OOtCm71/C7BcViIQEmlzvJqxoOXney+4gkumqwYkdMleuuUTznpIxxPQleNm8mQEQ/92 f1SXIIr/6caxni5L2Oi6R2X8bPLgTcxhP2RIiY7RQneb2RYLJusyiyuODM/BAuQyAla9 Uv3g== X-Forwarded-Encrypted: i=1; AJvYcCUclPxWbRUMilzeCVMdeOImjQbf+sUiYMb3v/NPSmed0A1dOf+Mi8uB3A9sc8JE7fE4cNKAMc8c+h5c2MVzbszE0J+etB39dZqh7AA= X-Gm-Message-State: AOJu0YwDU3VIfiW3ko11u0dkMhIlxnWWypBsHfRwXV4BUu+Bwo2cBwFH XaPbrrjewvqJzJdOWrgEee9H1a7sehyTrNkfnmG4f8hjmermYCDGnSReoi+scM9leXx3qEtcg04 = X-Google-Smtp-Source: AGHT+IH/mSd3rLEiYYj8dUEjAhdpkeQnt3cdXAUtQ7VcLpCUW6PImX4BG/ZFvXc1Yt3FkqqB5nM56A== X-Received: by 2002:a05:6a21:1798:b0:1af:ac31:9b8b with SMTP id nx24-20020a056a21179800b001afac319b8bmr4497887pzb.10.1714982349467; Mon, 06 May 2024 00:59:09 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:4e24:10c3:4b65:e126]) by smtp.gmail.com with ESMTPSA id h6-20020a170902f54600b001ec64b128dasm7633772plf.129.2024.05.06.00.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 00:59:09 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 10/17] zram: introduce zcomp_config structure Date: Mon, 6 May 2024 16:58:23 +0900 Message-ID: <20240506075834.302472-11-senozhatsky@chromium.org> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog In-Reply-To: <20240506075834.302472-1-senozhatsky@chromium.org> References: <20240506075834.302472-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We will store a per-algorithm parameters there (compression level, dictionary, dictionary size, etc.). Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/backend_842.c | 2 +- drivers/block/zram/backend_deflate.c | 9 ++++++--- drivers/block/zram/backend_lz4.c | 2 +- drivers/block/zram/backend_lz4hc.c | 10 ++++++---- drivers/block/zram/backend_lzo.c | 2 +- drivers/block/zram/backend_lzorle.c | 2 +- drivers/block/zram/backend_zstd.c | 8 ++++++-- drivers/block/zram/zcomp.c | 5 +++-- drivers/block/zram/zcomp.h | 14 ++++++++++++-- drivers/block/zram/zram_drv.c | 20 +++++++++++++++++++- drivers/block/zram/zram_drv.h | 1 + 11 files changed, 57 insertions(+), 18 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_842.c index 8ea7a230b890..12e716deb763 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -19,7 +19,7 @@ static void destroy_842(void *ctx) kfree(zctx); } -static void *create_842(void) +static void *create_842(struct zcomp_config *config) { struct sw842_ctx *ctx; diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/backend_deflate.c index 949d402ea3dd..83c660adc722 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -32,7 +32,7 @@ static void deflate_destroy(void *ctx) kfree(zctx); } -static void *deflate_create(void) +static void *deflate_create(struct zcomp_config *config) { struct deflate_ctx *ctx; size_t sz; @@ -42,8 +42,11 @@ static void *deflate_create(void) if (!ctx) return NULL; - /* @FIXME: using a hardcoded Z_DEFAULT_COMPRESSION for now */ - ctx->level = Z_DEFAULT_COMPRESSION; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = Z_DEFAULT_COMPRESSION; + sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL); ctx->cctx.workspace = vzalloc(sz); if (!ctx->cctx.workspace) diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_lz4.c index 697592dbabe2..98d9c9274149 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -4,7 +4,7 @@ #include "backend_lz4.h" -static void *lz4_create(void) +static void *lz4_create(struct zcomp_config *config) { return vmalloc(LZ4_MEM_COMPRESS); } diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backend_lz4hc.c index 5c437623aa65..62721a7c82dc 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -18,7 +18,7 @@ static void lz4hc_destroy(void *ctx) kfree(zctx); } -static void *lz4hc_create(void) +static void *lz4hc_create(struct zcomp_config *config) { struct lz4hc_ctx *ctx; @@ -26,14 +26,16 @@ static void *lz4hc_create(void) if (!ctx) return NULL; + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = LZ4HC_DEFAULT_CLEVEL; + ctx->mem = vmalloc(LZ4HC_MEM_COMPRESS); if (!ctx->mem) { lz4hc_destroy(ctx); return NULL; } - - /* @FIXME: using a hardcoded LZ4HC_DEFAULT_CLEVEL for now */ - ctx->level = LZ4HC_DEFAULT_CLEVEL; return ctx; } diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_lzo.c index b88b408964cd..79ecfae9c4c5 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -6,7 +6,7 @@ #include "backend_lzo.h" -static void *lzo_create(void) +static void *lzo_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backend_lzorle.c index 9bf1843021b0..b0937103b5fb 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -6,7 +6,7 @@ #include "backend_lzorle.h" -static void *lzorle_create(void) +static void *lzorle_create(struct zcomp_config *config) { return kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); } diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend_zstd.c index 4a7734aa1a8a..b2fb94902bef 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -24,7 +24,7 @@ static void zstd_destroy(void *ctx) kfree(zctx); } -static void *zstd_create(void) +static void *zstd_create(struct zcomp_config *config) { zstd_parameters params; struct zstd_ctx *ctx; @@ -34,7 +34,11 @@ static void *zstd_create(void) if (!ctx) return NULL; - ctx->level = ZSTD_defaultCLevel(); + if (config->level != ZCOMP_CONFIG_NO_LEVEL) + ctx->level = config->level; + else + ctx->level = ZSTD_defaultCLevel(); + params = zstd_get_params(ctx->level, PAGE_SIZE); sz = zstd_cctx_workspace_bound(¶ms.cParams); ctx->cctx_mem = vzalloc(sz); diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index d49791f724e9..3f990a715487 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -61,7 +61,7 @@ static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) */ static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm) { - zstrm->ctx = comp->backend->create_ctx(); + zstrm->ctx = comp->backend->create_ctx(comp->config); /* * allocate 2 pages. 1 for compressed data, plus 1 extra for the @@ -204,7 +204,7 @@ void zcomp_destroy(struct zcomp *comp) kfree(comp); } -struct zcomp *zcomp_create(const char *alg) +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config) { struct zcomp *comp; int error; @@ -221,6 +221,7 @@ struct zcomp *zcomp_create(const char *alg) if (!comp) return ERR_PTR(-ENOMEM); + comp->config = config; comp->backend = lookup_backend(alg); if (!comp->backend) { kfree(comp); diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 757b85017e23..345c78bc76db 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -5,6 +5,7 @@ #ifndef _ZCOMP_H_ #define _ZCOMP_H_ + #include struct zcomp_strm { @@ -15,6 +16,14 @@ struct zcomp_strm { void *ctx; }; +#define ZCOMP_CONFIG_NO_LEVEL INT_MIN + +struct zcomp_config { + s32 level; + size_t dict_sz; + void *dict; +}; + struct zcomp_backend { int (*compress)(void *ctx, const unsigned char *src, unsigned char *dst, size_t *dst_len); @@ -22,7 +31,7 @@ struct zcomp_backend { int (*decompress)(void *ctx, const unsigned char *src, size_t src_len, unsigned char *dst); - void *(*create_ctx)(void); + void *(*create_ctx)(struct zcomp_config *config); void (*destroy_ctx)(void *ctx); const char *name; @@ -32,6 +41,7 @@ struct zcomp_backend { struct zcomp { struct zcomp_strm __percpu *stream; struct zcomp_backend *backend; + struct zcomp_config *config; struct hlist_node node; }; @@ -40,7 +50,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node); ssize_t zcomp_available_show(const char *comp, char *buf); bool zcomp_available_algorithm(const char *comp); -struct zcomp *zcomp_create(const char *alg); +struct zcomp *zcomp_create(const char *alg, struct zcomp_config *config); void zcomp_destroy(struct zcomp *comp); 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 298ef0e97e03..6c36cd349431 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1994,6 +1994,20 @@ static void zram_slot_free_notify(struct block_device *bdev, zram_slot_unlock(zram, index); } +static void zram_reset_comp_configs(struct zram *zram) +{ + u32 prio; + + for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) { + struct zcomp_config *config = &zram->configs[prio]; + + vfree(config->dict); + config->level = ZCOMP_CONFIG_NO_LEVEL; + config->dict_sz = 0; + config->dict = NULL; + } +} + static void zram_destroy_comps(struct zram *zram) { u32 prio; @@ -2007,6 +2021,8 @@ static void zram_destroy_comps(struct zram *zram) zcomp_destroy(comp); zram->num_active_comps--; } + + zram_reset_comp_configs(zram); } static void zram_reset_device(struct zram *zram) @@ -2064,7 +2080,8 @@ static ssize_t disksize_store(struct device *dev, if (!zram->comp_algs[prio]) continue; - comp = zcomp_create(zram->comp_algs[prio]); + comp = zcomp_create(zram->comp_algs[prio], + &zram->configs[prio]); if (IS_ERR(comp)) { pr_err("Cannot initialise %s compressing backend\n", zram->comp_algs[prio]); @@ -2271,6 +2288,7 @@ static int zram_add(void) if (ret) goto out_cleanup_disk; + zram_reset_comp_configs(zram); comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); zram_debugfs_register(zram); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 37bf29f34d26..2c77f3bff166 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -107,6 +107,7 @@ struct zram { struct zram_table_entry *table; struct zs_pool *mem_pool; struct zcomp *comps[ZRAM_MAX_COMPS]; + struct zcomp_config configs[ZRAM_MAX_COMPS]; struct gendisk *disk; /* Prevent concurrent execution of device init */ struct rw_semaphore init_lock;