Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Herbert Xu <herbert@gondor.apana.org.au>, linux-crypto@vger.kernel.org
Cc: Danny Tsen <dtsen@linux.ibm.com>, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 5/5] crypto: cryptd - Remove pointless per-instance queue pointer
Date: Thu,  3 Dec 2026 00:03:33 -0800	[thread overview]
Message-ID: <20261203080333.145409-6-ebiggers@kernel.org> (raw)
In-Reply-To: <20261203080333.145409-1-ebiggers@kernel.org>

cryptd only has a single global per-CPU queue.

There's no need to wrap it in struct cryptd_queue, pass it into every
cryptd_create_*() function, store a pointer to it in every instance
context, and type-pun the instance context via struct
cryptd_instance_ctx in cryptd_get_queue() just to retrieve the same
global queue back out.  It's already available as a static variable.

Just use the global per-CPU queue directly instead.

That makes the *_instance_ctx wrapper structs no longer necessary.  So
remove those too and just use the spawn structs directly, like various
other templates already do.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 crypto/cryptd.c | 156 ++++++++++++++++--------------------------------
 1 file changed, 50 insertions(+), 106 deletions(-)

diff --git a/crypto/cryptd.c b/crypto/cryptd.c
index 7e871715117aa..3649a7750c65b 100644
--- a/crypto/cryptd.c
+++ b/crypto/cryptd.c
@@ -37,33 +37,11 @@ struct cryptd_cpu_queue {
 	struct work_struct work;
 };
 
