From patchwork Thu Nov 10 08:13:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623421 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7812C4332F for ; Thu, 10 Nov 2022 08:15:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231571AbiKJIPK (ORCPT ); Thu, 10 Nov 2022 03:15:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232323AbiKJIPJ (ORCPT ); Thu, 10 Nov 2022 03:15:09 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54F83AE4D for ; Thu, 10 Nov 2022 00:15:06 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CBEA861DB1 for ; Thu, 10 Nov 2022 08:15:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C340C433D7 for ; Thu, 10 Nov 2022 08:15:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068105; bh=BZEvWbxY95nJBhrcWwnIURZrOns/m0Y4Ua+jzzKP1ew=; h=From:To:Subject:Date:In-Reply-To:References:From; b=QLWSjn+oWCKqaa8HGKdp0gtgsVGcgnlTvq3HMknBQzJiehZ64opOfqneplaFnwp/o 8Iy0h+vmOjf8YzVITYtCHcfCC3jUpfNfttw3JQ5QSuk9mInl85hNJg9T6to5QuMKpM XFjeISCPIhG659fuyUU5ZuZ/RjgDWYXebSW6/9HpIhblrPInCvnuyFhUlMoWXzHTsh LjvBGHv1I1s9WsnWnyV185yiVKH6aIqF9zd6ItqN0/wRNDKEm31CS5XH14X1Hi2u5i /V16RMG3eFc/azaPq4U/k+Il+IYFEBnK8WVM3BWuwDbnCVGUnF6XGTup0yO3UkxUAM xjU15EiWinj4A== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 1/6] crypto: optimize algorithm registration when self-tests disabled Date: Thu, 10 Nov 2022 00:13:41 -0800 Message-Id: <20221110081346.336046-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Currently, registering an algorithm with the crypto API always causes a notification to be posted to the "cryptomgr", which then creates a kthread to self-test the algorithm. However, if self-tests are disabled in the kconfig (as is the default option), then this kthread just notifies waiters that the algorithm has been tested, then exits. This causes a significant amount of overhead, especially in the kthread creation and destruction, which is not necessary at all. For example, in a quick test I found that booting a "minimum" x86_64 kernel with all the crypto options enabled (except for the self-tests) takes about 400ms until PID 1 can start. Of that, a full 13ms is spent just doing this pointless dance, involving a kthread being created, run, and destroyed over 200 times. That's over 3% of the entire kernel start time. Fix this by just skipping the creation of the test larval and the posting of the registration notification entirely, when self-tests are disabled. Signed-off-by: Eric Biggers --- crypto/algapi.c | 151 ++++++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 69 deletions(-) base-commit: f67dd6ce0723ad013395f20a3f79d8a437d3f455 diff --git a/crypto/algapi.c b/crypto/algapi.c index 5c69ff8e8fa5c..8bbbd5dbbe157 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -222,12 +222,62 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, } EXPORT_SYMBOL_GPL(crypto_remove_spawns); +static void crypto_alg_finish_registration(struct crypto_alg *alg, + bool fulfill_requests, + struct list_head *algs_to_put) +{ + struct crypto_alg *q; + + list_for_each_entry(q, &crypto_alg_list, cra_list) { + if (q == alg) + continue; + + if (crypto_is_moribund(q)) + continue; + + if (crypto_is_larval(q)) { + struct crypto_larval *larval = (void *)q; + + /* + * Check to see if either our generic name or + * specific name can satisfy the name requested + * by the larval entry q. + */ + if (strcmp(alg->cra_name, q->cra_name) && + strcmp(alg->cra_driver_name, q->cra_name)) + continue; + + if (larval->adult) + continue; + if ((q->cra_flags ^ alg->cra_flags) & larval->mask) + continue; + + if (fulfill_requests && crypto_mod_get(alg)) + larval->adult = alg; + else + larval->adult = ERR_PTR(-EAGAIN); + + continue; + } + + if (strcmp(alg->cra_name, q->cra_name)) + continue; + + if (strcmp(alg->cra_driver_name, q->cra_driver_name) && + q->cra_priority > alg->cra_priority) + continue; + + crypto_remove_spawns(q, algs_to_put, alg); + } +} + static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg) { struct crypto_larval *larval; - if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER)) - return NULL; + if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) || + IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)) + return NULL; /* No self-test needed */ larval = crypto_larval_alloc(alg->cra_name, alg->cra_flags | CRYPTO_ALG_TESTED, 0); @@ -248,7 +298,8 @@ static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg) return larval; } -static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) +static struct crypto_larval * +__crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put) { struct crypto_alg *q; struct crypto_larval *larval; @@ -259,9 +310,6 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) INIT_LIST_HEAD(&alg->cra_users); - /* No cheating! */ - alg->cra_flags &= ~CRYPTO_ALG_TESTED; - ret = -EEXIST; list_for_each_entry(q, &crypto_alg_list, cra_list) { @@ -288,12 +336,17 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) list_add(&alg->cra_list, &crypto_alg_list); - if (larval) + crypto_stats_init(alg); + + if (larval) { + /* No cheating! */ + alg->cra_flags &= ~CRYPTO_ALG_TESTED; + list_add(&larval->alg.cra_list, &crypto_alg_list); - else + } else { alg->cra_flags |= CRYPTO_ALG_TESTED; - - crypto_stats_init(alg); + crypto_alg_finish_registration(alg, true, algs_to_put); + } out: return larval; @@ -341,7 +394,10 @@ void crypto_alg_tested(const char *name, int err) alg->cra_flags |= CRYPTO_ALG_TESTED; - /* Only satisfy larval waiters if we are the best. */ + /* + * If a higher-priority implementation of the same algorithm is + * currently being tested, then don't fulfill request larvals. + */ best = true; list_for_each_entry(q, &crypto_alg_list, cra_list) { if (crypto_is_moribund(q) || !crypto_is_larval(q)) @@ -356,47 +412,7 @@ void crypto_alg_tested(const char *name, int err) } } - list_for_each_entry(q, &crypto_alg_list, cra_list) { - if (q == alg) - continue; - - if (crypto_is_moribund(q)) - continue; - - if (crypto_is_larval(q)) { - struct crypto_larval *larval = (void *)q; - - /* - * Check to see if either our generic name or - * specific name can satisfy the name requested - * by the larval entry q. - */ - if (strcmp(alg->cra_name, q->cra_name) && - strcmp(alg->cra_driver_name, q->cra_name)) - continue; - - if (larval->adult) - continue; - if ((q->cra_flags ^ alg->cra_flags) & larval->mask) - continue; - - if (best && crypto_mod_get(alg)) - larval->adult = alg; - else - larval->adult = ERR_PTR(-EAGAIN); - - continue; - } - - if (strcmp(alg->cra_name, q->cra_name)) - continue; - - if (strcmp(alg->cra_driver_name, q->cra_driver_name) && - q->cra_priority > alg->cra_priority) - continue; - - crypto_remove_spawns(q, &list, alg); - } + crypto_alg_finish_registration(alg, best, &list); complete: complete_all(&test->completion); @@ -423,7 +439,7 @@ EXPORT_SYMBOL_GPL(crypto_remove_final); int crypto_register_alg(struct crypto_alg *alg) { struct crypto_larval *larval; - bool test_started; + LIST_HEAD(algs_to_put); int err; alg->cra_flags &= ~CRYPTO_ALG_DEAD; @@ -432,17 +448,16 @@ int crypto_register_alg(struct crypto_alg *alg) return err; down_write(&crypto_alg_sem); - larval = __crypto_register_alg(alg); - test_started = static_key_enabled(&crypto_boot_test_finished); + larval = __crypto_register_alg(alg, &algs_to_put); if (!IS_ERR_OR_NULL(larval)) - larval->test_started = test_started; + larval->test_started = static_key_enabled(&crypto_boot_test_finished); up_write(&crypto_alg_sem); - if (IS_ERR_OR_NULL(larval)) + if (IS_ERR(larval)) return PTR_ERR(larval); - - if (test_started) + if (larval && larval->test_started) crypto_wait_for_test(larval); + crypto_remove_final(&algs_to_put); return 0; } EXPORT_SYMBOL_GPL(crypto_register_alg); @@ -619,6 +634,7 @@ int crypto_register_instance(struct crypto_template *tmpl, struct crypto_larval *larval; struct crypto_spawn *spawn; u32 fips_internal = 0; + LIST_HEAD(algs_to_put); int err; err = crypto_check_alg(&inst->alg); @@ -650,7 +666,7 @@ int crypto_register_instance(struct crypto_template *tmpl, inst->alg.cra_flags |= (fips_internal & CRYPTO_ALG_FIPS_INTERNAL); - larval = __crypto_register_alg(&inst->alg); + larval = __crypto_register_alg(&inst->alg, &algs_to_put); if (IS_ERR(larval)) goto unlock; else if (larval) @@ -662,15 +678,12 @@ int crypto_register_instance(struct crypto_template *tmpl, unlock: up_write(&crypto_alg_sem); - err = PTR_ERR(larval); - if (IS_ERR_OR_NULL(larval)) - goto err; - - crypto_wait_for_test(larval); - err = 0; - -err: - return err; + if (IS_ERR(larval)) + return PTR_ERR(larval); + if (larval) + crypto_wait_for_test(larval); + crypto_remove_final(&algs_to_put); + return 0; } EXPORT_SYMBOL_GPL(crypto_register_instance); From patchwork Thu Nov 10 08:13:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623996 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C8ECC43217 for ; Thu, 10 Nov 2022 08:15:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232323AbiKJIPL (ORCPT ); Thu, 10 Nov 2022 03:15:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232335AbiKJIPJ (ORCPT ); Thu, 10 Nov 2022 03:15:09 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D338B1E2 for ; Thu, 10 Nov 2022 00:15:06 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0A91F61D9D for ; Thu, 10 Nov 2022 08:15:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61799C433B5 for ; Thu, 10 Nov 2022 08:15:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068105; bh=HoxPqfrM4U0nnbyfWtIiSeiwkxTify/Y80YuyaZDqeE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=J09cRuLAL87uENV0P0sR5eHimXYb6NpomHCi5fqoPrcKsUITPQrVkMkB6LyYsaPcm hVRw/T4DyRr/6nHUqHveSFDlrVSc/rql3JShWZ9MrcITd+gua0A0F9qo0ItyB9VQKh LD+XSjA/1cLwSg1vWQCF05Ge8BUiTn6ID6VOdjiN24yLbs61btDvEwzrX5yg+MdLI/ ywqPkh+Os/mzwrPttnmfjwLz30I/hhfNgx9HWXLfgRW7JuW9lJczCD46WskTOQAmwF Zxu3pppYhA4iMU2UZIgjIwIJJxKcWk5SOalkDUnprWoMLfVEaRHfGWyBiGMvcpkS1H GD3LYhA6eYMiw== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 2/6] crypto: optimize registration of internal algorithms Date: Thu, 10 Nov 2022 00:13:42 -0800 Message-Id: <20221110081346.336046-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Since algboss always skips testing of algorithms with the CRYPTO_ALG_INTERNAL flag, there is no need to go through the dance of creating the test kthread, which creates a lot of overhead. Instead, we can just directly finish the algorithm registration, like is now done when self-tests are disabled entirely. Signed-off-by: Eric Biggers --- crypto/algapi.c | 3 ++- crypto/algboss.c | 13 +------------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index 8bbbd5dbbe157..a8fa7c3f51be9 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -276,7 +276,8 @@ static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg) struct crypto_larval *larval; if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) || - IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)) + IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) || + (alg->cra_flags & CRYPTO_ALG_INTERNAL)) return NULL; /* No self-test needed */ larval = crypto_larval_alloc(alg->cra_name, diff --git a/crypto/algboss.c b/crypto/algboss.c index eb5fe84efb83e..13d37320a66eb 100644 --- a/crypto/algboss.c +++ b/crypto/algboss.c @@ -181,12 +181,8 @@ static int cryptomgr_test(void *data) goto skiptest; #endif - if (type & CRYPTO_ALG_TESTED) - goto skiptest; - err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); -skiptest: crypto_alg_tested(param->driver, err); kfree(param); @@ -197,7 +193,6 @@ static int cryptomgr_schedule_test(struct crypto_alg *alg) { struct task_struct *thread; struct crypto_test_param *param; - u32 type; if (!try_module_get(THIS_MODULE)) goto err; @@ -208,13 +203,7 @@ static int cryptomgr_schedule_test(struct crypto_alg *alg) memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver)); memcpy(param->alg, alg->cra_name, sizeof(param->alg)); - type = alg->cra_flags; - - /* Do not test internal algorithms. */ - if (type & CRYPTO_ALG_INTERNAL) - type |= CRYPTO_ALG_TESTED; - - param->type = type; + param->type = alg->cra_flags; thread = kthread_run(cryptomgr_test, param, "cryptomgr_test"); if (IS_ERR(thread)) From patchwork Thu Nov 10 08:13:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623995 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 655C0C4332F for ; Thu, 10 Nov 2022 08:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232490AbiKJIPN (ORCPT ); Thu, 10 Nov 2022 03:15:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232515AbiKJIPK (ORCPT ); Thu, 10 Nov 2022 03:15:10 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B37E1838F for ; Thu, 10 Nov 2022 00:15:08 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E4E77B82089 for ; Thu, 10 Nov 2022 08:15:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96E4DC43470 for ; Thu, 10 Nov 2022 08:15:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068105; bh=Zhs1hHWFhtMYTkWMZA8EQ/jCnYrhMwzyYJR3kWdmiyo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GHvCXqz9myQ8xxgnkYs7PzrDFIeIzGD6JjiF5ALsN5Vu75UkVtPmlMhgA1uHnAcCq 69tkSe7JaOhWHchNrO1/oj2EpPMW4/5i85H+rQhuzV94vmboNNhS6JPtGPnDoYaBgN wGGyskI6mrmAQf0qWFkK9SG417PR+7/V8KyZCU/4DzuHfLelW27rr6PM3+yEz5RkqC P2uCRNtr/BkpNEsoz0XMO0rsp2JXb5c3PLd+DX7ZcTf88E/0Jp0JjSwSeW5qZZ7pSF 3rWFA7yPsDdKhe/JzD+jcDBgV0q3xQmvSv+EZsf4FdxdVb+VOfGqzCMTqtaUz64jEV 3IaLoYvqU5Srg== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 3/6] crypto: compile out crypto_boot_test_finished when tests disabled Date: Thu, 10 Nov 2022 00:13:43 -0800 Message-Id: <20221110081346.336046-4-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers The crypto_boot_test_finished static key is unnecessary when self-tests are disabled in the kconfig, so optimize it out accordingly, along with the entirety of crypto_start_tests(). This mainly avoids the overhead of an unnecessary static_branch_enable() on every boot. Signed-off-by: Eric Biggers --- crypto/algapi.c | 10 ++++++++-- crypto/api.c | 8 +++++--- crypto/internal.h | 13 ++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index a8fa7c3f51be9..fbb4a88251bc8 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -451,7 +451,7 @@ int crypto_register_alg(struct crypto_alg *alg) down_write(&crypto_alg_sem); larval = __crypto_register_alg(alg, &algs_to_put); if (!IS_ERR_OR_NULL(larval)) - larval->test_started = static_key_enabled(&crypto_boot_test_finished); + larval->test_started = crypto_boot_test_finished(); up_write(&crypto_alg_sem); if (IS_ERR(larval)) @@ -1246,6 +1246,11 @@ void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt); #endif +#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS +static void __init crypto_start_tests(void) +{ +} +#else static void __init crypto_start_tests(void) { for (;;) { @@ -1281,8 +1286,9 @@ static void __init crypto_start_tests(void) crypto_wait_for_test(larval); } - static_branch_enable(&crypto_boot_test_finished); + static_branch_enable(&__crypto_boot_test_finished); } +#endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */ static int __init crypto_algapi_init(void) { diff --git a/crypto/api.c b/crypto/api.c index 64f2d365a8e94..3f002fe0336fc 100644 --- a/crypto/api.c +++ b/crypto/api.c @@ -31,8 +31,10 @@ EXPORT_SYMBOL_GPL(crypto_alg_sem); BLOCKING_NOTIFIER_HEAD(crypto_chain); EXPORT_SYMBOL_GPL(crypto_chain); -DEFINE_STATIC_KEY_FALSE(crypto_boot_test_finished); -EXPORT_SYMBOL_GPL(crypto_boot_test_finished); +#ifndef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS +DEFINE_STATIC_KEY_FALSE(__crypto_boot_test_finished); +EXPORT_SYMBOL_GPL(__crypto_boot_test_finished); +#endif static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg); @@ -205,7 +207,7 @@ static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg) struct crypto_larval *larval = (void *)alg; long timeout; - if (!static_branch_likely(&crypto_boot_test_finished)) + if (!crypto_boot_test_finished()) crypto_start_test(larval); timeout = wait_for_completion_killable_timeout( diff --git a/crypto/internal.h b/crypto/internal.h index c08385571853e..a3bc1fbcefde7 100644 --- a/crypto/internal.h +++ b/crypto/internal.h @@ -47,7 +47,18 @@ extern struct list_head crypto_alg_list; extern struct rw_semaphore crypto_alg_sem; extern struct blocking_notifier_head crypto_chain; -DECLARE_STATIC_KEY_FALSE(crypto_boot_test_finished); +#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS +static inline bool crypto_boot_test_finished(void) +{ + return true; +} +#else +DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished); +static inline bool crypto_boot_test_finished(void) +{ + return static_branch_likely(&__crypto_boot_test_finished); +} +#endif #ifdef CONFIG_PROC_FS void __init crypto_init_proc(void); From patchwork Thu Nov 10 08:13:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623420 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C119C4167B for ; Thu, 10 Nov 2022 08:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232335AbiKJIPM (ORCPT ); Thu, 10 Nov 2022 03:15:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232353AbiKJIPJ (ORCPT ); Thu, 10 Nov 2022 03:15:09 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F16F1CFD3 for ; Thu, 10 Nov 2022 00:15:08 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 399B3B8204F for ; Thu, 10 Nov 2022 08:15:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA573C433D6 for ; Thu, 10 Nov 2022 08:15:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068105; bh=BfFXqC1bkD9LGgv1kUjTDncw5VucjsU3M+CVl04dWmA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=e72hJLmUsVpzMwmINzn/eLzFIbqSN7ld3F7gzueYqEEPM3LXx9SPPumsLSOcpAxqJ nLDfcLDOzkUUyz/x8KaGvOshYmS1lWHBCdeJIlXxbDST10tKCoWZTkazDql/YHhbc8 KcB6iilU8p+qEDkASTj2ci9rCxcMcSWBw+etjNJSs//jxnFFCXwxvsDoj8pyRe2zR/ 90P514mX9BTEYyFxpHzvtJ+SIACEn2FzOBgHveU8AvoD+K3OYk5Yi1D/ZJtrI54H6T jDGS/mF+501fncDuxdd079je90EbAbDv0LCSpyNwyh53JYKytc1CkgD5gbMJ+6e7Eo Bw62o4jyNi4DA== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 4/6] crypto: skip kdf_sp800108 self-test when tests disabled Date: Thu, 10 Nov 2022 00:13:44 -0800 Message-Id: <20221110081346.336046-5-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make kdf_sp800108 honor the CONFIG_CRYPTO_MANAGER_DISABLE_TESTS kconfig option, so that it doesn't always waste time running its self-test. Signed-off-by: Eric Biggers --- crypto/kdf_sp800108.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crypto/kdf_sp800108.c b/crypto/kdf_sp800108.c index 58edf7797abfb..c6e3ad82d5f7a 100644 --- a/crypto/kdf_sp800108.c +++ b/crypto/kdf_sp800108.c @@ -125,9 +125,13 @@ static const struct kdf_testvec kdf_ctr_hmac_sha256_tv_template[] = { static int __init crypto_kdf108_init(void) { - int ret = kdf_test(&kdf_ctr_hmac_sha256_tv_template[0], "hmac(sha256)", - crypto_kdf108_setkey, crypto_kdf108_ctr_generate); + int ret; + if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)) + return 0; + + ret = kdf_test(&kdf_ctr_hmac_sha256_tv_template[0], "hmac(sha256)", + crypto_kdf108_setkey, crypto_kdf108_ctr_generate); if (ret) { if (fips_enabled) panic("alg: self-tests for CTR-KDF (hmac(sha256)) failed (rc=%d)\n", From patchwork Thu Nov 10 08:13:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A33AFC43219 for ; Thu, 10 Nov 2022 08:15:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232509AbiKJIPL (ORCPT ); Thu, 10 Nov 2022 03:15:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232350AbiKJIPJ (ORCPT ); Thu, 10 Nov 2022 03:15:09 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 185731B1D0 for ; Thu, 10 Nov 2022 00:15:07 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A752A61DC3 for ; Thu, 10 Nov 2022 08:15:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A520C433D7 for ; Thu, 10 Nov 2022 08:15:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068106; bh=8zY/anngcz+FEltTmxAC07IEno7jSt5nwm0l9nQ8Qow=; h=From:To:Subject:Date:In-Reply-To:References:From; b=uTaoGZt5j2Z8i0LA1gISekJvS8i5USotWkiaV/sIOM4vj98RF92f343yDAwYNfnX/ PdOoJwVWSK/jk2yyCVgQjCFhd9/Jkqw9K1F4JSXCltuPXmroQjorDz9xrm/qWsz/ZQ 9z6IVMKmb7VOZqBcYMk7tgEJNJ45HVLAVpSRKxjix9z2B49VU95jO9U0JRCcEmamDk dOfNVBWGRm6CygW0UBfqeqYeLBq0wrBTSYxghvZJDED0DHr1uaYJ36UP97MxvLES/M mFt2tGmBP76CQObmvegusopiYYpDmRQgf9jSgcRwRt7WEnBVbrdIlWhCTVv2EjXwH2 YD2o0m8cafB9g== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 5/6] crypto: silence noisy kdf_sp800108 self-test Date: Thu, 10 Nov 2022 00:13:45 -0800 Message-Id: <20221110081346.336046-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Make the kdf_sp800108 self-test only print a message on success when fips_enabled, so that it's consistent with testmgr.c and doesn't spam the kernel log with a message that isn't really important. Signed-off-by: Eric Biggers --- crypto/kdf_sp800108.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/kdf_sp800108.c b/crypto/kdf_sp800108.c index c6e3ad82d5f7a..c3f9938e1ad27 100644 --- a/crypto/kdf_sp800108.c +++ b/crypto/kdf_sp800108.c @@ -140,7 +140,7 @@ static int __init crypto_kdf108_init(void) WARN(1, "alg: self-tests for CTR-KDF (hmac(sha256)) failed (rc=%d)\n", ret); - } else { + } else if (fips_enabled) { pr_info("alg: self-tests for CTR-KDF (hmac(sha256)) passed\n"); } From patchwork Thu Nov 10 08:13:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 623419 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7162C433FE for ; Thu, 10 Nov 2022 08:15:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232353AbiKJIPM (ORCPT ); Thu, 10 Nov 2022 03:15:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232490AbiKJIPJ (ORCPT ); Thu, 10 Nov 2022 03:15:09 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9C211AF24 for ; Thu, 10 Nov 2022 00:15:08 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9F299B820F4 for ; Thu, 10 Nov 2022 08:15:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F12AC433C1 for ; Thu, 10 Nov 2022 08:15:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668068106; bh=1wrT0gidSrDF4d1H3dgpVP/jA8cCHSsxQKy1HF5H8Lc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=n8+lYgwRrXgnsJDueBtOX6bBXDAyeiw4maHXOzNICPBu0tFDBNTiWlAO0XkLpiDaO mcJTCA8A1Np+8JOFwsWIAQHPjqPb9YIwXE1sOchFJAwEiXBvoZaGI4wBsCxtvJePx/ M/XMIVY/8cSC15EkC0Pjbl4s3/rcVMgvXXJmw9VHghBPKjcK2ohCRYgdXW3HhfFolL oWiTLjomZIZDW5kjKIrYLZwIQ47ERdVArPlfrhBcklQfLRZXq01P3CRU2rCa8OsR2I c9JuaEUNDIo2Msq4IDbtfbrnLpzKjbVqWGxCsRxAvRzT99MbqjTZO/HEnG5WDTHhey RG5EiGMdwBqxA== From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 6/6] crypto: compile out test-related algboss code when tests disabled Date: Thu, 10 Nov 2022 00:13:46 -0800 Message-Id: <20221110081346.336046-7-ebiggers@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221110081346.336046-1-ebiggers@kernel.org> References: <20221110081346.336046-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers When CONFIG_CRYPTO_MANAGER_DISABLE_TESTS is set, the code in algboss.c that handles CRYPTO_MSG_ALG_REGISTER is unnecessary, so make it be compiled out. Signed-off-by: Eric Biggers --- crypto/algboss.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crypto/algboss.c b/crypto/algboss.c index 13d37320a66eb..f3c6c9fe133d5 100644 --- a/crypto/algboss.c +++ b/crypto/algboss.c @@ -171,15 +171,17 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval) return NOTIFY_OK; } +#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS +static int cryptomgr_schedule_test(struct crypto_alg *alg) +{ + return NOTIFY_DONE; +} +#else static int cryptomgr_test(void *data) { struct crypto_test_param *param = data; u32 type = param->type; - int err = 0; - -#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS - goto skiptest; -#endif + int err; err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); @@ -218,6 +220,7 @@ static int cryptomgr_schedule_test(struct crypto_alg *alg) err: return NOTIFY_OK; } +#endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */ static int cryptomgr_notify(struct notifier_block *this, unsigned long msg, void *data)