RE: [PATCH v2] mm/zswap: move to use crypto_acomp API for hardware acceleration
> -Original Message- > From: Vitaly Wool [mailto:vitaly.w...@konsulko.com] > Sent: Sunday, June 21, 2020 11:16 PM > To: Song Bao Hua (Barry Song) > Cc: Andrew Morton ; > herb...@gondor.apana.org.au; da...@davemloft.net; > linux-cry...@vger.kernel.org; Linux-MM ; LKML > ; Linuxarm ; Luis > Claudio R . Goncalves ; Sebastian Andrzej Siewior > ; Mahipal Challa ; > Seth Jennings ; Dan Streetman ; > Wangzhou (B) > Subject: Re: [PATCH v2] mm/zswap: move to use crypto_acomp API for > hardware acceleration > > On Sun, Jun 21, 2020 at 1:52 AM Barry Song > wrote: > > > > right now, all new ZIP drivers are using crypto_acomp APIs rather than > > legacy crypto_comp APIs. But zswap.c is still using the old APIs. That > > means zswap won't be able to use any new zip drivers in kernel. > > > > This patch moves to use cryto_acomp APIs to fix the problem. On the > > other hand, tradiontal compressors like lz4,lzo etc have been wrapped > > into acomp via scomp backend. So platforms without async compressors > > can fallback to use acomp via scomp backend. > > > > Cc: Luis Claudio R. Goncalves > > Cc: Sebastian Andrzej Siewior > > Cc: Andrew Morton > > Cc: Herbert Xu > > Cc: David S. Miller > > Cc: Mahipal Challa > > Cc: Seth Jennings > > Cc: Dan Streetman > > Cc: Vitaly Wool > > Cc: Zhou Wang > > Signed-off-by: Barry Song > > --- > > -v2: > > rebase to 5.8-rc1; > > cleanup commit log; > > cleanup to improve the readability according to Sebastian's comment > > > > mm/zswap.c | 153 > > ++--- > > 1 file changed, 110 insertions(+), 43 deletions(-) > > > > diff --git a/mm/zswap.c b/mm/zswap.c > > index fbb782924ccc..0d914ba6b4a0 100644 > > --- a/mm/zswap.c > > +++ b/mm/zswap.c > > @@ -24,8 +24,10 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > +#include > > > > #include > > #include > > @@ -127,9 +129,17 @@ > module_param_named(same_filled_pages_enabled, > > zswap_same_filled_pages_enabled, > > * data structures > > **/ > > > > +struct crypto_acomp_ctx { > > + struct crypto_acomp *acomp; > > + struct acomp_req *req; > > + struct crypto_wait wait; > > + u8 *dstmem; > > + struct mutex mutex; > > +}; > > + > > struct zswap_pool { > > struct zpool *zpool; > > - struct crypto_comp * __percpu *tfm; > > + struct crypto_acomp_ctx * __percpu *acomp_ctx; > > struct kref kref; > > struct list_head list; > > struct work_struct release_work; @@ -415,30 +425,60 @@ static > > int zswap_dstmem_dead(unsigned int cpu) static int > > zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) { > > struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, > node); > > - struct crypto_comp *tfm; > > + struct crypto_acomp *acomp; > > + struct acomp_req *req; > > + struct crypto_acomp_ctx *acomp_ctx; > > > > - if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu))) > > + if (WARN_ON(*per_cpu_ptr(pool->acomp_ctx, cpu))) > > return 0; > > > > - tfm = crypto_alloc_comp(pool->tfm_name, 0, 0); > > - if (IS_ERR_OR_NULL(tfm)) { > > - pr_err("could not alloc crypto comp %s : %ld\n", > > - pool->tfm_name, PTR_ERR(tfm)); > > + acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL); > > + if (IS_ERR_OR_NULL(acomp_ctx)) { > > + pr_err("Could not initialize acomp_ctx\n"); > > + return -ENOMEM; > > + } > > + acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0); > > + if (IS_ERR_OR_NULL(acomp)) { > > + pr_err("could not alloc crypto acomp %s : %ld\n", > > + pool->tfm_name, PTR_ERR(acomp)); > > return -ENOMEM; > > } > > I bet you actually want to free acomp_ctx here. Overall, could you please > provide more careful error path implementation or explain why it isn't > necessary? Oops. I could hardly believe my eyes. it is definitely necessary to free the allocated data struct here, will send an incremental patch to fix this. Thanks for your comment. Best Regards, Barry > > Best regards, > Vitaly
Re: [PATCH v2] mm/zswap: move to use crypto_acomp API for hardware acceleration
On Sun, Jun 21, 2020 at 1:52 AM Barry Song wrote: > > right now, all new ZIP drivers are using crypto_acomp APIs rather than > legacy crypto_comp APIs. But zswap.c is still using the old APIs. That > means zswap won't be able to use any new zip drivers in kernel. > > This patch moves to use cryto_acomp APIs to fix the problem. On the > other hand, tradiontal compressors like lz4,lzo etc have been wrapped > into acomp via scomp backend. So platforms without async compressors > can fallback to use acomp via scomp backend. > > Cc: Luis Claudio R. Goncalves > Cc: Sebastian Andrzej Siewior > Cc: Andrew Morton > Cc: Herbert Xu > Cc: David S. Miller > Cc: Mahipal Challa > Cc: Seth Jennings > Cc: Dan Streetman > Cc: Vitaly Wool > Cc: Zhou Wang > Signed-off-by: Barry Song > --- > -v2: > rebase to 5.8-rc1; > cleanup commit log; > cleanup to improve the readability according to Sebastian's comment > > mm/zswap.c | 153 ++--- > 1 file changed, 110 insertions(+), 43 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index fbb782924ccc..0d914ba6b4a0 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -24,8 +24,10 @@ > #include > #include > #include > +#include > #include > #include > +#include > > #include > #include > @@ -127,9 +129,17 @@ module_param_named(same_filled_pages_enabled, > zswap_same_filled_pages_enabled, > * data structures > **/ > > +struct crypto_acomp_ctx { > + struct crypto_acomp *acomp; > + struct acomp_req *req; > + struct crypto_wait wait; > + u8 *dstmem; > + struct mutex mutex; > +}; > + > struct zswap_pool { > struct zpool *zpool; > - struct crypto_comp * __percpu *tfm; > + struct crypto_acomp_ctx * __percpu *acomp_ctx; > struct kref kref; > struct list_head list; > struct work_struct release_work; > @@ -415,30 +425,60 @@ static int zswap_dstmem_dead(unsigned int cpu) > static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) > { > struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); > - struct crypto_comp *tfm; > + struct crypto_acomp *acomp; > + struct acomp_req *req; > + struct crypto_acomp_ctx *acomp_ctx; > > - if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu))) > + if (WARN_ON(*per_cpu_ptr(pool->acomp_ctx, cpu))) > return 0; > > - tfm = crypto_alloc_comp(pool->tfm_name, 0, 0); > - if (IS_ERR_OR_NULL(tfm)) { > - pr_err("could not alloc crypto comp %s : %ld\n", > - pool->tfm_name, PTR_ERR(tfm)); > + acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL); > + if (IS_ERR_OR_NULL(acomp_ctx)) { > + pr_err("Could not initialize acomp_ctx\n"); > + return -ENOMEM; > + } > + acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0); > + if (IS_ERR_OR_NULL(acomp)) { > + pr_err("could not alloc crypto acomp %s : %ld\n", > + pool->tfm_name, PTR_ERR(acomp)); > return -ENOMEM; > } I bet you actually want to free acomp_ctx here. Overall, could you please provide more careful error path implementation or explain why it isn't necessary? Best regards, Vitaly > - *per_cpu_ptr(pool->tfm, cpu) = tfm; > + acomp_ctx->acomp = acomp; > + > + req = acomp_request_alloc(acomp_ctx->acomp); > + if (IS_ERR_OR_NULL(req)) { > + pr_err("could not alloc crypto acomp %s : %ld\n", > + pool->tfm_name, PTR_ERR(acomp)); > + return -ENOMEM; > + } > + acomp_ctx->req = req; > + > + mutex_init(&acomp_ctx->mutex); > + crypto_init_wait(&acomp_ctx->wait); > + acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, > + crypto_req_done, &acomp_ctx->wait); > + > + acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu); > + *per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx; > + > return 0; > } > > static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) > { > struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); > - struct crypto_comp *tfm; > + struct crypto_acomp_ctx *acomp_ctx; > + > + acomp_ctx = *per_cpu_ptr(pool->acomp_ctx, cpu); > + if (!IS_ERR_OR_NULL(acomp_ctx)) { > + if (!IS_ERR_OR_NULL(acomp_ctx->req)) > + acomp_request_free(acomp_ctx->req); > + if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) > + crypto_free_acomp(acomp_ctx->acomp); > + kfree(acomp_ctx); > + } > + *per_cpu_ptr(pool->acomp_ctx, cpu) = NULL; > > - tfm = *per_cpu_ptr(pool->tfm, cpu); > - if (!IS_ERR_OR_NULL(tfm)) > - crypto_free_comp(tfm); > - *per_cpu_ptr(pool->tfm, cpu) = NULL; > retu
[PATCH v2] mm/zswap: move to use crypto_acomp API for hardware acceleration
right now, all new ZIP drivers are using crypto_acomp APIs rather than legacy crypto_comp APIs. But zswap.c is still using the old APIs. That means zswap won't be able to use any new zip drivers in kernel. This patch moves to use cryto_acomp APIs to fix the problem. On the other hand, tradiontal compressors like lz4,lzo etc have been wrapped into acomp via scomp backend. So platforms without async compressors can fallback to use acomp via scomp backend. Cc: Luis Claudio R. Goncalves Cc: Sebastian Andrzej Siewior Cc: Andrew Morton Cc: Herbert Xu Cc: David S. Miller Cc: Mahipal Challa Cc: Seth Jennings Cc: Dan Streetman Cc: Vitaly Wool Cc: Zhou Wang Signed-off-by: Barry Song --- -v2: rebase to 5.8-rc1; cleanup commit log; cleanup to improve the readability according to Sebastian's comment mm/zswap.c | 153 ++--- 1 file changed, 110 insertions(+), 43 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index fbb782924ccc..0d914ba6b4a0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -24,8 +24,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -127,9 +129,17 @@ module_param_named(same_filled_pages_enabled, zswap_same_filled_pages_enabled, * data structures **/ +struct crypto_acomp_ctx { + struct crypto_acomp *acomp; + struct acomp_req *req; + struct crypto_wait wait; + u8 *dstmem; + struct mutex mutex; +}; + struct zswap_pool { struct zpool *zpool; - struct crypto_comp * __percpu *tfm; + struct crypto_acomp_ctx * __percpu *acomp_ctx; struct kref kref; struct list_head list; struct work_struct release_work; @@ -415,30 +425,60 @@ static int zswap_dstmem_dead(unsigned int cpu) static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) { struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); - struct crypto_comp *tfm; + struct crypto_acomp *acomp; + struct acomp_req *req; + struct crypto_acomp_ctx *acomp_ctx; - if (WARN_ON(*per_cpu_ptr(pool->tfm, cpu))) + if (WARN_ON(*per_cpu_ptr(pool->acomp_ctx, cpu))) return 0; - tfm = crypto_alloc_comp(pool->tfm_name, 0, 0); - if (IS_ERR_OR_NULL(tfm)) { - pr_err("could not alloc crypto comp %s : %ld\n", - pool->tfm_name, PTR_ERR(tfm)); + acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL); + if (IS_ERR_OR_NULL(acomp_ctx)) { + pr_err("Could not initialize acomp_ctx\n"); + return -ENOMEM; + } + acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0); + if (IS_ERR_OR_NULL(acomp)) { + pr_err("could not alloc crypto acomp %s : %ld\n", + pool->tfm_name, PTR_ERR(acomp)); return -ENOMEM; } - *per_cpu_ptr(pool->tfm, cpu) = tfm; + acomp_ctx->acomp = acomp; + + req = acomp_request_alloc(acomp_ctx->acomp); + if (IS_ERR_OR_NULL(req)) { + pr_err("could not alloc crypto acomp %s : %ld\n", + pool->tfm_name, PTR_ERR(acomp)); + return -ENOMEM; + } + acomp_ctx->req = req; + + mutex_init(&acomp_ctx->mutex); + crypto_init_wait(&acomp_ctx->wait); + acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, + crypto_req_done, &acomp_ctx->wait); + + acomp_ctx->dstmem = per_cpu(zswap_dstmem, cpu); + *per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx; + return 0; } static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) { struct zswap_pool *pool = hlist_entry(node, struct zswap_pool, node); - struct crypto_comp *tfm; + struct crypto_acomp_ctx *acomp_ctx; + + acomp_ctx = *per_cpu_ptr(pool->acomp_ctx, cpu); + if (!IS_ERR_OR_NULL(acomp_ctx)) { + if (!IS_ERR_OR_NULL(acomp_ctx->req)) + acomp_request_free(acomp_ctx->req); + if (!IS_ERR_OR_NULL(acomp_ctx->acomp)) + crypto_free_acomp(acomp_ctx->acomp); + kfree(acomp_ctx); + } + *per_cpu_ptr(pool->acomp_ctx, cpu) = NULL; - tfm = *per_cpu_ptr(pool->tfm, cpu); - if (!IS_ERR_OR_NULL(tfm)) - crypto_free_comp(tfm); - *per_cpu_ptr(pool->tfm, cpu) = NULL; return 0; } @@ -561,8 +601,9 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) pr_debug("using %s zpool\n", zpool_get_type(pool->zpool)); strlcpy(pool->tfm_name, compressor, sizeof(pool->tfm_name)); - pool->tfm = alloc_percpu(struct crypto_comp *); - if (!pool->tfm) { + + pool->acomp_ctx = alloc_percpu(struct crypto_acomp_ctx *); + if (!pool->acomp_ctx) { pr_err("percpu alloc failed\n");