-struct cryptd_queue {
-	/*
-	 * Protected by disabling BH to allow enqueueing from softinterrupt and
-	 * dequeuing from kworker (cryptd_queue_worker()).
-	 */
-	struct cryptd_cpu_queue __percpu *cpu_queue;
-};
-
-struct cryptd_instance_ctx {
-	struct crypto_spawn spawn;
-	struct cryptd_queue *queue;
-};
-
-struct skcipherd_instance_ctx {
-	struct crypto_skcipher_spawn spawn;
-	struct cryptd_queue *queue;
-};
-
-struct hashd_instance_ctx {
-	struct crypto_shash_spawn spawn;
-	struct cryptd_queue *queue;
-};
-
-struct aead_instance_ctx {
-	struct crypto_aead_spawn aead_spawn;
-	struct cryptd_queue *queue;
-};
+/*
+ * Protected by disabling BH to allow enqueueing from softinterrupt and
+ * dequeuing from kworker (cryptd_queue_worker()).
+ */
+static struct cryptd_cpu_queue __percpu *cryptd_queues;
 
 struct cryptd_skcipher_ctx {
 	struct crypto_skcipher *child;
@@ -93,17 +71,16 @@ struct cryptd_aead_request_ctx {
 
 static void cryptd_queue_worker(struct work_struct *work);
 
-static int cryptd_init_queue(struct cryptd_queue *queue,
-			     unsigned int max_cpu_qlen)
+static int cryptd_init_queue(unsigned int max_cpu_qlen)
 {
 	int cpu;
 	struct cryptd_cpu_queue *cpu_queue;
 
-	queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
-	if (!queue->cpu_queue)
+	cryptd_queues = alloc_percpu(struct cryptd_cpu_queue);
+	if (!cryptd_queues)
 		return -ENOMEM;
 	for_each_possible_cpu(cpu) {
-		cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
+		cpu_queue = per_cpu_ptr(cryptd_queues, cpu);
 		crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
 		INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
 		local_lock_init(&cpu_queue->bh_lock);
@@ -112,33 +89,32 @@ static int cryptd_init_queue(struct cryptd_queue *queue,
 	return 0;
 }
 
-static void cryptd_fini_queue(struct cryptd_queue *queue)
+static void cryptd_fini_queue(void)
 {
 	int cpu;
 	struct cryptd_cpu_queue *cpu_queue;
 
 	for_each_possible_cpu(cpu) {
-		cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
+		cpu_queue = per_cpu_ptr(cryptd_queues, cpu);
 		BUG_ON(cpu_queue->queue.qlen);
 	}
-	free_percpu(queue->cpu_queue);
+	free_percpu(cryptd_queues);
 }
 
-static int cryptd_enqueue_request(struct cryptd_queue *queue,
-				  struct crypto_async_request *request)
+static int cryptd_enqueue_request(struct crypto_async_request *request)
 {
 	int err;
 	struct cryptd_cpu_queue *cpu_queue;
 
 	local_bh_disable();
-	local_lock_nested_bh(&queue->cpu_queue->bh_lock);
-	cpu_queue = this_cpu_ptr(queue->cpu_queue);
+	local_lock_nested_bh(&cryptd_queues->bh_lock);
+	cpu_queue = this_cpu_ptr(cryptd_queues);
 	err = crypto_enqueue_request(&cpu_queue->queue, request);
 
 	if (err != -ENOSPC)
 		queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
 
-	local_unlock_nested_bh(&queue->cpu_queue->bh_lock);
+	local_unlock_nested_bh(&cryptd_queues->bh_lock);
 	local_bh_enable();
 
 	return err;
@@ -174,13 +150,6 @@ static void cryptd_queue_worker(struct work_struct *work)
 		queue_work(cryptd_wq, &cpu_queue->work);
 }
 
-static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
-{
-	struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
-	struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
-	return ictx->queue;
-}
-
 static u32 cryptd_mask(struct crypto_attr_type *algt)
 {
 	/* No point in cryptd wrapping an algorithm that's already async. */
@@ -289,17 +258,14 @@ static int cryptd_skcipher_enqueue(struct skcipher_request *req,
 				   crypto_completion_t compl)
 {
 	struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
-	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 	struct skcipher_request *subreq = &rctx->req;
-	struct cryptd_queue *queue;
 
-	queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
 	subreq->base.complete = req->base.complete;
 	subreq->base.data = req->base.data;
 	req->base.complete = compl;
 	req->base.data = req;
 
-	return cryptd_enqueue_request(queue, &req->base);
+	return cryptd_enqueue_request(&req->base);
 }
 
 static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
@@ -315,8 +281,7 @@ static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
 static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
 {
 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
-	struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
-	struct crypto_skcipher_spawn *spawn = &ictx->spawn;
+	struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
 	struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
 	struct crypto_skcipher *cipher;
 
@@ -340,36 +305,32 @@ static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
 
 static void cryptd_skcipher_free(struct skcipher_instance *inst)
 {
-	struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
-
-	crypto_drop_skcipher(&ctx->spawn);
+	crypto_drop_skcipher(skcipher_instance_ctx(inst));
 	kfree(inst);
 }
 
 static int cryptd_create_skcipher(struct crypto_template *tmpl,
 				  struct rtattr **tb,
-				  struct crypto_attr_type *algt,
-				  struct cryptd_queue *queue)
+				  struct crypto_attr_type *algt)
 {
-	struct skcipherd_instance_ctx *ctx;
+	struct crypto_skcipher_spawn *spawn;
 	struct skcipher_instance *inst;
 	struct skcipher_alg_common *alg;
 	u32 mask = cryptd_mask(algt);
 	int err;
 
-	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
+	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
 	if (!inst)
 		return -ENOMEM;
 
-	ctx = skcipher_instance_ctx(inst);
-	ctx->queue = queue;
+	spawn = skcipher_instance_ctx(inst);
 
-	err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
+	err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
 				   crypto_attr_alg_name(tb[1]), 0, mask);
 	if (err)
 		goto err_free_inst;
 
-	alg = crypto_spawn_skcipher_alg_common(&ctx->spawn);
+	alg = crypto_spawn_skcipher_alg_common(spawn);
 	err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
 	if (err)
 		goto err_free_inst;
@@ -402,8 +363,7 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
 static int cryptd_hash_init_tfm(struct crypto_ahash *tfm)
 {
 	struct ahash_instance *inst = ahash_alg_instance(tfm);
-	struct hashd_instance_ctx *ictx = ahash_instance_ctx(inst);
-	struct crypto_shash_spawn *spawn = &ictx->spawn;
+	struct crypto_shash_spawn *spawn = ahash_instance_ctx(inst);
 	struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
 	struct crypto_shash *hash;
 
@@ -441,16 +401,13 @@ static int cryptd_hash_enqueue(struct ahash_request *req,
 				crypto_completion_t compl)
 {
 	struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
-	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
-	struct cryptd_queue *queue =
-		cryptd_get_queue(crypto_ahash_tfm(tfm));
 
 	rctx->complete = req->base.complete;
 	rctx->data = req->base.data;
 	req->base.complete = compl;
 	req->base.data = req;
 
-	return cryptd_enqueue_request(queue, &req->base);
+	return cryptd_enqueue_request(&req->base);
 }
 
 static struct shash_desc *cryptd_hash_prepare(struct ahash_request *req,
@@ -602,34 +559,30 @@ static int cryptd_hash_import(struct ahash_request *req, const void *in)
 
 static void cryptd_hash_free(struct ahash_instance *inst)
 {
-	struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
-
-	crypto_drop_shash(&ctx->spawn);
+	crypto_drop_shash(ahash_instance_ctx(inst));
 	kfree(inst);
 }
 
 static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
-			      struct crypto_attr_type *algt,
-			      struct cryptd_queue *queue)
+			      struct crypto_attr_type *algt)
 {
-	struct hashd_instance_ctx *ctx;
+	struct crypto_shash_spawn *spawn;
 	struct ahash_instance *inst;
 	struct shash_alg *alg;
 	u32 mask = cryptd_mask(algt);
 	int err;
 
-	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
+	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
 	if (!inst)
 		return -ENOMEM;
 
-	ctx = ahash_instance_ctx(inst);
-	ctx->queue = queue;
+	spawn = ahash_instance_ctx(inst);
 
-	err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
+	err = crypto_grab_shash(spawn, ahash_crypto_instance(inst),
 				crypto_attr_alg_name(tb[1]), 0, mask);
 	if (err)
 		goto err_free_inst;
-	alg = crypto_spawn_shash_alg(&ctx->spawn);
+	alg = crypto_spawn_shash_alg(spawn);
 
 	err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
 	if (err)
@@ -748,15 +701,13 @@ static int cryptd_aead_enqueue(struct aead_request *req,
 				    crypto_completion_t compl)
 {
 	struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
-	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
-	struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
 	struct aead_request *subreq = &rctx->req;
 
 	subreq->base.complete = req->base.complete;
 	subreq->base.data = req->base.data;
 	req->base.complete = compl;
 	req->base.data = req;
-	return cryptd_enqueue_request(queue, &req->base);
+	return cryptd_enqueue_request(&req->base);
 }
 
 static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
@@ -772,8 +723,7 @@ static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
 static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
 {
 	struct aead_instance *inst = aead_alg_instance(tfm);
-	struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
-	struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
+	struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
 	struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
 	struct crypto_aead *cipher;
 
@@ -796,36 +746,32 @@ static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
 
 static void cryptd_aead_free(struct aead_instance *inst)
 {
-	struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
-
-	crypto_drop_aead(&ctx->aead_spawn);
+	crypto_drop_aead(aead_instance_ctx(inst));
 	kfree(inst);
 }
 
 static int cryptd_create_aead(struct crypto_template *tmpl,
-		              struct rtattr **tb,
-			      struct crypto_attr_type *algt,
-			      struct cryptd_queue *queue)
+			      struct rtattr **tb,
+			      struct crypto_attr_type *algt)
 {
-	struct aead_instance_ctx *ctx;
+	struct crypto_aead_spawn *spawn;
 	struct aead_instance *inst;
 	struct aead_alg *alg;
 	u32 mask = cryptd_mask(algt);
 	int err;
 
-	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
+	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
 	if (!inst)
 		return -ENOMEM;
 
-	ctx = aead_instance_ctx(inst);
-	ctx->queue = queue;
+	spawn = aead_instance_ctx(inst);
 
-	err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
+	err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
 			       crypto_attr_alg_name(tb[1]), 0, mask);
 	if (err)
 		goto err_free_inst;
 
-	alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
+	alg = crypto_spawn_aead_alg(spawn);
 	err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
 	if (err)
 		goto err_free_inst;
@@ -853,8 +799,6 @@ static int cryptd_create_aead(struct crypto_template *tmpl,
 	return err;
 }
 
-static struct cryptd_queue queue;
-
 static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
 {
 	struct crypto_attr_type *algt;
@@ -865,11 +809,11 @@ static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
 
 	switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
 	case CRYPTO_ALG_TYPE_LSKCIPHER:
-		return cryptd_create_skcipher(tmpl, tb, algt, &queue);
+		return cryptd_create_skcipher(tmpl, tb, algt);
 	case CRYPTO_ALG_TYPE_HASH:
-		return cryptd_create_hash(tmpl, tb, algt, &queue);
+		return cryptd_create_hash(tmpl, tb, algt);
 	case CRYPTO_ALG_TYPE_AEAD:
-		return cryptd_create_aead(tmpl, tb, algt, &queue);
+		return cryptd_create_aead(tmpl, tb, algt);
 	}
 
 	return -EINVAL;
@@ -891,7 +835,7 @@ static int __init cryptd_init(void)
 	if (!cryptd_wq)
 		return -ENOMEM;
 
-	err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
+	err = cryptd_init_queue(cryptd_max_cpu_qlen);
 	if (err)
 		goto err_destroy_wq;
 
@@ -902,7 +846,7 @@ static int __init cryptd_init(void)
 	return 0;
 
 err_fini_queue:
-	cryptd_fini_queue(&queue);
+	cryptd_fini_queue();
 err_destroy_wq:
 	destroy_workqueue(cryptd_wq);
 	return err;
@@ -911,7 +855,7 @@ static int __init cryptd_init(void)
 static void __exit cryptd_exit(void)
 {
 	destroy_workqueue(cryptd_wq);
-	cryptd_fini_queue(&queue);
+	cryptd_fini_queue();
 	crypto_unregister_template(&cryptd_tmpl);
 }
 
-- 
2.56.1


      parent reply	other threads:[~2026-12-03  8:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:03 [PATCH 0/5] crypto: Remove crypto_simd and clean up cryptd Eric Biggers
2026-12-03  8:03 ` [PATCH 1/5] crypto: simd - Remove unused crypto_simd module Eric Biggers
2026-12-03  8:03 ` [PATCH 2/5] crypto: cryptd - Remove unused AEAD functions Eric Biggers
2026-12-03  8:03 ` [PATCH 3/5] crypto: cryptd - Remove pointless reference count Eric Biggers
2026-12-03  8:03 ` [PATCH 4/5] crypto: cryptd - Remove unused CRYPTO_ALG_INTERNAL support Eric Biggers
2026-12-03  8:03 ` Eric Biggers [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203080333.145409-6-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=dtsen@linux.ibm.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox