Skip to content

Commit daae28d

Browse files
vivierherbertx
authored andcommitted
hwrng: core - move add_early_randomness() out of rng_mutex
add_early_randomness() is called every time a new rng backend is added and every time it is set as the current rng provider. add_early_randomness() is called from functions locking rng_mutex, and if it hangs all the hw_random framework hangs: we can't read sysfs, add or remove a backend. This patch move add_early_randomness() out of the rng_mutex zone. It only needs the reading_mutex. Signed-off-by: Laurent Vivier <lvivier@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent eff9771 commit daae28d

1 file changed

Lines changed: 44 additions & 16 deletions

File tree

‎drivers/char/hw_random/core.c‎

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,22 @@ static void drop_current_rng(void)
112112
}
113113

114114
/* Returns ERR_PTR(), NULL or refcounted hwrng */
115+
static struct hwrng *get_current_rng_nolock(void)
116+
{
117+
if (current_rng)
118+
kref_get(&current_rng->ref);
119+
120+
return current_rng;
121+
}
122+
115123
static struct hwrng *get_current_rng(void)
116124
{
117125
struct hwrng *rng;
118126

119127
if (mutex_lock_interruptible(&rng_mutex))
120128
return ERR_PTR(-ERESTARTSYS);
121129

122-
rng = current_rng;
123-
if (rng)
124-
kref_get(&rng->ref);
130+
rng = get_current_rng_nolock();
125131

126132
mutex_unlock(&rng_mutex);
127133
return rng;
@@ -156,8 +162,6 @@ static int hwrng_init(struct hwrng *rng)
156162
reinit_completion(&rng->cleanup_done);
157163

158164
skip_init:
159-
add_early_randomness(rng);
160-
161165
current_quality = rng->quality ? : default_quality;
162166
if (current_quality > 1024)
163167
current_quality = 1024;
@@ -321,12 +325,13 @@ static ssize_t hwrng_attr_current_store(struct device *dev,
321325
const char *buf, size_t len)
322326
{
323327
int err = -ENODEV;
324-
struct hwrng *rng;
328+
struct hwrng *rng, *old_rng, *new_rng;
325329

326330
err = mutex_lock_interruptible(&rng_mutex);
327331
if (err)
328332
return -ERESTARTSYS;
329333

334+
old_rng = current_rng;
330335
if (sysfs_streq(buf, "")) {
331336
err = enable_best_rng();
332337
} else {
@@ -338,9 +343,15 @@ static ssize_t hwrng_attr_current_store(struct device *dev,
338343
}
339344
}
340345
}
341-
346+
new_rng = get_current_rng_nolock();
342347
mutex_unlock(&rng_mutex);
343348

349+
if (new_rng) {
350+
if (new_rng != old_rng)
351+
add_early_randomness(new_rng);
352+
put_rng(new_rng);
353+
}
354+
344355
return err ? : len;
345356
}
346357

@@ -460,13 +471,17 @@ static void start_khwrngd(void)
460471
int hwrng_register(struct hwrng *rng)
461472
{
462473
int err = -EINVAL;
463-
struct hwrng *old_rng, *tmp;
474+
struct hwrng *old_rng, *new_rng, *tmp;
464475
struct list_head *rng_list_ptr;
465476

466477
if (!rng->name || (!rng->data_read && !rng->read))
467478
goto out;
468479

469480
mutex_lock(&rng_mutex);
481+
482+
old_rng = current_rng;
483+
new_rng = NULL;
484+
470485
/* Must not register two RNGs with the same name. */
471486
err = -EEXIST;
472487
list_for_each_entry(tmp, &rng_list, list) {
@@ -485,7 +500,6 @@ int hwrng_register(struct hwrng *rng)
485500
}
486501
list_add_tail(&rng->list, rng_list_ptr);
487502

488-
old_rng = current_rng;
489503
err = 0;
490504
if (!old_rng ||
491505
(!cur_rng_set_by_user && rng->quality > old_rng->quality)) {
@@ -499,30 +513,37 @@ int hwrng_register(struct hwrng *rng)
499513
goto out_unlock;
500514
}
501515

502-
if (old_rng && !rng->init) {
516+
new_rng = rng;
517+
kref_get(&new_rng->ref);
518+
out_unlock:
519+
mutex_unlock(&rng_mutex);
520+
521+
if (new_rng) {
522+
if (new_rng != old_rng || !rng->init) {
503523
/*
504524
* Use a new device's input to add some randomness to
505525
* the system. If this rng device isn't going to be
506526
* used right away, its init function hasn't been
507-
* called yet; so only use the randomness from devices
508-
* that don't need an init callback.
527+
* called yet by set_current_rng(); so only use the
528+
* randomness from devices that don't need an init callback
509529
*/
510-
add_early_randomness(rng);
530+
add_early_randomness(new_rng);
531+
}
532+
put_rng(new_rng);
511533
}
512-
513-
out_unlock:
514-
mutex_unlock(&rng_mutex);
515534
out:
516535
return err;
517536
}
518537
EXPORT_SYMBOL_GPL(hwrng_register);
519538

520539
void hwrng_unregister(struct hwrng *rng)
521540
{
541+
struct hwrng *old_rng, *new_rng;
522542
int err;
523543

524544
mutex_lock(&rng_mutex);
525545

546+
old_rng = current_rng;
526547
list_del(&rng->list);
527548
if (current_rng == rng) {
528549
err = enable_best_rng();
@@ -532,13 +553,20 @@ void hwrng_unregister(struct hwrng *rng)
532553
}
533554
}
534555

556+
new_rng = get_current_rng_nolock();
535557
if (list_empty(&rng_list)) {
536558
mutex_unlock(&rng_mutex);
537559
if (hwrng_fill)
538560
kthread_stop(hwrng_fill);
539561
} else
540562
mutex_unlock(&rng_mutex);
541563

564+
if (new_rng) {
565+
if (old_rng != new_rng)
566+
add_early_randomness(new_rng);
567+
put_rng(new_rng);
568+
}
569+
542570
wait_for_completion(&rng->cleanup_done);
543571
}
544572
EXPORT_SYMBOL_GPL(hwrng_unregister);

0 commit comments

Comments
 (0)