Skip to content

Commit cad439f

Browse files
committed
crypto: api - Do not create test larvals if manager is disabled
The delayed boot-time testing patch created a dependency loop between api.c and algapi.c because it added a crypto_alg_tested call to the former when the crypto manager is disabled. We could instead avoid creating the test larvals if the crypto manager is disabled. This avoids the dependency loop as well as saving some unnecessary work, albeit in a very unlikely case. Reported-by: Nathan Chancellor <[email protected]> Reported-by: Naresh Kamboju <[email protected]> Reported-by: kernel test robot <[email protected]> Fixes: adad556 ("crypto: api - Fix built-in testing dependency failures") Signed-off-by: Herbert Xu <[email protected]>
1 parent 3ae88f6 commit cad439f

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

crypto/algapi.c

+36-20
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
216216
}
217217
EXPORT_SYMBOL_GPL(crypto_remove_spawns);
218218

219+
static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg)
220+
{
221+
struct crypto_larval *larval;
222+
223+
if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER))
224+
return NULL;
225+
226+
larval = crypto_larval_alloc(alg->cra_name,
227+
alg->cra_flags | CRYPTO_ALG_TESTED, 0);
228+
if (IS_ERR(larval))
229+
return larval;
230+
231+
larval->adult = crypto_mod_get(alg);
232+
if (!larval->adult) {
233+
kfree(larval);
234+
return ERR_PTR(-ENOENT);
235+
}
236+
237+
refcount_set(&larval->alg.cra_refcnt, 1);
238+
memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
239+
CRYPTO_MAX_ALG_NAME);
240+
larval->alg.cra_priority = alg->cra_priority;
241+
242+
return larval;
243+
}
244+
219245
static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
220246
{
221247
struct crypto_alg *q;
@@ -250,31 +276,20 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
250276
goto err;
251277
}
252278

253-
larval = crypto_larval_alloc(alg->cra_name,
254-
alg->cra_flags | CRYPTO_ALG_TESTED, 0);
279+
larval = crypto_alloc_test_larval(alg);
255280
if (IS_ERR(larval))
256281
goto out;
257282

258-
ret = -ENOENT;
259-
larval->adult = crypto_mod_get(alg);
260-
if (!larval->adult)
261-
goto free_larval;
262-
263-
refcount_set(&larval->alg.cra_refcnt, 1);
264-
memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
265-
CRYPTO_MAX_ALG_NAME);
266-
larval->alg.cra_priority = alg->cra_priority;
267-
268283
list_add(&alg->cra_list, &crypto_alg_list);
269-
list_add(&larval->alg.cra_list, &crypto_alg_list);
284+
285+
if (larval)
286+
list_add(&larval->alg.cra_list, &crypto_alg_list);
270287

271288
crypto_stats_init(alg);
272289

273290
out:
274291
return larval;
275292

276-
free_larval:
277-
kfree(larval);
278293
err:
279294
larval = ERR_PTR(ret);
280295
goto out;
@@ -403,10 +418,11 @@ int crypto_register_alg(struct crypto_alg *alg)
403418
down_write(&crypto_alg_sem);
404419
larval = __crypto_register_alg(alg);
405420
test_started = static_key_enabled(&crypto_boot_test_finished);
406-
larval->test_started = test_started;
421+
if (!IS_ERR_OR_NULL(larval))
422+
larval->test_started = test_started;
407423
up_write(&crypto_alg_sem);
408424

409-
if (IS_ERR(larval))
425+
if (IS_ERR_OR_NULL(larval))
410426
return PTR_ERR(larval);
411427

412428
if (test_started)
@@ -616,8 +632,8 @@ int crypto_register_instance(struct crypto_template *tmpl,
616632
larval = __crypto_register_alg(&inst->alg);
617633
if (IS_ERR(larval))
618634
goto unlock;
619-
620-
larval->test_started = true;
635+
else if (larval)
636+
larval->test_started = true;
621637

622638
hlist_add_head(&inst->list, &tmpl->instances);
623639
inst->tmpl = tmpl;
@@ -626,7 +642,7 @@ int crypto_register_instance(struct crypto_template *tmpl,
626642
up_write(&crypto_alg_sem);
627643

628644
err = PTR_ERR(larval);
629-
if (IS_ERR(larval))
645+
if (IS_ERR_OR_NULL(larval))
630646
goto err;
631647

632648
crypto_wait_for_test(larval);

crypto/api.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,8 @@ void crypto_wait_for_test(struct crypto_larval *larval)
167167
int err;
168168

169169
err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
170-
if (err != NOTIFY_STOP) {
171-
if (WARN_ON(err != NOTIFY_DONE))
172-
goto out;
173-
crypto_alg_tested(larval->alg.cra_driver_name, 0);
174-
}
170+
if (WARN_ON_ONCE(err != NOTIFY_STOP))
171+
goto out;
175172

176173
err = wait_for_completion_killable(&larval->completion);
177174
WARN_ON(err);

0 commit comments

Comments
 (0)