From nobody Sat Nov 23 14:04:17 2024 Received: from mx1.sberdevices.ru (mx1.sberdevices.ru [37.18.73.165]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC15F1AA1F1; Tue, 19 Nov 2024 12:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.18.73.165 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019275; cv=none; b=PyfVUNcLz5bO5AZ9M8QUCkjeBeCmmyizERSV1W0rKnIgY9wet69x1Z2lQrSoFZ7XjQyBzxOkII7pM2VnX2/V+vGS76Ov286laFiVhlPUydQQtqrK4kTWwPvgj9qX00H1KuBbqQzUOZ7CsbMraZYhmQpOH0vpfIOdm3ZMZDzMYFM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019275; c=relaxed/simple; bh=6XpyFTX2IuB5ot3nsoK50HO+wkWs9VGANW6Iz4/OXwY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=geXfa6o9+zUG1QW+nfr9MD4lhwx3SVsHHiHgtTBoPN9vIPUlqXGP55RPQyT6W06UBM9EGMF5RNbfmjXW0t4+uRhY2EnpPeZo3eJd7YwfnBVcZmSBN3lEKIs9j9oMnQlb+QtDJm3/y9JmZ5Qq31p61JPNsmv/6eftbHxl3OFOYco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com; spf=pass smtp.mailfrom=salutedevices.com; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b=Z/9OiHK0; arc=none smtp.client-ip=37.18.73.165 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b="Z/9OiHK0" Received: from p-infra-ksmg-sc-msk01.sberdevices.ru (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 984A210000A; Tue, 19 Nov 2024 15:27:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 984A210000A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1732019263; bh=I61/Dpjoy4FuWjLiA71sDLbq/BGk0dF2e0BXKgpFj+o=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=Z/9OiHK0u6mL2CtAGuWRaBNYzE9TdLNpAqSpCQEmXRw0TkbBAJ28o0dbH5wRaVX/V 9v+0IeBBB5YyKRgCM8CByRCtBMFlwp5ZMUVz1voeP4qf3DtXly6LSZ5TL8mZnMqLao JhZd1ai3h3w10S8utGaZP/wToYMDW/CmeBiyq0k103LKDqLanAwOMyTMCJdFTHyuPP 7XTO0Sy10KdptEdudhCBfIoBN1BtotKGGeCvMYFMn/3HM8+5mhMbcT8X7kEcxBC0EU JTrwRo7hLsOsRHDKwWMCZWKM3JQOrs0dZSUga5hmmpDgCdR4+Wip84nr0PFI268JMG xY4DGehjqGlxg== Received: from smtp.sberdevices.ru (p-i-exch-sc-m02.sberdevices.ru [172.16.192.103]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Tue, 19 Nov 2024 15:27:43 +0300 (MSK) From: Alexey Romanov To: , , , CC: , , , Alexey Romanov Subject: [PATCH v1 1/3] zram: pass zcomp instead of zcomp_params to create_context method Date: Tue, 19 Nov 2024 15:27:11 +0300 Message-ID: <20241119122713.3294173-2-avromanov@salutedevices.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119122713.3294173-1-avromanov@salutedevices.com> References: <20241119122713.3294173-1-avromanov@salutedevices.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: p-i-exch-a-m1.sberdevices.ru (172.24.196.116) To p-i-exch-a-m1.sberdevices.ru (172.24.196.116) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 189267 [Nov 19 2024] X-KSMG-AntiSpam-Version: 6.1.1.7 X-KSMG-AntiSpam-Envelope-From: avromanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 41 0.3.41 623e98d5198769c015c72f45fabbb9f77bdb702b, {Tracking_from_domain_doesnt_match_to}, salutedevices.com:7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2;smtp.sberdevices.ru:5.0.1,7.1.1, FromAlignment: s X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/11/19 08:41:00 #26886618 X-KSMG-AntiVirus-Status: Clean, skipped Content-Type: text/plain; charset="utf-8" This is a more general approach: for some backends, some of the data needed to create a context can be stored in zcomp structure. And we also can get zcomp_params structure from zcomp. Signed-off-by: Alexey Romanov --- drivers/block/zram/backend_842.c | 2 +- drivers/block/zram/backend_deflate.c | 3 ++- drivers/block/zram/backend_lz4.c | 3 ++- drivers/block/zram/backend_lz4hc.c | 3 ++- drivers/block/zram/backend_lzo.c | 2 +- drivers/block/zram/backend_lzorle.c | 2 +- drivers/block/zram/backend_zstd.c | 3 ++- drivers/block/zram/zcomp.c | 2 +- drivers/block/zram/zcomp.h | 4 +++- 9 files changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_= 842.c index 10d9d5c60f53..9147feb1e994 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -21,7 +21,7 @@ static void destroy_842(struct zcomp_ctx *ctx) kfree(ctx->context); } =20 -static int create_842(struct zcomp_params *params, struct zcomp_ctx *ctx) +static int create_842(struct zcomp *zcomp, struct zcomp_ctx *ctx) { ctx->context =3D kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); if (!ctx->context) diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/back= end_deflate.c index 0f7f252c12f4..10fde82dc5e7 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -46,8 +46,9 @@ static void deflate_destroy(struct zcomp_ctx *ctx) kfree(zctx); } =20 -static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *c= tx) +static int deflate_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) { + struct zcomp_params *params =3D zcomp->params; struct deflate_ctx *zctx; size_t sz; int ret; diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_= lz4.c index 847f3334eb38..8f7c8f16b6ce 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -37,8 +37,9 @@ static void lz4_destroy(struct zcomp_ctx *ctx) kfree(zctx); } =20 -static int lz4_create(struct zcomp_params *params, struct zcomp_ctx *ctx) +static int lz4_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) { + struct zcomp_params *params =3D zcomp->params; struct lz4_ctx *zctx; =20 zctx =3D kzalloc(sizeof(*zctx), GFP_KERNEL); diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backen= d_lz4hc.c index 5f37d5abcaeb..b0302b8027ab 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -37,8 +37,9 @@ static void lz4hc_destroy(struct zcomp_ctx *ctx) kfree(zctx); } =20 -static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx) +static int lz4hc_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) { + struct zcomp_params *params =3D zcomp->params; struct lz4hc_ctx *zctx; =20 zctx =3D kzalloc(sizeof(*zctx), GFP_KERNEL); diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_= lzo.c index 4c906beaae6b..78e611ea841e 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -15,7 +15,7 @@ static int lzo_setup_params(struct zcomp_params *params) return 0; } =20 -static int lzo_create(struct zcomp_params *params, struct zcomp_ctx *ctx) +static int lzo_create(struct zcomp *params, struct zcomp_ctx *ctx) { ctx->context =3D kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); if (!ctx->context) diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backe= nd_lzorle.c index 10640c96cbfc..b0ff72468ea8 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -15,7 +15,7 @@ static int lzorle_setup_params(struct zcomp_params *param= s) return 0; } =20 -static int lzorle_create(struct zcomp_params *params, struct zcomp_ctx *ct= x) +static int lzorle_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) { ctx->context =3D kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); if (!ctx->context) diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend= _zstd.c index 1184c0036f44..b73b975599f4 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -125,8 +125,9 @@ static void zstd_destroy(struct zcomp_ctx *ctx) kfree(zctx); } =20 -static int zstd_create(struct zcomp_params *params, struct zcomp_ctx *ctx) +static int zstd_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) { + struct zcomp_params *params =3D zcomp->params; struct zstd_ctx *zctx; zstd_parameters prm; size_t sz; diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index bb514403e305..be3a31f09344 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -54,7 +54,7 @@ static int zcomp_strm_init(struct zcomp *comp, struct zco= mp_strm *zstrm) { int ret; =20 - ret =3D comp->ops->create_ctx(comp->params, &zstrm->ctx); + ret =3D comp->ops->create_ctx(comp, &zstrm->ctx); if (ret) return ret; =20 diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index ad5762813842..89d32bb13f8c 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -45,13 +45,15 @@ struct zcomp_req { size_t dst_len; }; =20 +struct zcomp; + struct zcomp_ops { int (*compress)(struct zcomp_params *params, struct zcomp_ctx *ctx, struct zcomp_req *req); int (*decompress)(struct zcomp_params *params, struct zcomp_ctx *ctx, struct zcomp_req *req); =20 - int (*create_ctx)(struct zcomp_params *params, struct zcomp_ctx *ctx); + int (*create_ctx)(struct zcomp *zcomp, struct zcomp_ctx *ctx); void (*destroy_ctx)(struct zcomp_ctx *ctx); =20 int (*setup_params)(struct zcomp_params *params); --=20 2.34.1 From nobody Sat Nov 23 14:04:17 2024 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB7291C4A02; Tue, 19 Nov 2024 12:27:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.89.224.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019277; cv=none; b=hcVI9fAE1ufyM3B6puIdINglIBgIwwdl+sSPTgsiaCIaZTGqYKNeMMxrJKMEh2hK219mqbMqPH2X+9cg8h/go52+mBOknv0GCqmI9PPz/3olIRleT8HR0rcTk51l0gnbYOsA389wRR5G++GfOEl0JqZQfIFBIM1fhIgSdEfZhzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019277; c=relaxed/simple; bh=o2BPdw6hLYbIROxy4x7HqarbaLMJHgFW9LvbNuYBJ/U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RAZ6aptjbnSI+6a60aRzchF1BchjFDXUvDwvppYqSxm++4AedAg7ecxuRncztWRdEArAHKisdFOjtMaH7Mpt4SpX5N1JChVAT35vBxxaPU/l9cy6FJHQuO0KPVUSczLjmupawUtmzP6tIELcRaP9pV4tV+bCiOvTyndZf+j2UUs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com; spf=pass smtp.mailfrom=salutedevices.com; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b=SpaHxcD5; arc=none smtp.client-ip=45.89.224.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b="SpaHxcD5" Received: from p-infra-ksmg-sc-msk02.sberdevices.ru (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id D6FC512000D; Tue, 19 Nov 2024 15:27:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru D6FC512000D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1732019264; bh=fKiBH4qBVxO9XGXPLT4rNEeVQlKxPf+r+nszEIDn/70=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=SpaHxcD5n3NKLTRO2oR7U4K5ysgU9qQyfbeuTMR+QvTL2ulmjBqi97DoBBZmweOYp qh9FIpFujQWxppQgqn+uNEwEYkbWDFbuH8Tq3MFUzPauO78/kiAmjmZbp3MnaqBqDw xLKLiNMg1k4BvvSZTM9cqfFDlQ9i/PAMMnkW7dR24PUBdFKzBjRkqS4LuvMVgla6DV 0vgw1DtWfwAJMBxUX9lEbNPJXO5Lc29gCHuZ+YjwtK2kDO+AUGcqExK4yvtdR5kwus DAQwc0RH32Ct3sSuRb7i89FgC6hrmmaQys2+WCucVpjs+/pozHR8XgsvVmrRlZhde3 QQyvePPCS731Q== Received: from smtp.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Tue, 19 Nov 2024 15:27:44 +0300 (MSK) From: Alexey Romanov To: , , , CC: , , , Alexey Romanov Subject: [PATCH v1 2/3] zram: store crypto backends in list instead of array Date: Tue, 19 Nov 2024 15:27:12 +0300 Message-ID: <20241119122713.3294173-3-avromanov@salutedevices.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119122713.3294173-1-avromanov@salutedevices.com> References: <20241119122713.3294173-1-avromanov@salutedevices.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: p-i-exch-a-m1.sberdevices.ru (172.24.196.116) To p-i-exch-a-m1.sberdevices.ru (172.24.196.116) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 189267 [Nov 19 2024] X-KSMG-AntiSpam-Version: 6.1.1.7 X-KSMG-AntiSpam-Envelope-From: avromanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 41 0.3.41 623e98d5198769c015c72f45fabbb9f77bdb702b, {Tracking_from_domain_doesnt_match_to}, salutedevices.com:7.1.1;smtp.sberdevices.ru:5.0.1,7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2, FromAlignment: s X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/11/19 08:41:00 #26886618 X-KSMG-AntiVirus-Status: Clean, skipped Content-Type: text/plain; charset="utf-8" This approach allows us to add backends in runtime. We will use this in next patches. Signed-off-by: Alexey Romanov --- drivers/block/zram/backend_842.c | 12 ++- drivers/block/zram/backend_842.h | 2 +- drivers/block/zram/backend_deflate.c | 12 ++- drivers/block/zram/backend_deflate.h | 2 +- drivers/block/zram/backend_lz4.c | 12 ++- drivers/block/zram/backend_lz4.h | 2 +- drivers/block/zram/backend_lz4hc.c | 12 ++- drivers/block/zram/backend_lz4hc.h | 2 +- drivers/block/zram/backend_lzo.c | 12 ++- drivers/block/zram/backend_lzo.h | 2 +- drivers/block/zram/backend_lzorle.c | 12 ++- drivers/block/zram/backend_lzorle.h | 2 +- drivers/block/zram/backend_zstd.c | 12 ++- drivers/block/zram/backend_zstd.h | 2 +- drivers/block/zram/zcomp.c | 124 ++++++++++++++++++--------- drivers/block/zram/zcomp.h | 7 ++ drivers/block/zram/zram_drv.c | 7 ++ 17 files changed, 180 insertions(+), 56 deletions(-) diff --git a/drivers/block/zram/backend_842.c b/drivers/block/zram/backend_= 842.c index 9147feb1e994..3b91b43888af 100644 --- a/drivers/block/zram/backend_842.c +++ b/drivers/block/zram/backend_842.c @@ -50,12 +50,22 @@ static int decompress_842(struct zcomp_params *params, = struct zcomp_ctx *ctx, return sw842_decompress(req->src, req->src_len, req->dst, &dlen); } =20 -const struct zcomp_ops backend_842 =3D { +static void destroy_ops_842(struct zcomp_ops *backend_842) +{ +} + +struct zcomp_ops backend_842 =3D { .compress =3D compress_842, .decompress =3D decompress_842, .create_ctx =3D create_842, .destroy_ctx =3D destroy_842, .setup_params =3D setup_params_842, .release_params =3D release_params_842, + .destroy =3D destroy_ops_842, .name =3D "842", }; + +struct zcomp_ops *get_backend_842(void) +{ + return &backend_842; +} diff --git a/drivers/block/zram/backend_842.h b/drivers/block/zram/backend_= 842.h index 4dc85c188799..9cdc59b0faa9 100644 --- a/drivers/block/zram/backend_842.h +++ b/drivers/block/zram/backend_842.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_842; +struct zcomp_ops *get_backend_842(void); =20 #endif /* __BACKEND_842_H__ */ diff --git a/drivers/block/zram/backend_deflate.c b/drivers/block/zram/back= end_deflate.c index 10fde82dc5e7..c1460ab04b24 100644 --- a/drivers/block/zram/backend_deflate.c +++ b/drivers/block/zram/backend_deflate.c @@ -136,12 +136,22 @@ static int deflate_decompress(struct zcomp_params *pa= rams, return 0; } =20 -const struct zcomp_ops backend_deflate =3D { +static void deflate_destroy_ops(struct zcomp_ops *backend_delfate) +{ +} + +struct zcomp_ops backend_deflate =3D { .compress =3D deflate_compress, .decompress =3D deflate_decompress, .create_ctx =3D deflate_create, .destroy_ctx =3D deflate_destroy, .setup_params =3D deflate_setup_params, .release_params =3D deflate_release_params, + .destroy =3D deflate_destroy_ops, .name =3D "deflate", }; + +struct zcomp_ops *get_backend_deflate(void) +{ + return &backend_deflate; +} diff --git a/drivers/block/zram/backend_deflate.h b/drivers/block/zram/back= end_deflate.h index a39ac12b114c..c15b18012aeb 100644 --- a/drivers/block/zram/backend_deflate.h +++ b/drivers/block/zram/backend_deflate.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_deflate; +struct zcomp_ops *get_backend_deflate(void); =20 #endif /* __BACKEND_DEFLATE_H__ */ diff --git a/drivers/block/zram/backend_lz4.c b/drivers/block/zram/backend_= lz4.c index 8f7c8f16b6ce..5638eefa657a 100644 --- a/drivers/block/zram/backend_lz4.c +++ b/drivers/block/zram/backend_lz4.c @@ -117,12 +117,22 @@ static int lz4_decompress(struct zcomp_params *params= , struct zcomp_ctx *ctx, return 0; } =20 -const struct zcomp_ops backend_lz4 =3D { +static void lz4_destroy_ops(struct zcomp_ops *backend_lz4) +{ +} + +struct zcomp_ops backend_lz4 =3D { .compress =3D lz4_compress, .decompress =3D lz4_decompress, .create_ctx =3D lz4_create, .destroy_ctx =3D lz4_destroy, .setup_params =3D lz4_setup_params, .release_params =3D lz4_release_params, + .destroy =3D lz4_destroy_ops, .name =3D "lz4", }; + +struct zcomp_ops *get_backend_lz4(void) +{ + return &backend_lz4; +} diff --git a/drivers/block/zram/backend_lz4.h b/drivers/block/zram/backend_= lz4.h index c11fa602a703..439f96222ee5 100644 --- a/drivers/block/zram/backend_lz4.h +++ b/drivers/block/zram/backend_lz4.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_lz4; +struct zcomp_ops *get_backend_lz4(void); =20 #endif /* __BACKEND_LZ4_H__ */ diff --git a/drivers/block/zram/backend_lz4hc.c b/drivers/block/zram/backen= d_lz4hc.c index b0302b8027ab..c502b6afd571 100644 --- a/drivers/block/zram/backend_lz4hc.c +++ b/drivers/block/zram/backend_lz4hc.c @@ -118,12 +118,22 @@ static int lz4hc_decompress(struct zcomp_params *para= ms, struct zcomp_ctx *ctx, return 0; } =20 -const struct zcomp_ops backend_lz4hc =3D { +static void lz4hc_destroy_ops(struct zcomp_ops *backend_lz4hc) +{ +} + +struct zcomp_ops backend_lz4hc =3D { .compress =3D lz4hc_compress, .decompress =3D lz4hc_decompress, .create_ctx =3D lz4hc_create, .destroy_ctx =3D lz4hc_destroy, .setup_params =3D lz4hc_setup_params, .release_params =3D lz4hc_release_params, + .destroy =3D lz4hc_destroy_ops, .name =3D "lz4hc", }; + +struct zcomp_ops *get_backend_lz4hc(void) +{ + return &backend_lz4hc; +} diff --git a/drivers/block/zram/backend_lz4hc.h b/drivers/block/zram/backen= d_lz4hc.h index 6de03551ed4d..1334519082bb 100644 --- a/drivers/block/zram/backend_lz4hc.h +++ b/drivers/block/zram/backend_lz4hc.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_lz4hc; +struct zcomp_ops *get_backend_lz4hc(void); =20 #endif /* __BACKEND_LZ4HC_H__ */ diff --git a/drivers/block/zram/backend_lzo.c b/drivers/block/zram/backend_= lzo.c index 78e611ea841e..88eab940f723 100644 --- a/drivers/block/zram/backend_lzo.c +++ b/drivers/block/zram/backend_lzo.c @@ -48,12 +48,22 @@ static int lzo_decompress(struct zcomp_params *params, = struct zcomp_ctx *ctx, return ret =3D=3D LZO_E_OK ? 0 : ret; } =20 -const struct zcomp_ops backend_lzo =3D { +static void lzo_destroy_ops(struct zcomp_ops *backend_lzo) +{ +} + +struct zcomp_ops backend_lzo =3D { .compress =3D lzo_compress, .decompress =3D lzo_decompress, .create_ctx =3D lzo_create, .destroy_ctx =3D lzo_destroy, .setup_params =3D lzo_setup_params, .release_params =3D lzo_release_params, + .destroy =3D lzo_destroy_ops, .name =3D "lzo", }; + +struct zcomp_ops *get_backend_lzo(void) +{ + return &backend_lzo; +} diff --git a/drivers/block/zram/backend_lzo.h b/drivers/block/zram/backend_= lzo.h index 93d54749e63c..21493591dec3 100644 --- a/drivers/block/zram/backend_lzo.h +++ b/drivers/block/zram/backend_lzo.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_lzo; +struct zcomp_ops *get_backend_lzo(void); =20 #endif /* __BACKEND_LZO_H__ */ diff --git a/drivers/block/zram/backend_lzorle.c b/drivers/block/zram/backe= nd_lzorle.c index b0ff72468ea8..28837acd205c 100644 --- a/drivers/block/zram/backend_lzorle.c +++ b/drivers/block/zram/backend_lzorle.c @@ -48,12 +48,22 @@ static int lzorle_decompress(struct zcomp_params *param= s, struct zcomp_ctx *ctx, return ret =3D=3D LZO_E_OK ? 0 : ret; } =20 -const struct zcomp_ops backend_lzorle =3D { +static void lzorle_destroy_ops(struct zcomp_ops *backend_lzorle) +{ +} + +struct zcomp_ops backend_lzorle =3D { .compress =3D lzorle_compress, .decompress =3D lzorle_decompress, .create_ctx =3D lzorle_create, .destroy_ctx =3D lzorle_destroy, .setup_params =3D lzorle_setup_params, .release_params =3D lzorle_release_params, + .destroy =3D lzorle_destroy_ops, .name =3D "lzo-rle", }; + +struct zcomp_ops *get_backend_lzorle(void) +{ + return &backend_lzorle; +} diff --git a/drivers/block/zram/backend_lzorle.h b/drivers/block/zram/backe= nd_lzorle.h index 6ecb163b09f1..7871f44b73a3 100644 --- a/drivers/block/zram/backend_lzorle.h +++ b/drivers/block/zram/backend_lzorle.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_lzorle; +struct zcomp_ops *get_backend_lzorle(void); =20 #endif /* __BACKEND_LZORLE_H__ */ diff --git a/drivers/block/zram/backend_zstd.c b/drivers/block/zram/backend= _zstd.c index b73b975599f4..0762bea90296 100644 --- a/drivers/block/zram/backend_zstd.c +++ b/drivers/block/zram/backend_zstd.c @@ -216,12 +216,22 @@ static int zstd_decompress(struct zcomp_params *param= s, struct zcomp_ctx *ctx, return 0; } =20 -const struct zcomp_ops backend_zstd =3D { +static void zstd_destroy_ops(struct zcomp_ops *backend_zstd) +{ +} + +struct zcomp_ops backend_zstd =3D { .compress =3D zstd_compress, .decompress =3D zstd_decompress, .create_ctx =3D zstd_create, .destroy_ctx =3D zstd_destroy, .setup_params =3D zstd_setup_params, .release_params =3D zstd_release_params, + .destroy =3D zstd_destroy_ops, .name =3D "zstd", }; + +struct zcomp_ops *get_backend_zstd(void) +{ + return &backend_zstd; +} diff --git a/drivers/block/zram/backend_zstd.h b/drivers/block/zram/backend= _zstd.h index 10fdfff1ec1c..f737fbdfa57a 100644 --- a/drivers/block/zram/backend_zstd.h +++ b/drivers/block/zram/backend_zstd.h @@ -5,6 +5,6 @@ =20 #include "zcomp.h" =20 -extern const struct zcomp_ops backend_zstd; +struct zcomp_ops *get_backend_zstd(void); =20 #endif /* __BACKEND_ZSTD_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index be3a31f09344..40b5ab4c598b 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -9,6 +9,7 @@ #include #include #include +#include =20 #include "zcomp.h" =20 @@ -20,28 +21,7 @@ #include "backend_deflate.h" #include "backend_842.h" =20 -static const struct zcomp_ops *backends[] =3D { -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) - &backend_lzorle, - &backend_lzo, -#endif -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) - &backend_lz4, -#endif -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) - &backend_lz4hc, -#endif -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) - &backend_zstd, -#endif -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) - &backend_deflate, -#endif -#if IS_ENABLED(CONFIG_ZRAM_BACKEND_842) - &backend_842, -#endif - NULL -}; +static LIST_HEAD(backends); =20 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm) { @@ -72,14 +52,13 @@ static int zcomp_strm_init(struct zcomp *comp, struct z= comp_strm *zstrm) =20 static const struct zcomp_ops *lookup_backend_ops(const char *comp) { - int i =3D 0; + struct zcomp_ops *backend; =20 - while (backends[i]) { - if (sysfs_streq(comp, backends[i]->name)) - break; - i++; - } - return backends[i]; + list_for_each_entry(backend, &backends, list) + if (sysfs_streq(comp, backend->name)) + return backend; + + return NULL; } =20 bool zcomp_available_algorithm(const char *comp) @@ -91,15 +70,15 @@ bool zcomp_available_algorithm(const char *comp) ssize_t zcomp_available_show(const char *comp, char *buf) { ssize_t sz =3D 0; - int i; + struct zcomp_ops *backend; =20 - for (i =3D 0; i < ARRAY_SIZE(backends) - 1; i++) { - if (!strcmp(comp, backends[i]->name)) { + list_for_each_entry(backend, &backends, list) { + if (!strcmp(comp, backend->name)) { sz +=3D scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "[%s] ", backends[i]->name); + "[%s] ", backend->name); } else { sz +=3D scnprintf(buf + sz, PAGE_SIZE - sz - 2, - "%s ", backends[i]->name); + "%s ", backend->name); } } =20 @@ -211,14 +190,6 @@ struct zcomp *zcomp_create(const char *alg, struct zco= mp_params *params) struct zcomp *comp; int error; =20 - /* - * The backends array has a sentinel NULL value, so the minimum - * size is 1. In order to be valid the array, apart from the - * sentinel NULL element, should have at least one compression - * backend selected. - */ - BUILD_BUG_ON(ARRAY_SIZE(backends) <=3D 1); - comp =3D kzalloc(sizeof(struct zcomp), GFP_KERNEL); if (!comp) return ERR_PTR(-ENOMEM); @@ -236,3 +207,72 @@ struct zcomp *zcomp_create(const char *alg, struct zco= mp_params *params) } return comp; } + +void clean_zcomp_backends(void) +{ + struct zcomp_ops *backend; + + list_for_each_entry(backend, &backends, list) + backend->destroy(backend); +} + +int init_zcomp_backends(void) +{ + struct zcomp_ops *ops; + +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) + ops =3D get_backend_lzorle(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); + + ops =3D get_backend_lzo(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) + ops =3D get_backend_lz4(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) + ops =3D get_backend_lz4hc(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) + ops =3D get_backend_zstd(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) + ops =3D get_backend_deflate(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_842) + ops =3D get_backend_842(); + if (IS_ERR_OR_NULL(ops)) + goto err; + + list_add(&ops->list, &backends); +#endif + + return 0; + +err: + clean_zcomp_backends(); + + return PTR_ERR(ops); +} diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h index 89d32bb13f8c..12d82acd0d39 100644 --- a/drivers/block/zram/zcomp.h +++ b/drivers/block/zram/zcomp.h @@ -59,7 +59,11 @@ struct zcomp_ops { int (*setup_params)(struct zcomp_params *params); void (*release_params)(struct zcomp_params *params); =20 + void (*destroy)(struct zcomp_ops *ops); + const char *name; + + struct list_head list; }; =20 /* dynamic per-device compression frontend */ @@ -86,4 +90,7 @@ int zcomp_compress(struct zcomp *comp, struct zcomp_strm = *zstrm, int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm, const void *src, unsigned int src_len, void *dst); =20 +void clean_zcomp_backends(void); +int init_zcomp_backends(void); + #endif /* _ZCOMP_H_ */ diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index cee49bb0126d..29df68e0e450 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -2726,6 +2726,7 @@ static void destroy_devices(void) idr_destroy(&zram_index_idr); unregister_blkdev(zram_major, "zram"); cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); + clean_zcomp_backends(); } =20 static int __init zram_init(void) @@ -2765,6 +2766,12 @@ static int __init zram_init(void) num_devices--; } =20 + ret =3D init_zcomp_backends(); + if (ret) { + pr_err("Unable to create zcomp devices\n"); + goto out_error; + } + return 0; =20 out_error: --=20 2.34.1 From nobody Sat Nov 23 14:04:17 2024 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB7861C4A30; Tue, 19 Nov 2024 12:27:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.89.224.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019276; cv=none; b=BPBBhA8CMWiHCVoLgt1A+/efuFQg17sQOc8VbNbkaeDeiLo9FDyqf0L7+8rOPhqY7igRa/8VGa685mQuNjp6ncSwblx7pMqSQ/9XgLbVLWUEg/ehLBp5DsoRSYXQgN+SB5L9QvHpHKDHQidIIUrVl6pPaTljNEWS0ZB0Qc3kWZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732019276; c=relaxed/simple; bh=D42lwy9xTWB8QZAX6iLYR+0ZcB03TZzQs8+MPtuypKU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=s/HheSre7B4nL4ASLSaw+oerKGxBKfWQBqID2Vy1U3UpkcRDTh1wsf5MFOgJFZQBHVegBEVSBesV/XLjACUNgIfm1D6y0nl5K1CKRRK+uFLYu+zehyry0D3lpXrwKADTLB1B0nHKO8ow3lieuA7DTX8JFwOrWPxBLlqDyni7gCg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com; spf=pass smtp.mailfrom=salutedevices.com; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b=fZGGKIkF; arc=none smtp.client-ip=45.89.224.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b="fZGGKIkF" Received: from p-infra-ksmg-sc-msk02.sberdevices.ru (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 915A012000E; Tue, 19 Nov 2024 15:27:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 915A012000E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1732019265; bh=QPcceA5eUH/BK4VLP8TE3Yj5nWTGC8/LNbJPjG20oq4=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=fZGGKIkFMcs3xCmuEZD/WImBrSaAS270UESsScgWk43gHnJTOTpil62Zsb29Ay7T5 gmsj62R2+gRNsp+r7x/LBqkUXnbdmG87mhqFvAzeBYOHTDSkTfhQL+zAYFd391gLS5 eXxySujWc8n1BnR27PWG6cwDIDV63lfpyAHnfdjpWHUbwCQeMy8mpvh1V8Hq8Mkrm+ 6tPjBUI6AWl8jaYEFJ2ldYG9NqDnuPBbrXVsmlnaNG1Sxc3WbcHqaBLTwXqMLUTHwn LgVBa4bLCZWeK7ftG6txaubnvAJo2OA8R56Ua+R1adSx0silJVTfCHAgeimazbTFfS FRnNUO1JvcSPA== Received: from smtp.sberdevices.ru (p-i-exch-sc-m02.sberdevices.ru [172.16.192.103]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Tue, 19 Nov 2024 15:27:45 +0300 (MSK) From: Alexey Romanov To: , , , CC: , , , Alexey Romanov Subject: [PATCH v1 3/3] zram: introduce crypto-api backend Date: Tue, 19 Nov 2024 15:27:13 +0300 Message-ID: <20241119122713.3294173-4-avromanov@salutedevices.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241119122713.3294173-1-avromanov@salutedevices.com> References: <20241119122713.3294173-1-avromanov@salutedevices.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: p-i-exch-a-m1.sberdevices.ru (172.24.196.116) To p-i-exch-a-m1.sberdevices.ru (172.24.196.116) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 189267 [Nov 19 2024] X-KSMG-AntiSpam-Version: 6.1.1.7 X-KSMG-AntiSpam-Envelope-From: avromanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 41 0.3.41 623e98d5198769c015c72f45fabbb9f77bdb702b, {Tracking_from_domain_doesnt_match_to}, salutedevices.com:7.1.1;smtp.sberdevices.ru:5.0.1,7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2, FromAlignment: s X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/11/19 08:41:00 #26886618 X-KSMG-AntiVirus-Status: Clean, skipped Content-Type: text/plain; charset="utf-8" Since we use custom backend implementation, we remove the ability for users to use algorithms from crypto backend. This breaks backward compatibility, user doesn't necessarily use one of the algorithms from "custom" backends defined in zram folder. For example, he can use some driver with hardware compression support. This patch adds opinion to enable Crypto API: add ZRAM_BACKEND_CRYPTO_API. Option is enabled by default, because in previously version of ZRAM it was possible to choose any alogirthm using Crypto API. This is also done for backward compatibility purposes. Signed-off-by: Alexey Romanov --- drivers/block/zram/Kconfig | 10 ++ drivers/block/zram/Makefile | 1 + drivers/block/zram/backend_crypto_api.c | 117 ++++++++++++++++++++++++ drivers/block/zram/backend_crypto_api.h | 10 ++ drivers/block/zram/zcomp.c | 73 +++++++++++++-- 5 files changed, 203 insertions(+), 8 deletions(-) create mode 100644 drivers/block/zram/backend_crypto_api.c create mode 100644 drivers/block/zram/backend_crypto_api.h diff --git a/drivers/block/zram/Kconfig b/drivers/block/zram/Kconfig index 6aea609b795c..672578040912 100644 --- a/drivers/block/zram/Kconfig +++ b/drivers/block/zram/Kconfig @@ -57,6 +57,16 @@ config ZRAM_BACKEND_LZO select LZO_COMPRESS select LZO_DECOMPRESS =20 +config ZRAM_BACKEND_CRYPTO_API + bool "Enable support for compression algorithms available in Crypto API" + depends on ZRAM + default y + select CRYPTO + help + If you still want to use Crypto API as a backend, enable this option. + All compression algorithms enabled on your system will be available in = ZRAM. + This option is useful if you are using hardware compression using any d= river. + choice prompt "Default zram compressor" default ZRAM_DEF_COMP_LZORLE diff --git a/drivers/block/zram/Makefile b/drivers/block/zram/Makefile index 0fdefd576691..d12d03a01f35 100644 --- a/drivers/block/zram/Makefile +++ b/drivers/block/zram/Makefile @@ -8,5 +8,6 @@ zram-$(CONFIG_ZRAM_BACKEND_LZ4HC) +=3D backend_lz4hc.o zram-$(CONFIG_ZRAM_BACKEND_ZSTD) +=3D backend_zstd.o zram-$(CONFIG_ZRAM_BACKEND_DEFLATE) +=3D backend_deflate.o zram-$(CONFIG_ZRAM_BACKEND_842) +=3D backend_842.o +zram-$(CONFIG_ZRAM_BACKEND_CRYPTO_API) +=3D backend_crypto_api.o =20 obj-$(CONFIG_ZRAM) +=3D zram.o diff --git a/drivers/block/zram/backend_crypto_api.c b/drivers/block/zram/b= ackend_crypto_api.c new file mode 100644 index 000000000000..1cd792b77aac --- /dev/null +++ b/drivers/block/zram/backend_crypto_api.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include "backend_crypto_api.h" + +struct crypto_api_ctx { + struct crypto_comp *tfm; +}; + +extern struct list_head crypto_alg_list; + +static void crypto_api_release_params(struct zcomp_params *params) +{ +} + +static int crypto_api_setup_params(struct zcomp_params *params) +{ + return 0; +} + +static int crypto_api_create(struct zcomp *zcomp, struct zcomp_ctx *ctx) +{ + struct crypto_api_ctx *crypto_ctx; + const char *algname =3D zcomp->ops->name; + + crypto_ctx =3D kzalloc(sizeof(*crypto_ctx), GFP_KERNEL); + if (!crypto_ctx) + return -ENOMEM; + + crypto_ctx->tfm =3D crypto_alloc_comp(algname, 0, 0); + if (IS_ERR_OR_NULL(crypto_ctx->tfm)) { + kfree(crypto_ctx); + return -ENOMEM; + } + + ctx->context =3D crypto_ctx; + + return 0; +} + +static void crypto_api_destroy(struct zcomp_ctx *ctx) +{ + struct crypto_api_ctx *crypto_ctx =3D ctx->context; + + if (!IS_ERR_OR_NULL(crypto_ctx->tfm)) + crypto_free_comp(crypto_ctx->tfm); + + kfree(crypto_ctx); +} + +static int crypto_api_compress(struct zcomp_params *params, struct zcomp_c= tx *ctx, + struct zcomp_req *req) +{ + struct crypto_api_ctx *crypto_ctx =3D ctx->context; + unsigned int dst_len =3D req->dst_len; + int ret; + + ret =3D crypto_comp_compress(crypto_ctx->tfm, + req->src, req->src_len, + req->dst, &dst_len); + + req->dst_len =3D dst_len; + + return ret; +} + +static int crypto_api_decompress(struct zcomp_params *params, struct zcomp= _ctx *ctx, + struct zcomp_req *req) +{ + struct crypto_api_ctx *crypto_ctx =3D ctx->context; + unsigned int dst_len =3D req->dst_len; + int ret; + + ret =3D crypto_comp_decompress(crypto_ctx->tfm, + req->src, req->src_len, + req->dst, &dst_len); + + req->dst_len =3D dst_len; + + return ret; +} + +static void crypto_api_destroy_ops(struct zcomp_ops *ops) +{ + kfree(ops->name); + kfree(ops); +} + +struct zcomp_ops *get_backend_crypto_api(const char *name) +{ + struct zcomp_ops *ops; + char *algname; + + ops =3D kmalloc(sizeof(*ops), GFP_KERNEL); + if (!ops) + return ERR_PTR(-ENOMEM); + + algname =3D kstrdup(name, GFP_KERNEL); + if (!algname) { + kfree(ops); + return ERR_PTR(-ENOMEM); + } + + ops->compress =3D crypto_api_compress; + ops->decompress =3D crypto_api_decompress, + ops->create_ctx =3D crypto_api_create, + ops->destroy_ctx =3D crypto_api_destroy, + ops->setup_params =3D crypto_api_setup_params, + ops->release_params =3D crypto_api_release_params, + ops->destroy =3D crypto_api_destroy_ops, + ops->name =3D algname; + + return ops; +} diff --git a/drivers/block/zram/backend_crypto_api.h b/drivers/block/zram/b= ackend_crypto_api.h new file mode 100644 index 000000000000..5ff8f75efdb4 --- /dev/null +++ b/drivers/block/zram/backend_crypto_api.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef __BACKEND_CRYPTO_API_H__ +#define __BACKEND_CRYPTO_API_H__ + +#include "zcomp.h" + +struct zcomp_ops *get_backend_crypto_api(const char *name); + +#endif /* __BACKEND_CRYPTO_API_H__ */ diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c index 40b5ab4c598b..da4b370c31c1 100644 --- a/drivers/block/zram/zcomp.c +++ b/drivers/block/zram/zcomp.c @@ -20,6 +20,9 @@ #include "backend_zstd.h" #include "backend_deflate.h" #include "backend_842.h" +#include "backend_crypto_api.h" + +extern struct list_head crypto_alg_list; =20 static LIST_HEAD(backends); =20 @@ -216,63 +219,117 @@ void clean_zcomp_backends(void) backend->destroy(backend); } =20 +static bool backend_enabled(const char *name) +{ + struct zcomp_ops *backend; + + list_for_each_entry(backend, &backends, list) + if (!strcmp(backend->name, name)) + return true; + + return false; +} + +static int init_crypto_api_backends(void) +{ + struct crypto_alg *alg; + struct zcomp_ops *ops; + + list_for_each_entry(alg, &crypto_alg_list, cra_list) { + if (!crypto_has_comp(alg->cra_name, 0, 0)) + continue; + + if (backend_enabled(alg->cra_name)) + continue; + + ops =3D get_backend_crypto_api(alg->cra_name); + if (IS_ERR_OR_NULL(ops)) + return PTR_ERR(ops); + + list_add(&ops->list, &backends); + } + + return 0; +} + int init_zcomp_backends(void) { struct zcomp_ops *ops; + int ret; =20 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO) ops =3D get_backend_lzorle(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); =20 ops =3D get_backend_lzo(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4) ops =3D get_backend_lz4(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC) ops =3D get_backend_lz4hc(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD) ops =3D get_backend_zstd(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE) ops =3D get_backend_deflate(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif #if IS_ENABLED(CONFIG_ZRAM_BACKEND_842) ops =3D get_backend_842(); - if (IS_ERR_OR_NULL(ops)) + if (IS_ERR_OR_NULL(ops)) { + ret =3D PTR_ERR(ops); goto err; + } =20 list_add(&ops->list, &backends); #endif =20 +#if IS_ENABLED(CONFIG_ZRAM_BACKEND_CRYPTO_API) + ret =3D init_crypto_api_backends(); + if (ret) + goto err; +#endif + return 0; =20 err: clean_zcomp_backends(); =20 - return PTR_ERR(ops); + return ret; } --=20 2.34.1