From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796285045; b=JUX3FFec9ayB1pGoBTkRijE1YTRKz0loj1wJruJLF11qgFOw/W3pO79qIAT7GeOEiRcn 3sqnh6gM1KXL+Q2nEk8TTgO4UQlxb1pujGVNDPrHQ/JbyjrmUMgUpVuYPr50+rOOEOLFe kTJGslnZ1JPgFIuw7tvSV9apPVKwr5kFGTVuxFE9G5iZMCrMoGWltf+EUCxGuC/LpnZnp H3tL8w2r+Tf6niy0mjYAhbwISbXNsdORU3aKLSjSNdvarfAEnug8fiLD4OHp3FCEZtbLR mn7oZh6ETEIkUkJinuIRId9jpjnEfYkc8SNAJtFM814n7Mx3USVVghQ62xQpRB/Zxcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796285045; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=3LRWu9eerJb+nohL6Byjow5j4D/8Ae2VUe/KxFdLhL4=; b=jRh7XXm0a2K7ZW/3OrbFYISJUI3JWB4zJupApFkffr7nUz41ThswZ/IjkGlzEbr/pogV Umf65Lg6xFYw9uwBe0I3atIaggVFkKvIs5CpJ2e9PNrcuyKWfcTPNj1Eb/pQeyhRgq2P0 DAekPlBbQ/fDbAMG0T/MSHpIAZyqyPA9aiZfc4ZORAN/GcQzoxZ1D0/Vq5QBO++EToZtW SO4l2e0clzagf9J1mZvQ0H5lBR+5QteNWef3LdMjbH/v3Q37akSLfElMr8UEhYv2zoZcl h2RKJfcPq+XonJsyz3BRR/lhSjNjLROQ4d7SRhqclsv4gfizqXUQTgi7g3zGVL0Tssg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PJBiggGx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 497F7606A33E for ; Thu, 3 Dec 2026 08:04:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PJBiggGx Received: by smtp.kernel.org (Postfix) with ESMTPSA id B90791F00896; Thu, 3 Dec 2026 08:04:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796285044; bh=3LRWu9eerJb+nohL6Byjow5j4D/8Ae2VUe/KxFdLhL4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=PJBiggGx5MKtg9CsjpR8Lt9QBHSJ5rJri3ihfGZBqavNaQVIBu2vY18SsY6+4MVmd xcEBDSaP39vn3PwGrifPwzcfUtX2AgahJnZ5kce0Eg8T7hw6Z4M6NBwZ0laIafgmJk WrTCVG9xl/18CdK/Cq0P4dAs1gkDokO0BJJb5bxeLldvJeh2cLqU+cGKN8E2YbB0l9 Gf7zolyaKpUPMYL9ZldHcoqFDTcf1FOkWaNX9swHSbKHnr0fPirdI9T05auZfMmutc EI9FtZNbHBCFyBCO8c+aJxmK9R+ejJVxrAOGEL4ZpX61ZKX4B/8x4bH+cXRXJ87lNK H7w6UhA/f3GLQ== From: Eric Biggers To: Herbert Xu , linux-crypto@vger.kernel.org Cc: Danny Tsen , Eric Biggers Subject: [PATCH 5/5] crypto: cryptd - Remove pointless per-instance queue pointer Date: Thu, 3 Dec 2026 00:03:33 -0800 Message-ID: <20261203080333.145409-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203080333.145409-1-ebiggers@kernel.org> References: <20261203080333.145409-1-ebiggers@kernel.org> X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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